我只想在主机位于组*beat
时执行任务,如下所示。
when:
- (groups['*beat'] is defined and inventory_hostname in groups['*beat'])
这不起作用。该任务一直被跳过。
有人知道我如何才能达到预期的行为吗?
从另一个角度看待手头的问题可能会有所帮助:您可以列出主机所在的组,然后用变量group_names
和select
只列出与您的模式匹配的组,而不是试图查找您正在循环的当前主机是否真的在特定的组模式中
从那时起,如果列表为空,则表示您的主机不在符合该模式的组中。
给定库存:
all:
hosts:
localhost:
vars:
ansible_connection: local
children:
heartbeat:
hosts:
rate:
diastole:
systole:
filebeat:
hosts:
syslog:
file:
stdout:
fox:
hosts:
brown:
jump:
over:
lazy:
dog:
战术手册:
- hosts: all
gather_facts: no
tasks:
- debug:
msg: "I am in a group named *beat"
when:
- group_names | select('match', '^(.*)beat$') | list | length > 0
它给出输出:
PLAY [all] ********************************************************************************************************
TASK [debug] ******************************************************************************************************
skipping: [localhost]
ok: [rate] => {
"msg": "I am in a group named *beat"
}
ok: [diastole] => {
"msg": "I am in a group named *beat"
}
ok: [systole] => {
"msg": "I am in a group named *beat"
}
ok: [syslog] => {
"msg": "I am in a group named *beat"
}
ok: [file] => {
"msg": "I am in a group named *beat"
}
ok: [stdout] => {
"msg": "I am in a group named *beat"
}
skipping: [brown]
skipping: [jump]
skipping: [over]
skipping: [lazy]
skipping: [dog]
PLAY RECAP ********************************************************************************************************
brown : ok=0 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
diastole : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
dog : ok=0 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
file : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
jump : ok=0 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
lazy : ok=0 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
localhost : ok=0 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
over : ok=0 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
rate : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
stdout : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
syslog : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
systole : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0