I am going nuts over what seems to be a very simple thing.
I am using ec2.py. I want a list of the EC2 instance IDs, but only of instances that are tagged with a particular name and value, e.g. MyTag:MyValue.
I know that {{ hostvars.XXX.groups.tag_MyTag_MyValue }} exists for any host tagged with MyTag:MyValue.
I also know that {{hostvars.XXX.ec2_id}} will get me the ID of the instance named XXX.
I can get a list of ALL the instance IDs like this:
-
set_fact:
ids: -
set_fact:
ids: “{{ ids }} + [ ‘{{ hostvars[item].ec2_id }}’ ]”
with_items: “{{ hostvars |list }}”
I feel I should be able to add a when: clause after the with_items: clause, something like:
when: item in hostvars.item.groups.tag_MyTag_MyValue
… but that causes an error saying that hostvars has no attribute called “item”. If I try to dereference it (“{{ item }}”) Jinja tells me I can’t use curly braces in when: statements.
Any clues?
Regards, K.
PS: ec2.py directly returns what I am after - it returns the tag groups outside the hostvars. Is there some way I can access that from inside Ansible? Those groups seem only to be accessible via the hosts: directive, which is no use to me as I need this list inside a role.