[英]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 }}
debug時,沒有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仍在咆哮着說我的變量未定義。 不管怎樣它會產生我需要的結果。 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"
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.