No, I tried to explain this in a previous response.

add_host is what is called a "bypass host loop" plugin.  This means that
add_host will only execute for the first host targeted by your `hosts`
specification.  Your `hosts` targets a group containing the following hosts:

aaa-frs-ans-test1
aaa-frs-ans-test2
aaa-frs-ans-test3

As such, add_host only runs for aaa-frs-ans-test1.

The problem is with how you have structured your tasks.  You have
instructed ansible to build a new server, for each one of these servers.
The results of your `ec2` variable that you register, get stored per host
(aaa-frs-ans-test1, aaa-frs-ans-test2, aaa-frs-ans-test3).  This means that
`ec2.instances` only hold the results of a single host build, per host.

Most people only target localhost, instead of a group that contains
multiple hosts.

And then they build servers with the ec2 module using exact_count, or
with_items, then all of the results for building all new instances, are
stored with 1 inventory host.

So again, you are building 3 new instances, using 3 old instances, so
effectively you end up with an `ec2` variable stored for each host
(aaa-frs-ans-test1, aaa-frs-ans-test2, aaa-frs-ans-test3).

However add_host only runs on the first host (bypass host loop plugin), and
thus only has access to the `ec2` var stored for aaa-frs-ans-test1.

Another solution would be to do something like:


- name: Add hosts group temporary inventory group
   add_host: name={{ hostvars[item]['ec2'].instances.0.private_ip }}
groups=dynamic
   with_items: "{{ play_hosts }}"


However, that assumes you always have only created 1 instance via the ec2
task per host in the play.

Another option, and this is completely untested, is to use the `extract`
filter, something like:


- name: Add hosts group temporary inventory group
   add_host: name={{ item.private_ip }} groups=dynamic
   with_items: "{{ play_hosts|map('extract', hostvars, ['ec2',
'instances'])|list }}"




On Fri, May 27, 2016 at 11:42 AM, Eric S <[email protected]> wrote:

> is it an issue with the add_host module?  I even tried upgrading to 2.1
> and still am experiencing the same thing.  Can the add_host module only add
> one host at a time?
>
> --
> 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/520458e7-6b97-40ae-8121-345981f91b1e%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/520458e7-6b97-40ae-8121-345981f91b1e%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Matt Martz
@sivel
sivel.net

-- 
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/CAD8N0v_grjN5APaLOS_shNSER3%2BPFS6uqSJg2Bd9-Ld6A%3DpibA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to