【发布时间】:2021-10-04 09:21:04
【问题描述】:
我正在尝试从一个组中的所有主机获取ansible_fqdn,模板中有类似这样的内容
{% for host in groups.all %}
{{ hostvars[host].ansible_fqdn }}
{% endfor %}
或使用:
{% for host in query('inventory_hostnames', target) | map('extract', hostvars, 'inventory_hostname') %}
{{ hostvars[host].ansible_fqdn }}
{% endfor %}
在所有情况下我都得到:
"AnsibleUndefinedVariable: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_fqdn'"
我确实注意到,如果我只是“打印/写入”:
{{ hostvars[host] }}
ansible_fqdn 在facter_networking 之内,但也在尝试:
{{ hostvars[host][facter_networking].fqdn }}
或
{{ hostvars[host][facter_networking].fqdn }}
我明白了:
fatal: [10.25.176.10]: FAILED! => {
"changed": false,
"msg": "AnsibleUndefinedVariable: ansible.vars.hostvars.HostVarsVars object has no element {
'domain': 'test-x',
'fqdn': 'TEST-X.example.org',
'hostname': 'TEST-X',
'interfaces': {
'ens192': {
'bindings': [{
'address': '10.25.176.10',
'netmask': '255.255.254.0',
'network': '10.25.176.0'
}, {
'address': '10.25.177.226',
'netmask': '255.255.252.0',
'network': '10.25.176.0'
}],
'ip': '10.25.176.10',
'mac': '00:50:56:8f:ce:ca',
'mtu': 1500,
'netmask': '255.255.254.0',
'network': '10.25.176.0'
},
'lo': {
'bindings': [{
'address': '127.0.0.1',
'netmask': '255.0.0.0',
'network': '127.0.0.0'
}],
'ip': '127.0.0.1',
'mtu': 65536,
'netmask': '255.0.0.0',
'network': '127.0.0.0'
}
},
'ip': '10.25.176.10',
'mac': '00:50:56:8f:ce:ca',
'mtu': 1500,
'netmask': '255.255.254.0',
'network': '10.25.176.0',
'primary': 'ens192'
}
}
如果我使用:
{{ hostvars[host].facter_networking }}
我明白了:
"msg": "AnsibleUndefinedVariable: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'facter_networking'
数据在那里,但我知道如何提取它吗?
这是剧本:
- name: Gathering info
action: setup
- name: Create .pgpass
template:
src: pgpass.j2
dest: /home/postgres/.pgpass
owner: postgres
group: postgres
mode: 0600
【问题讨论】:
-
澄清一下,您是否正在为库存的“所有”主机收集事实?