From 4edd3199949775d8e54f9e9a292be6c3c2e502f1 Mon Sep 17 00:00:00 2001 From: lixue <1120722567@qq.com> Date: Mon, 24 Feb 2020 16:16:30 +0800 Subject: [PATCH] Add example tags --- examples/tags/LICENSE.md | 4 + examples/tags/README.md | 30 +++++++ examples/tags/add.yml | 12 +++ examples/tags/group_vars/all | 52 ++++++++++++ examples/tags/remove.yml | 82 +++++++++++++++++++ examples/tags/roles/disk/tasks/main.yml | 22 +++++ examples/tags/roles/eip/tasks/main.yml | 18 ++++ examples/tags/roles/instance/tasks/main.yml | 40 +++++++++ .../tags/roles/security_group/tasks/main.yml | 18 ++++ examples/tags/roles/slb/tasks/main.yml | 27 ++++++ examples/tags/roles/vpc/tasks/main.yml | 34 ++++++++ examples/tags/sweep.yml | 79 ++++++++++++++++++ 12 files changed, 418 insertions(+) create mode 100644 examples/tags/LICENSE.md create mode 100644 examples/tags/README.md create mode 100644 examples/tags/add.yml create mode 100644 examples/tags/group_vars/all create mode 100644 examples/tags/remove.yml create mode 100644 examples/tags/roles/disk/tasks/main.yml create mode 100644 examples/tags/roles/eip/tasks/main.yml create mode 100644 examples/tags/roles/instance/tasks/main.yml create mode 100644 examples/tags/roles/security_group/tasks/main.yml create mode 100644 examples/tags/roles/slb/tasks/main.yml create mode 100644 examples/tags/roles/vpc/tasks/main.yml create mode 100644 examples/tags/sweep.yml diff --git a/examples/tags/LICENSE.md b/examples/tags/LICENSE.md new file mode 100644 index 00000000..bf7c12c3 --- /dev/null +++ b/examples/tags/LICENSE.md @@ -0,0 +1,4 @@ +Copyright (C) 2017-present Alibaba Cloud, Inc. + +This work is licensed under the Creative Commons Attribution 3.0 Unported License. +To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/deed.en_US. diff --git a/examples/tags/README.md b/examples/tags/README.md new file mode 100644 index 00000000..a7c3b29b --- /dev/null +++ b/examples/tags/README.md @@ -0,0 +1,30 @@ +## Add or Remove tags for resources + +- Requires Ansible 2.9 or newer +- Requires Ansible-Alicloud 1.17.0 or newer +- Requires footmark 1.18.0 or newer + + +These playbooks filter resources and add tags for them. + +These playbooks' hosts default to `localhost`. To use, make the file and edit the `hosts` inventory file to include the names or IPs of the servers +you want to deploy. + +Then run the playbook, like this: + + ansible-playbook Add_tags.yml + or + ansible-playbook -i hosts Add_tags.yml + +When the run is complete, you can login in the Alicloud console to check them. + +### Notes + +Here are some notes before running these playbooks: + +- When add tags, If tags exist, changed is false. +- When remove tags, If tags not exist, changed is false. + +### Ideas for Improvement +We would love to see contributions and improvements, so please fork this +repository on GitHub and send us your changes via pull requests. diff --git a/examples/tags/add.yml b/examples/tags/add.yml new file mode 100644 index 00000000..c819f9b5 --- /dev/null +++ b/examples/tags/add.yml @@ -0,0 +1,12 @@ +--- +- name: Add tags for below resource + hosts: localhost + remote_user: root + + roles: + - vpc + - slb + - security_group + - instance + - disk + - eip \ No newline at end of file diff --git a/examples/tags/group_vars/all b/examples/tags/group_vars/all new file mode 100644 index 00000000..7eea33a9 --- /dev/null +++ b/examples/tags/group_vars/all @@ -0,0 +1,52 @@ +--- +# ecs instance filter parameters +instance_name: 'Instance_From_Ansible' +instance_tags: {created_by: Ansible-Alicloud, created_from: example-alicloud-tags} +image_id: "ubuntu_18_04_64_20G_alibase_20190624.vhd" +instance_type: ecs.g5.large +instance_description: "Create a new ECS instance resource via Ansible example alicloud-tags." +host_name: "my-instance-from-ansible" +password: "Test12345" + +allocate_public_ip: True +internet_charge_type: "PayByTraffic" +max_bandwidth_in: 200 +max_bandwidth_out: 50 + +system_disk_category: "cloud_ssd" +system_disk_size: 50 + +# security group filter parameters +security_group_name: 'Security_Group_From_Ansible' +security_group_tags: {created_by: Ansible-Alicloud, created_from: example-alicloud-tags} + +group_description: "Create a new security group resource via Ansible example alicloud-tags." + +# disk names filter parameters +disk_tags: {created_by: Ansible-Alicloud, created_from: example-alicloud-tags} + +disk_name: "Disk_From_Ansible" +disk_category: "cloud_ssd" +disk_description: "Create a new ECS disk resource via Ansible example tags." +data_disk_size: 80 +delete_with_instance: True + +# slb instance filter parameters +slb_name: 'Slb_From_Ansible' +slb_tags: {created_by: Ansible-Alicloud, created_from: example-alicloud-tags} + +# vpc filter parameters +vpc_name: 'Vpc_From_Ansible' +vpc_tags: {created_by: Ansible-Alicloud, created_from: example-alicloud-tags} +vpc_cidr: "172.16.0.0/12" +vpc_description: "Create a new VPC resource via Ansible example alicloud-ecs-vpc." + +# eip filter parameters +eip_name: 'Eip_From_Ansible' +eip_tags: {created_by: Ansible-Alicloud, created_from: example-alicloud-tags} + +# vswitch parameters +vswitch_cidr: "172.16.1.0/24" +vswitch_description: "Create a new VSwitch resource via Ansible example alicloud-tags." +vswitch_name: 'Vswitch_From_Ansible' +availability_zone: cn-beijing-h diff --git a/examples/tags/remove.yml b/examples/tags/remove.yml new file mode 100644 index 00000000..cf492a83 --- /dev/null +++ b/examples/tags/remove.yml @@ -0,0 +1,82 @@ +--- +- name: Remove tags for resource + hosts: localhost + remote_user: root + + tasks: + - name: Filter disk using name + ali_disk_info: + disk_names: ['{{ disk_name }}'] + register: disks + + - name: Changed. Remove disk Tags. + ali_ecs_tag: + state: absent + resource_type: 'disk' + resource_ids: '{{ disks.disk_ids }}' + tags: '{{ disk_tags }}' + + + - name: Filter the existing eip + ali_eip_info: + name_prefix: '{{ eip_name }}' + register: eips + + - name: Changed. Remove eip Tags. + ali_vpc_tag: + state: absent + resource_type: 'eip' + resource_ids: '{{ eips.ids }}' + tags: '{{ eip_tags }}' + + + - name: Filter slb using name_regex + ali_slb_lb_info: + name_prefix: '{{ slb_name }}' + register: slbs + + - name: Changed. Remove slb tags. + ali_slb_tag: + state: absent + resource_type: 'instance' + resource_ids: '{{ slbs.ids }}' + tags: '{{ slb_tags }}' + + + - name: Filter instance using name_regex + ali_instance_info: + name_prefix: '{{ instance_name }}' + register: instances + + - name: Changed. Remove instance tags. + ali_ecs_tag: + state: absent + resource_type: 'instance' + resource_ids: '{{ instances.ids }}' + tags: '{{ instance_tags }}' + + + - name: Filter security group using name_regex + ali_security_group_info: + name_prefix: '{{ security_group_name }}' + register: sgs + + - name: Changed. Remove security group Tags. + ali_ecs_tag: + state: absent + resource_type: 'securitygroup' + resource_ids: '{{ sgs.ids }}' + tags: '{{ security_group_tags }}' + + + - name: Filter vpc using name_regex + ali_vpc_info: + name_prefix: '{{ vpc_name }}' + register: vpcs + + - name: Changed. Remove vpc tags. + ali_vpc_tag: + state: absent + resource_type: 'vpc' + resource_ids: '{{ vpcs.ids }}' + tags: '{{ vpc_tags }}' diff --git a/examples/tags/roles/disk/tasks/main.yml b/examples/tags/roles/disk/tasks/main.yml new file mode 100644 index 00000000..a31a8b1a --- /dev/null +++ b/examples/tags/roles/disk/tasks/main.yml @@ -0,0 +1,22 @@ +--- +- name: Create first new disk and attach it to an instance 0 + ali_disk: + disk_name: '{{ disk_name }}' + disk_category: '{{ disk_category }}' + description: '{{ disk_description }}' + size: '{{ data_disk_size }}' + instance_id: '{{ instances.instances.0.id }}' + delete_with_instance: '{{ delete_with_instance }}' + zone_id: '{{ availability_zone }}' + register: disk_details + +- name: Filter disk using name + ali_disk_info: + disk_names: ['{{ disk_name }}'] + register: disks + +- name: Changed. Add disk Tags. + ali_ecs_tag: + resource_type: 'disk' + resource_ids: '{{ disks.disk_ids }}' + tags: '{{ disk_tags }}' diff --git a/examples/tags/roles/eip/tasks/main.yml b/examples/tags/roles/eip/tasks/main.yml new file mode 100644 index 00000000..c1463514 --- /dev/null +++ b/examples/tags/roles/eip/tasks/main.yml @@ -0,0 +1,18 @@ +--- +- name: Create a new alicloud EIP resource + ali_eip: + internet_charge_type: '{{ internet_charge_type }}' + bandwidth: 1 + name: '{{ eip_name }}' + register: eip + +- name: Filter the existing eip + ali_eip_info: + name_prefix: '{{ eip_name }}' + register: eips + +- name: Changed. Add eip Tags. + ali_vpc_tag: + resource_type: 'eip' + resource_ids: '{{ eips.ids }}' + tags: '{{ eip_tags }}' diff --git a/examples/tags/roles/instance/tasks/main.yml b/examples/tags/roles/instance/tasks/main.yml new file mode 100644 index 00000000..9965f176 --- /dev/null +++ b/examples/tags/roles/instance/tasks/main.yml @@ -0,0 +1,40 @@ +--- +- name: Get the existing ECS instances + ali_instance_info: + name_prefix: '{{ instance_name }}' + filters: + vpc_id: '{{ vpcs.vpcs.0.id }}' + register: instances + +- name: Creating an ECS instance + ali_instance: + image: '{{ image_id }}' + type: '{{ instance_type }}' + instance_name: '{{ instance_name }}' + description: '{{ instance_description }}' + host_name: '{{ host_name }}' + password: '{{ password }}' + + allocate_public_ip: '{{ allocate_public_ip }}' + internet_charge_type: '{{ internet_charge_type }}' + max_bandwidth_in: '{{ max_bandwidth_in }}' + max_bandwidth_out: '{{ max_bandwidth_out }}' + + security_groups: ['{{ sgs.groups.0.id }}'] + vswitch_id: '{{ vswitches.vswitches.0.id }}' + + system_disk_category: '{{ system_disk_category }}' + system_disk_size: '{{ system_disk_size }}' + count: 10 + when: not instances.instances + +- name: Filter instance using name_regex + ali_instance_info: + name_prefix: '{{ instance_name }}' + register: instances + +- name: Changed. Add instance tags. + ali_ecs_tag: + resource_type: 'instance' + resource_ids: '{{ instances.ids }}' + tags: '{{ instance_tags }}' diff --git a/examples/tags/roles/security_group/tasks/main.yml b/examples/tags/roles/security_group/tasks/main.yml new file mode 100644 index 00000000..3c3981a7 --- /dev/null +++ b/examples/tags/roles/security_group/tasks/main.yml @@ -0,0 +1,18 @@ +--- +- name: Create a VPC security group + ali_security_group: + name: '{{ security_group_name }}' + description: '{{ group_description }}' + vpc_id: '{{vpc.vpc.id}}' + register: group + +- name: Filter security group using name_regex + ali_security_group_info: + name_prefix: '{{ security_group_name }}' + register: sgs + +- name: Changed. Add security group Tags. + ali_ecs_tag: + resource_type: 'securitygroup' + resource_ids: '{{ sgs.ids }}' + tags: '{{ security_group_tags }}' diff --git a/examples/tags/roles/slb/tasks/main.yml b/examples/tags/roles/slb/tasks/main.yml new file mode 100644 index 00000000..456595a9 --- /dev/null +++ b/examples/tags/roles/slb/tasks/main.yml @@ -0,0 +1,27 @@ +--- +- name: Creating a public SLB instance + ali_slb_lb: + name: '{{ slb_name }}-public' + internet_charge_type: '{{ internet_charge_type }}' + is_internet: True + spec: slb.s1.small + register: slbpub + +- name: Creating a private SLB instance + ali_slb_lb: + name: '{{ slb_name }}-private' + internet_charge_type: '{{ internet_charge_type }}' + spec: slb.s1.small + vswitch_id: '{{ vswitches.vswitches.0.id }}' + register: slbpri + +- name: Filter slb using name_regex + ali_slb_lb_info: + name_prefix: '{{ slb_name }}' + register: slbs + +- name: Changed. Add slb tags. + ali_slb_tag: + resource_type: 'instance' + resource_ids: '{{ slbs.ids }}' + tags: '{{ slb_tags }}' diff --git a/examples/tags/roles/vpc/tasks/main.yml b/examples/tags/roles/vpc/tasks/main.yml new file mode 100644 index 00000000..36c39873 --- /dev/null +++ b/examples/tags/roles/vpc/tasks/main.yml @@ -0,0 +1,34 @@ +--- +- name: Create a new alicloud VPC resource + ali_vpc: + cidr_block: '{{ vpc_cidr }}' + vpc_name: '{{ vpc_name }}' + description: '{{ vpc_description }}' + register: vpc + +- name: Create a new alicloud VSwitch resource + ali_vswitch: + alicloud_zone: '{{ availability_zone }}' + cidr_block: '{{ vswitch_cidr }}' + vswitch_name: '{{ vswitch_name }}' + description: '{{ vswitch_description }}' + vpc_id: '{{vpc.vpc.id}}' + register: vswitch + +- name: Get the existing vswitch + ali_vswitch_info: + name_prefix: '{{ vswitch_name }}' + filters: + vpc_id: '{{vpc.vpc.id}}' + register: vswitches + +- name: Filter vpc using name_regex + ali_vpc_info: + name_prefix: '{{ vpc_name }}' + register: vpcs + +- name: Changed. Add vpc tags. + ali_vpc_tag: + resource_type: 'vpc' + resource_ids: '{{ vpcs.ids }}' + tags: '{{ vpc_tags }}' diff --git a/examples/tags/sweep.yml b/examples/tags/sweep.yml new file mode 100644 index 00000000..7da83045 --- /dev/null +++ b/examples/tags/sweep.yml @@ -0,0 +1,79 @@ +--- +- name: Delete resource + hosts: localhost + remote_user: root + + tasks: + - name: Filter the existing eip + ali_eip_info: + name_prefix: '{{ eip_name }}' + register: eips + + - name: Changed. Deleting eips + ali_eip: + ip_address: '{{item.ip_address}}' + state: absent + with_items: '{{eips.eips}}' + + + - name: Filter slb using name_regex + ali_slb_lb_info: + name_prefix: '{{ slb_name }}' + register: slbs + + - name: Changed. Delete slb instance. + ali_slb_lb: + name: '{{ item.name }}' + state: absent + with_items: '{{slbs.load_balancers}}' + + + - name: Filter instance using name_regex + ali_instance_info: + name_prefix: '{{ instance_name }}' + register: instances + + - name: Changed. Deleting instances + ali_instance: + instance_ids: '{{instances.ids}}' + force: True + state: absent + + + - name: Filter security group using name_regex + ali_security_group_info: + name_prefix: '{{ security_group_name }}' + register: sgs + + - name: Changed. Deleting the security groups + ali_security_group: + name: '{{ item.group_name }}' + vpc_id: '{{item.vpc_id}}' + state: absent + with_items: '{{sgs.groups}}' + + + - name: Get the existing vswitch + ali_vswitch_info: + name_prefix: '{{ vswitch_name }}' + register: vswitches + + - name: Changed. Deleting vswitches + ali_vswitch: + vpc_id: '{{ item.vpc_id}}' + cidr_block: '{{ item.cidr_block}}' + state: absent + with_items: '{{vswitches.vswitches}}' + + + - name: Filter vpc using name_regex + ali_vpc_info: + name_prefix: '{{ vpc_name }}' + register: vpcs + + - name: Changed. Deleting vpcs + ali_vpc: + cidr_block: '{{ item.cidr_block }}' + name: '{{ item.vpc_name }}' + state: absent + with_items: '{{vpcs.vpcs}}'