我有一个变量,其中包含我网络上主机的详细信息(称为“主机列表” - 我相信你称之为字典,但我不确定这个术语。该变量在 group_vars/all 中的文件中定义,因此它在所有剧本中都可用(不确定这是否重要)。
我有一个剧本,只有当 ansible_hostname 未在 hostlist 中的主机名列表中找到时,我才会运行它。hostlist 中的主机名是变量的属性之一,但我再次不确定“属性”是否是正确的术语...
主机列表定义为:
hostlist:
- { name: 'host1', ip_addr: '192.168.2.31', hostgrp: 'physical_workstation' }
- { name: 'host2', ip_addr: '192.168.2.32', hostgrp: 'physical_workstation' }
- { name: 'host3', ip_addr: '192.168.2.33', hostgrp: 'virtual_machine' }
我尝试使用以下方法来让这个工作正常进行:
- name: Conditional test
debug:
msg: "ansible_hostname not found in hostlist."
when: ansible_hostname not in hostlist.name
我不确定条件中所需的语法,或者我想要的是否可以通过这种方式实现?
答案1
可能有更优雅的方法来实现它,但是对我来说像这样的方法有效:
如果你的库存文件如下所示
host1
host2
host3
host4
然后,将运行具有以下内容的剧本,host4
因为它与主机列表变量不匹配:
$ cat test.yml
- hosts: all
vars:
hostlist:
- { name: 'host1', ip_addr: '192.168.2.31', hostgrp: 'physical_workstation' }
- { name: 'host2', ip_addr: '192.168.2.32', hostgrp: 'physical_workstation' }
- { name: 'host3', ip_addr: '192.168.2.33', hostgrp: 'virtual_machine' }
tasks:
- name: Conditional test
debug:
msg: "ansible_hostname not found in hostlist."
when: hostlist|selectattr("name", "equalto", ansible_hostname)|list|length == 0
当这样调用时:
ansible-playbook test.yml
意味着只有 host4 运行任务块....
PLAY [all] *************************************************
TASK [Gathering Facts] *************************************
ok: [host1]
ok: [host2]
ok: [host3]
ok: [host4]
TASK [debug] ***********************************************
ok: [host4] => {
"msg": "hostname not in hostlist name list"
}
skipping: [host1]
skipping: [host2]
skipping: [host3]
答案2
或者,这种情况更清洁
when: inventory_hostname not in hostlist|map(attribute="name")|list
如果要与库存别名列表进行比较,请使用inventory_hostname
而不是。请参阅ansible_hostname
inventory_hostname 和 ansible_hostname 有什么区别。
答案3
另一种解决方案是(如问题评论中所建议的那样)将主机列表重构到库存中,如下所示:
[physical_workstation]
host1 ansible_host=192.168.2.31
host2 ansible_host=192.168.2.32
[virtual_machine]
host3 ansible_host=192.168.2.33
[all]
host4 ansible_host=192.168.2.34
ungrouped
然后,您可以简化剧本,使用特殊组(从不属于任何其他组的主机中选择主机)对all
不属于任何组的主机运行剧本:
- hosts: ungrouped
tasks:
- name: only ungrouped
debug:
msg: "host not found in any other group."
或者针对特定主机分组运行...
- hosts: physical_workstation:virtual_machine
tasks:
- name: only in specified groups
debug:
msg: |
This will run on machines that are in groups:
physical_workstation or virtual_machine
输出
PLAY [ungrouped] ***********************************************
TASK [only ungrouped] ******************************************
ok: [host4] => {
"msg": "host not found in any other group."
}
PLAY [physical_workstation:virtual_machine] ********************
TASK [only in specified groups] ********************************
ok: [host1] => {
"msg": "This will run on machines that are in groups:\nphysical_workstation or virtual_machine"
}
ok: [host2] => {
"msg": "This will run on machines that are in groups:\nphysical_workstation or virtual_machine"
}
ok: [host3] => {
"msg": "This will run on machines that are in groups:\nphysical_workstation or virtual_machine"
}