Refreshing EC2 inventory between playbooks

401 views
Skip to first unread message

Tzach Livyatan

unread,
Jan 29, 2015, 7:33:33 AM1/29/15
to ansible...@googlegroups.com
Hi all
I'm using ec2 module to launch EC2 instances in one play book, and "-i ec2.py" to query a subset of the instances in a second playbook.
So far so good.

The problem I have is when I started to call both play books, using include, from one "super" playbook.
AFAIU, the inventory did not refresh, so the second one failed.
I can bypass the problem by running both playbooks from a bash script with
./ec2.py --refresh-cache > /dev/null
between them, but it is ugly and cause other problems.

Any Idiomatic alternative?

Thanks
Tzach

Giovanni Tirloni

unread,
Jan 29, 2015, 8:01:45 AM1/29/15
to ansible...@googlegroups.com



On Thu, 29 Jan 2015 04:33 -0800, Tzach Livyatan
<tzach.l...@gmail.com> wrote:
> Hi all
> I'm using ec2 module to launch EC2 instances in one play book, and "-i ec2.py" to query a subset of the instances in a second playbook.
> So far so good.
>
> The problem I have is when I started to call both play books, using include, from one "super" playbook.
> AFAIU, the inventory did not refresh, so the second one failed.

Try dynamically including new machines to groups with add_host:

http://docs.ansible.com/add_host_module.html
http://docs.ansible.com/ec2_module.html (see examples at bottom)

Giovanni

Tzach Livyatan

unread,
Jan 29, 2015, 8:53:22 AM1/29/15
to ansible...@googlegroups.com
Hi Giovanni
Thanks for the prompt response.
If I'm reading the doc correctly, the new group can only be used in the same playbook. 
Is it so?
I'm using more than one playbook, including all of the from one "super" playbook.

 



--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/1x5hFy4dzcQ/unsubscribe.
To unsubscribe from this group and all its topics, 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/1422536489.564465.220466761.4A784836%40webmail.messagingengine.com.
For more options, visit https://groups.google.com/d/optout.



--
Regards 
@TzachL

Brian Coca

unread,
Jan 29, 2015, 10:14:37 AM1/29/15
to ansible...@googlegroups.com
add_host affects the 'current run', all subsequent plays will have
access, the distribtion of the plays in different files does not
matter.

ansible reads all the files passed to it (directly or through
includes), compiles the plays and then runs them in order, at this
point the files they came from are irrelevant.

--
Brian Coca

Tzach Livyatan

unread,
Jan 31, 2015, 10:23:54 AM1/31/15
to ansible...@googlegroups.com
Thanks Brain, Giovanni
I'm confused on when to use ec2.py dynamic inventory and when creating group as you suggested.
If I understand correctly, the first make sense if I want to query info set by others, while the second make more sense if the servers live and die in the scope of my playbook (my use case).

I'm currently using hostvars from ec2.py to get the internal ips of servers:

    - command: echo "{{ hostvars[item].ec2_private_ip_address }}"
      with_items: groups.tag_Name_Cassandra
      register: output
      changed_when: no

Can I get the internal IP without ec2.py?
If I can not, refreshing the inventory between plays is mandatory - going back to my original question.



 

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/1x5hFy4dzcQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Regards 
@TzachL

Tzach Livyatan

unread,
Feb 1, 2015, 9:44:58 AM2/1/15
to ansible...@googlegroups.com
Found a solution, let me answer myself if anyone is interested

I got a list the internal IPs by creating a new group, as follow:

    - name: Add instances to private ip group
      local_action: add_host name={{item.private_ip}} groupname=CassandraPrivate_{{load_name}}
      with_items: ec2.instances

And using it from a different playbook as follow:

    - command: echo "{{ item }}"
      with_items: groups.CassandraPrivate_{{load_name}}
      register: outputPrivate
      changed_when: no

    - set_fact: ip_list="{{ outputPrivate.results|map(attribute='stdout')|join(',') }}"

Thanks again



--
Regards 
@TzachL
Reply all
Reply to author
Forward
0 new messages