假设我在我的group_vars中定义了这一点:
ucarp_data:
- vhid: 6
interface: eth0
hosts:
- 10.1.60.4
- 10.1.60.5
options: --shutdown --preempt
- vhid: 9
interface: eth0
hosts:
- 10.1.60.7
- 10.1.60.8
options: --shutdown --preempt我想检查我当前正在运行的主机是否在这个hosts:数组中。
类似于:
- name: Check if ucarp_data contains this host
assert:
that: ansible_host in ucarp_data.hosts但这会返回fatal: [test_machine]: FAILED! => {"msg": "The conditional check 'ansible_host in ucarp_data.hosts' failed. The error was: error while evaluating conditional (ansible_host in ucarp_data.hosts): 'dict object' has no attribute 'hosts'"
有什么想法吗?
而且我一直用Ansible和嵌套的yaml变量来阻塞这些路障..。我似乎无法像在木星笔记本中那样动态地探索,任何Python对象都有选项卡完成,这对此有很大帮助吗?
发布于 2020-11-23 08:47:20
例如,给定列表ucarp_data,下面的剧本
- hosts: 10.1.60.5,10.1.60.7,10.1.60.9
tasks:
- assert:
that: inventory_hostname in ucarp_data|map(attribute='hosts')|flatten
fail_msg: "[ERR] {{ inventory_hostname }} not in ucarp_data"给予(删节)
ok: [10.1.60.5] => changed=false
msg: All assertions passed
ok: [10.1.60.7] => changed=false
msg: All assertions passed
fatal: [10.1.60.9]: FAILED! => changed=false
assertion: inventory_hostname in ucarp_data|map(attribute='hosts')|flatten
evaluated_to: false
msg: '[ERR] 10.1.60.9 not in ucarp_data'https://stackoverflow.com/questions/64963200
复制相似问题