使用jinja2模板的无形主机

时间:2016-10-27 14:49:31

标签: ansible jinja2

我遇到了使用ansible hostvars的问题。我想通过使用jinja2和库存中的组获取​​接口的ipv4地址。

我使用的代码:

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

但是ansible说:

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

brocker-01有这个界面:

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

我尝试了什么:

如果

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

获取

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': .....

其他变种来自......我完全不知道。

如果

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

获取

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'....

我需要的所有主持人 这项工作:

如果

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

获取

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

哪里可能是错误的?

ansible 2.1.1.0

UPD 我忘了。在剧本中首先收集来自druid_broker的事实

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

- name: config haproxy on backends
  hosts: bakend_servers
  roles:
     - haproxy

1 个答案:

答案 0 :(得分:0)

昨天我将ansible更新为v2.2.0.0,这解决了这个问题。