-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
415 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.16.0 or newer | ||
- Requires footmark 1.17.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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
--- | ||
# 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." | ||
group_inboundRules: | ||
- ip_protocol: tcp | ||
port_range: 22/22 | ||
source_cidr_ip: 0.0.0.0/0 | ||
priority: 1 | ||
|
||
- ip_protocol: tcp | ||
port_range: 80/80 | ||
source_cidr_ip: 0.0.0.0/0 | ||
priority: 1 | ||
|
||
- ip_protocol: udp | ||
port_range: 10988/10989 | ||
source_cidr_ip: 47.89.23.33/32 | ||
priority: 2 | ||
|
||
group_outboundRules: | ||
- ip_protocol: tcp | ||
port_range: 80/80 | ||
dest_cidr_ip: 192.168.0.54/32 | ||
priority: 1 | ||
|
||
- ip_protocol: tcp | ||
port_range: 8080/8085 | ||
dest_cidr_ip: 47.89.23.33/32 | ||
priority: 1 | ||
|
||
- ip_protocol: udp | ||
port_range: 10989/10997 | ||
dest_cidr_ip: 47.89.23.33/32 | ||
priority: 2 | ||
|
||
|
||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
--- | ||
- 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: 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. Remove slb tags. | ||
ali_slb_tag: | ||
state: absent | ||
resource_type: 'instance' | ||
resource_ids: '{{ slbs.ids }}' | ||
tags: '{{ slb_tags }}' | ||
|
||
- 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. Remove instance tags. | ||
ali_ecs_tag: | ||
state: absent | ||
resource_type: 'instance' | ||
resource_ids: '{{ instances.ids }}' | ||
tags: '{{ instance_tags }}' | ||
|
||
- 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. Remove security group Tags. | ||
ali_ecs_tag: | ||
state: absent | ||
resource_type: 'securitygroup' | ||
resource_ids: '{{ sgs.ids }}' | ||
tags: '{{ security_group_tags }}' | ||
|
||
- 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. Remove vpc tags. | ||
ali_vpc_tag: | ||
state: absent | ||
resource_type: 'vpc' | ||
resource_ids: '{{ vpcs.ids }}' | ||
tags: '{{ vpc_tags }}' | ||
|
||
- name: Changed. Deleting vpcs | ||
ali_vpc: | ||
cidr_block: '{{ item.cidr_block }}' | ||
name: '{{ item.vpc_name }}' | ||
state: absent | ||
with_items: '{{vpcs.vpcs}}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
- 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 }}' | ||
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 }}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
- name: Create a VPC security group | ||
ali_security_group: | ||
name: '{{ security_group_name }}' | ||
description: '{{ group_description }}' | ||
vpc_id: '{{vpc.vpc.id}}' | ||
rules: '{{ group_inboundRules }}' | ||
rules_egress: '{{ group_outboundRules }}' | ||
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 }}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }}' |
Oops, something went wrong.