我脑子里有件事可能太复杂了。
我需要检查我的主机是否有ansible_virtualization_type == "openvz"
如果是这样的话,所有主机都应该执行特定的任务。
我现在试图设置一个事实(virt_list),其中包含一个主机列表及其本地主机上的virtualization_type:
- name: Set fuse on virtualization OpenVZ
set_fact:
virt_list:
host: "{{item}}"
type: "openvz"
when: hostvars[item].ansible_virtualization_type == "openvz"
with_items: "{{ groups['all'] }}"
delegate_to: localhost
delegate_facts: true但这是行不通的(本剧中的两位主持人都是在openvz上):
TASK [roles/testvirt : debug vars ansible_virtualization_type ] ****************************
ok: [host1] => {
"ansible_virtualization_type": "openvz"
}
ok: [host2] => {
"ansible_virtualization_type": "openvz"
}
TASK [roles/testvirt : debug vars virt_list ] **********************************************
ok: [host1] => {
"msg": [
{
"host": "host1",
"type": "openvz"
}
]
}
ok: [host2] => {
"msg": [
{
"host": "host2",
"type": "openvz"
}
]
}应该有一种更简单的方法,也许可以使用jinjia2直接组合列表。
有人有建议吗?
发布于 2021-08-17 11:58:52
问:“如果我的主机中有ansible_virtualization_type == "openvz”所有主机都应该执行特定的任务。“
例如,给出了测试的清单
shell> cat hosts
host1 ansible_virtualization_type=xen
host2 ansible_virtualization_type=xen
host3 ansible_virtualization_type=openvz提取变量
- debug:
msg: "{{ ansible_play_hosts|
map('extract', hostvars, 'ansible_virtualization_type')|
list }}"
run_once: true给出
msg:
- xen
- xen
- openvz测试类型是否存在
- debug:
msg: OK. ALL hosts should execute a specific task.
when: "'openvz' in vtypes"
vars:
vtypes: "{{ ansible_play_hosts|
map('extract', hostvars, 'ansible_virtualization_type')|
list }}"
run_once: true给出
msg: OK. ALL hosts should execute a specific task.如果此操作按预期进行,则对所有主机进行处理。
- set_fact:
all_hosts_execute_specific_task: true
when: "'openvz' in vtypes"
vars:
vtypes: "{{ ansible_play_hosts|
map('extract', hostvars, 'ansible_virtualization_type')|
list }}"
run_once: true
- debug:
msg: Execute a specific task.
when: all_hosts_execute_specific_task|default(false)给出
TASK [set_fact] ************************************************************
ok: [host1]
TASK [debug] ***************************************************************
ok: [host1] =>
msg: Execute a specific task.
ok: [host3] =>
msg: Execute a specific task.
ok: [host2] =>
msg: Execute a specific task.如果类型丢失,任务将被跳过。
https://stackoverflow.com/questions/68815821
复制相似问题