aws ec2 describe-instances --query 'Reservations[*].Instances[*].PrivateIpAddress' --output text---- name: Terminate EC2 instances with name Test hosts: 127.0.0.1 connection: local vars: tasks: - action: ec2_facts - debug: msg="{{ hostvars[item]['ec2_id'] }}" with_items: groups['tag_Name_Cassandra'] - name: Terminate instances local_action: module: ec2 region: "{{region}}" keypair: "{{key_name}}" instance_ids: hostvars[{{ item }} ]['ec2_id'] with_items: groups['tag_Name_Test'] state: 'stopped' wait: yes
- name: Give everyone a minute pause: minutes=1 instance_ids: hostvars[{{ item }} ]['ec2_id'] with_items: groups['tag_Name_Test']'item' is undefined
Hi Tzach
I usually approach instance termination by matching the instances with ec2 dynamic inventory, then add them to a group and terminate them in another play targeted at the newly created group, based on their instance id (which is automatically added as an inventory variable by the ec2 inventory script).
Note that I specify gather_facts: false and do not reference the hostvars dictionary. That way, Ansible does not attempt or need to reach the instances via ssh before it is able to terminate them.
- name: Find sandbox instance(s) with matching name
hosts: tag_Name_Test
gather_facts: false
tasks:
- name: Create group
group_by: key=legacy
- hosts: legacy
connection: local
gather_facts: false
tasks:
- name: Terminate instance(s)
ec2:
instance_ids: "{{ ec2_id }}"
state: absent
Hope this helps.
Tom
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/6c9e08aa-43d7-4329-a91c-a4a0b4f3f0e1%40googlegroups.com.
Hi Tzach
I usually approach instance termination by matching the instances with ec2 dynamic inventory, then add them to a group and terminate them in another play targeted at the newly created group, based on their instance id (which is automatically added as an inventory variable by the ec2 inventory script).
Note that I specify
gather_facts: falseand do not reference thehostvarsdictionary. That way, Ansible does not attempt or need to reach the instances via ssh before it is able to terminate them.- name: Find sandbox instance(s) with matching name hosts: tag_Name_Test gather_facts: false tasks: - name: Create group group_by: key=legacy
- hosts: legacy
connection: local
gather_facts: false tasks: - name: Terminate instance(s) ec2: instance_ids: "{{ ec2_id }}"
state: absentHope this helps.