检索jinja模板中的主机信息

时间:2016-05-23 17:48:06

标签: ansible jinja2

我的ansible主机文件如下所示

[deployment]
server1 ansible_ssh_host=x.x.x.x ansible_ssh_user=root ansible_ssh_pass=abcdefg ansible_br_mgmt_ip=192.10.2.5 ansible_br_vlan_ip=192.10.4.5 ansible_br_vxlan_ip=192.10.8.5
server2 ansible_ssh_host=y.y.y.y ansible_ssh_user=root ansible_ssh_pass=abcdefg ansible_br_mgmt_ip=192.10.2.6 ansible_br_vlan_ip=192.10.4.7 ansible_br_vxlan_ip=192.10.8.8

内部jinja模板(.j2文件)我试图检索服务器1或服务器2的ansible_br_mgmt的值

我尝试了以下方法,但无法获得确切的IP地址。

hostname: "{%  {{  hostvars['deployment'].ansible_br_mgmt_ip }} %}"

hostvars.deployment.ansible_br_mgmt_ip
groups['deployment'][ansible_br_mgmt_ip]

你能帮忙吗?

1 个答案:

答案 0 :(得分:0)

- debug: msg={{ hostvars[groups['deployment'][0]].ansible_br_mgmt_ip }}

输出:

TASK: [debug msg={{ hostvars[groups['deployment'][0]].ansible_br_mgmt_ip }}] ***
ok: [127.0.0.1] => {
    "msg": "192.10.2.5"
}