I’m using Ansible to provision Tomcat, Memcached, Postgres.
With static inventory, I’m able to do this
hosts
[dbservers]
db.local
[appservers]
app1.local
app2.local
app/tasks/setup_tomcat_app.yml
- name: Setup SB ROOT context template: src=root.xml.j2 dest=/var/lib/tomcat7/conf/Catalina/localhost/ROOT.xml notify: - Restart Tomcat tags: - sb_tomcat
app/templates/root.xml.j2
{% set dbserver = hostvars[groups['dbservers'][0]]['ansible_eth1']['ipv4']['address'] %} <Resource name="jdbc/postgres/configuration" auth="Container" factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" type="javax.sql.DataSource" driverClassName="org.postgresql.Driver" url="jdbc:postgresql://{{dbserver}}:5432/configuration" ....
I’m able to run this task in appservers and retrieve dbserver’s information.
But when using dynamic inventory (EC2), things become harder. How to simulate the following?
{% set dbserver = hostvars[groups['dbservers'][0]]['ansible_eth1']['ipv4']['address'] %}
Answer: Google
but it takes time
Search Ansible Group: https://groups.google.com/forum/#!searchin/ansible-project/dynamic$20inventory
Lot of results return ( Looks like dyamic inventory is not transparent enough with Ansible).
This link http://goo.gl/eKJfxk is similar but doesn’t help much.
Then I found https://coderwall.com/p/13lh6w/dump-all-variables.
Lookup all variables provided by Ansible, the following line works
{% set dbserver = hostvars[groups['security_group_sb-devtest-db'][0]]['ansible_default_ipv4']['address'] %}
All of this could be simple or documented somewhere but I do hope Ansible newbie can find it useful.
I’d appreciate your comments.