Thanks Michael your suggestions noted. I did some more testing and looks 
like only one single instance, assuming the last instance, from the 
inventory is being added to the group, and this is without any conditions. 
Very simple playbook example:

---
- hosts: ec2 
  gather_facts: False
  connection: local
  vars:
    ec2ip: "{{ hostvars[inventory_hostname]['ec2_publicIp'] }}"
    ec2dns: "{{ hostvars[inventory_hostname]['ec2_public_dns_name'] }}"
  tasks:
     - add_host: "hostname={{ ec2ip }} groupname=ec2hosts server_name={{ 
ec2dns }}"

and I get only one single host in the ec2hosts group:

PLAY [ec2] 
******************************************************************** 

TASK: [add_host hostname={{hostvars[inventory_hostname]['ec2_publicIp']}} 
groupname=ec2hosts 
server_name={{hostvars[inventory_hostname]['ec2_public_dns_name']}}] *** 
ok: [instance_public_ip_obscured] => {"new_groups": ["ec2hosts"], 
"new_host": "instance_public_ip_obscured"}

Looks like this is not working the way I assume. From what I can see I have 
52 instances in the current ec2 inventory cache:

$ cat ~/.ansible/tmp/ansible-ec2.cache | grep region | sort | uniq -c
     47         "ec2_region": "ap-southeast-2", 
      5         "ec2_region": "eu-west-1",

Thanks,
Igor

On Saturday, May 10, 2014 9:56:37 AM UTC+10, Michael DeHaan wrote:
>
> 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 <
> ig...@encompasscorporation.com <javascript:>> 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 ansible-proje...@googlegroups.com <javascript:>.
>> To post to this group, send email to ansible...@googlegroups.com<javascript:>
>> .
>> 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 ansible-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/ad27e7fb-921e-4962-8cf6-67fef89f8b76%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to