Well, if we use dynamic inventory, then I recommend using count_tags and exact_count with the ec2 module when creating instances with create. ut
--- - hosts: localhost connection: local gather_facts: false vars_files: { ./env.yml } tasks: - name: Provision a set of instances ec2: instance_type: "{{ item.value.instance_type }}" image: "{{ image }}" region: "{{ region }}" vpc_subnet_id: "{{ item.value.vpc_subnet_id }}" tenancy: "{{ tenancy }}" group_id: "{{ group_id }}" key_name: "{{ key_name }}" wait: true instance_tags: Name: "{{ env_id }}" Type: "{{ item.key }}" count_tag: Type: "{{ item.key }}" exact_count: "{{ item.value.count }}" with_dict: "{{ servers }}" register: ec2
The env.yml file contains all of these variables and the server dictionary:
--- env_id: JaxDemo key_name: JaxMagicKeyPair image: "ami-xxxxxxxx" region: us-east-1 group_id: "sg-xxxxxxxx,sg-yyyyyyyy,sg-zzzzzzzz" tenancy: dedicated servers: app: count: 2 vpc_subnet_id: subnet-xxxxxxxx instance_type: m3.medium httpd: count: 1 vpc_subnet_id: subnet-yyyyyyyy instance_type: m3.medium oracle: count: 1 vpc_subnet_id: subnet-zzzzzzzz instance_type: m4.4xlarge
Now, if you want to change the number of servers, just change the score in the server dictionary. If you want to delete all of them, we all count to 0.
Or if you want, copy the create.yml file to delete_all.yml and replace
exact_count: "{{ item.value.count }}"
with
exact_count: 0
source share