Refreshing EC2 inventory between playbooks

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

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

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.

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.

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.

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