0
votes

I faced problem of using ansible hostvars. I want by using jinja2 and groups in inventory get ipv4 address of interface.

Code that I use:

{% for srv in groups.druid_broker %}
erver {{ srv }} {{ hostvars[srv]['ansible_priv0']['ipv4']['address'] }}:8082 check
{% endfor %}

But ansible said:

ansible-playbook haproxy.yml -vvvv -l devbackend
......................
..."msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'ansible_priv0'"}

brocker-01 have this interface:

ip a
priv0@if14: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
inet 192.168.1.109/23 brd 192.168.1.255 scope global priv0

What I try:

if

{% for srv in groups.druid_broker %}
server {{ srv }} {{ hostvars[srv] }}:8082 check
{% endfor %}

get

ansible-playbook haproxy.yml -vvvv -l devbackend
............
server brocker-01 {'inventory_hostname': u'brocker-01', u'iface': u'eth0', u'cassandra_ports': [7000, 7001, 7199, 9160, 61620, 61621], 'inventory_hostname_short': u'brocker-01', u'backend_ports': [3306, 11211, 27017, 27018, 27030], 'playbook_dir': .....

And other var from... I don't know exactly from.


if

server {{ inventory_hostname }} {{ hostvars[inventory_hostname] }}:8082 check

get

ansible-playbook haproxy.yml -vvvv -l devbackend
...............
server devbackend {u'module_setup': True, u'ansible_distribution_version': u'14.04', u'ansible_env': {u'LANG': u'C', u'TERM': u'xterm', u'SHELL': u'/bin/bash', u'LC_MESSAGES': u'C', u'SHLVL': u'1', u'SSH_TTY': u'/dev/pts/6', u'SSH_CLIENT'....

All hostvars that I need And this work:

if

server {{ inventory_hostname }} {{ hostvars[inventory_hostname]['ansible_priv0']['ipv4']['address'] }}:8082 check

get

ansible-playbook haproxy.yml -vvvv -l devbackend
..........
server devbackend 192.168.1.111:8082 check

where can be mistake?

ansible 2.1.1.0

UPD I forgot. in playbook first of all gathering facts from druid_broker

---
- name: get facts from druid_broker
  hosts: druid_broker
  tasks: [ ]

- name: config haproxy on backends
  hosts: bakend_servers
  roles:
     - haproxy
1
have those hosts in groups.druid_broker have been queried for facts in the current playbook run ? - user2599522

1 Answers

0
votes

Yesterday I update ansible to v2.2.0.0 and this solve the problem.