Refer other host to get facts when running in a host

23 views
Skip to first unread message

Le Van

unread,
Feb 4, 2015, 5:36:39 AM2/4/15
to ansible...@googlegroups.com
Hi,

I'm using Ansible to provision an application deployment.

here is my structure

hosts
[dbservers]
db.local

[memcached]
mc.local

[appservers]
app1.local
#app2.local


I'm using Tomcat and have a context like this in appservers ( app1.local)
roles/tomcat/templates/context.j2

<Context path="" reloadable="false" docBase="/apps/deploy/current" workDir="work" >
 <Environment name="memcached" value="mc.local" type="java.lang.String" override="false"/>
 <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"

   <Resource   name="jdbc/postgres/configuration"
     
        url="jdbc:postgresql://db.local:5432/configuration"
   />
</Context>

My questions are:
+ How can I resolve mc.local ( db.local) to IP?
Doing this, I don't have to set  hosts in appservers like
/etc/hosts
192.168.. mc.local
....

+ In case of using dymamic inventory ( Ec2), how can I refer a db ( from db tags), resolve its ip and resolve the template.
+ Or is there another way to accomplish this?

I guess this model is trivial and should have a normal way out there ( but I'm still a Ansible newbie to get it)

Thanks,
Pilgrim


Le Van

unread,
Feb 4, 2015, 10:12:56 PM2/4/15
to ansible...@googlegroups.com
this seems a basic question and have a simple answer.

{% for host in groups['dbservers'] %}
   {{ hostvars[host]['ansible_eth1']['ipv4']['address'] }}
{% endfor %}

the interesting point is I normally run a specfic roles/tasks by tagging 
like this

ansible-playbook site.yml --limit=appservers --tags=sb_tomcat

note: since we limit appservers only then Ansible have no idea to get facts from other servers ( dbserver, memcache) in this case.

I have to do cheat like this

ansible-playbook site.yml --limit=dbservers, appservers --tags=sb_tomcat  ( add dbservers to gather facts)
but it's not enough, I have to add a fake task like this for dbservers with tag sb_tomcat

- name: Dont fool me
  command: echo "can you hear me"
  tags:
    - sb_tomcat   

Doing this to tell Ansible to gather dbserver facts and 
{% for host in groups['dbservers'] %}
   {{ hostvars[host]['ansible_eth1']['ipv4']['address'] }}
{% endfor %}

works.

Questions:
+ In real case, we really need a specfic tag. Do we have another way to gather fact explitcitly without modifying --limit or add fake tasks?

Regards,
Pilgrim
Reply all
Reply to author
Forward
0 new messages