在 ansible 剧本中使用主机变量
Use host variables in ansible playbook
我正在尝试结合 "with_items" 在剧本中引用主机变量。
我的库存
[container]
testcontainer-01.example.org template_name="syslog" ipv4="192.168.1.101"
testcontainer-02.example.org template_name="syslog" ipv4="192.168.1.102"
剧本:
tasks:
- debug:
var: "{{ item.ipv4 }}"
with_items:
- "{{ groups['container'] }}"
每当我 运行 播放时,我都会收到以下错误:
The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'ipv4'
当我只询问 {{ item }}
调试时,没有 ipv4 属性,它只是说变量未定义。
"testcontainer-01.example.org ": "VARIABLE IS NOT DEFINED!: Unable to look up a name or access an attribute in template string ({{testcontainer-01.example.org}}).\nMake sure your variable name does not contain invalid characters like '-': unsupported operand type(s) for -: 'StrictUndefined' and 'StrictUndefined'"
如果要ipv4主机运行玩用
- debug:
var: ipv4
如果你想列出容器中的所有ipv4使用
- debug:
msg: "{{ hostvars[item].ipv4 }}"
loop: "{{ groups['container'] }}"
(未测试)
如 Vladimer Botka 所发布,以下作品。
- debug:
var: "{{ hostvars[item].ipv4 }}"
with_inventory_hostnames:
- container
出于某种原因,Ansible 仍然对我咆哮说我的变量未定义。 None 产生我需要的结果越少。在正确的地方使用了 ipv4 变量的值。
ok: [containerhost.example.org] => (item=testcontainer-02.example.org) => {
"192.168.1.102": "VARIABLE IS NOT DEFINED!",
"item": "testcontainer-02.example.org"
}
ok: [containerhost.example.org] => (item=testcontainer-01.example.org) => {
"192.168.1.101": "VARIABLE IS NOT DEFINED!",
"item": "testcontainer-01.example.org"
}
我正在尝试结合 "with_items" 在剧本中引用主机变量。
我的库存
[container]
testcontainer-01.example.org template_name="syslog" ipv4="192.168.1.101"
testcontainer-02.example.org template_name="syslog" ipv4="192.168.1.102"
剧本:
tasks:
- debug:
var: "{{ item.ipv4 }}"
with_items:
- "{{ groups['container'] }}"
每当我 运行 播放时,我都会收到以下错误:
The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'ipv4'
当我只询问 {{ item }}
调试时,没有 ipv4 属性,它只是说变量未定义。
"testcontainer-01.example.org ": "VARIABLE IS NOT DEFINED!: Unable to look up a name or access an attribute in template string ({{testcontainer-01.example.org}}).\nMake sure your variable name does not contain invalid characters like '-': unsupported operand type(s) for -: 'StrictUndefined' and 'StrictUndefined'"
如果要ipv4主机运行玩用
- debug:
var: ipv4
如果你想列出容器中的所有ipv4使用
- debug:
msg: "{{ hostvars[item].ipv4 }}"
loop: "{{ groups['container'] }}"
(未测试)
如 Vladimer Botka 所发布,以下作品。
- debug:
var: "{{ hostvars[item].ipv4 }}"
with_inventory_hostnames:
- container
出于某种原因,Ansible 仍然对我咆哮说我的变量未定义。 None 产生我需要的结果越少。在正确的地方使用了 ipv4 变量的值。
ok: [containerhost.example.org] => (item=testcontainer-02.example.org) => {
"192.168.1.102": "VARIABLE IS NOT DEFINED!",
"item": "testcontainer-02.example.org"
}
ok: [containerhost.example.org] => (item=testcontainer-01.example.org) => {
"192.168.1.101": "VARIABLE IS NOT DEFINED!",
"item": "testcontainer-01.example.org"
}