A few things to cleanup first:
" - add_host: hostname={{ hostvars[inventory_hostname]['ec2_publicIp']
}} groupname=ec2hosts server_name={{
hostvars[inventory_hostname]['ec2_public_dns_name']
}}"
When you get ginormous variables like this, slow down, and define useful
shortcuts in your "vars" section, like this:
vars:
ec2ip: "{{ hostvars[inventory_hostname]['ec2_publicIp'] }}"
This allows cleaner playbooks like:
" - add_host: hostname={{ ec2ip }} groupname=ec2hosts server_name={{
ec2dns }}"
etc
That's unrelated though.
Also this is overkill:
when: hostvars[inventory_hostname]['ec2_region'] == 'ap-southeast-2'
Could have just been:
when: "ec2_region == 'ap-southeast-2'"
Ansible likes it simple.
On Thu, May 8, 2014 at 2:32 AM, Igor Cicimov <[email protected]
> wrote:
> Hi all,
>
> I have the following playbook trying to create a subgroup out of my ec2
> dynamic inventory with instances matching given tags:
>
> ---
> - hosts: ec2
> gather_facts: False
> connection: local
> tasks:
> - add_host: hostname={{ hostvars[inventory_hostname]['ec2_publicIp']
> }} groupname=ec2hosts server_name={{
> hostvars[inventory_hostname]['ec2_public_dns_name'] }}
> when: hostvars[inventory_hostname]['ec2_region'] ==
> 'ap-southeast-2'
> and hostvars[inventory_hostname]['ec2_tag_Role'] ~
> 'application'
> and hostvars[inventory_hostname]['ec2_tag_Type'] == 'tomcat'
>
> - hosts: ec2hosts
> gather_facts: True
> remote_user: user1
> sudo: True
> tasks:
> # fetch instance data from the metadata servers in ec2
> - ec2_facts:
>
> # show all known facts for this host
> - debug: var=hostvars[inventory_hostname]
>
> # just show the instance-id
> - debug: msg="{{
> hostvars[inventory_hostname]['ansible_ec2_instance-id'] }}"
>
>
> This is the result of its run:
>
> PLAY [ec2]
> ********************************************************************
>
> TASK: [add_host hostname={{hostvars[inventory_hostname]['ec2_publicIp']}}
> groupname=ec2hosts
> server_name={{hostvars[inventory_hostname]['ec2_public_dns_name']}}] ***
> skipping: [54.72.64.112]
>
> PLAY [ec2hosts]
> ***************************************************************
> skipping: no hosts matched
>
>
> The instances are being picked up correctly, I confirmed that with debug
> output, but looks like the group is not being populated correctly. I'm
> probably missing something here since looks like the supposed loop is not
> happening.
>
> Can anyone spot what am I doing wrong here?
>
> Thanks,
> Igor
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/dd9eb356-287a-49ed-81cc-465623a4aa77%40googlegroups.com<https://groups.google.com/d/msgid/ansible-project/dd9eb356-287a-49ed-81cc-465623a4aa77%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/CA%2BnsWgxa79Okth0B%2BKNZnnOJh8PN%2B3eoBNCP9KL9M1VmhW1FJA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.