Skip to content

Commit

Permalink
Add example tags
Browse files Browse the repository at this point in the history
  • Loading branch information
lixue323 committed Feb 28, 2020
1 parent 8dc527a commit 4edd319
Show file tree
Hide file tree
Showing 12 changed files with 418 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/tags/LICENSE.md
Original file line number Diff line number Diff line change
@@ -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.
30 changes: 30 additions & 0 deletions examples/tags/README.md
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 12 additions & 0 deletions examples/tags/add.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
- name: Add tags for below resource
hosts: localhost
remote_user: root

roles:
- vpc
- slb
- security_group
- instance
- disk
- eip
52 changes: 52 additions & 0 deletions examples/tags/group_vars/all
Original file line number Diff line number Diff line change
@@ -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
82 changes: 82 additions & 0 deletions examples/tags/remove.yml
Original file line number Diff line number Diff line change
@@ -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 }}'
22 changes: 22 additions & 0 deletions examples/tags/roles/disk/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -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 }}'
18 changes: 18 additions & 0 deletions examples/tags/roles/eip/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -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 }}'
40 changes: 40 additions & 0 deletions examples/tags/roles/instance/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -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 }}'
18 changes: 18 additions & 0 deletions examples/tags/roles/security_group/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -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 }}'
27 changes: 27 additions & 0 deletions examples/tags/roles/slb/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -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 }}'
34 changes: 34 additions & 0 deletions examples/tags/roles/vpc/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -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 }}'
Loading

0 comments on commit 4edd319

Please sign in to comment.