问题描述
我的清单主机如下:
# inventory
[kafka]
192.168.1.1
192.168.1.2
[mysql]
192.168.1.3
我的ansible-playbook如下:
My ansible-playbook as follows:
site.yml:
- name: test
hosts: all
roles:
- kafka
kafka角色任务:
kafka roles tasks:
# main.yml
- name: get kafka groups length
shell: echo "{{ groups['kafka']|length }}"
run_once: true
delegate_to: localhost
when: "'kafka' in group_names"
预期结果
get kafka groups length
可以分别执行一次,并只能委派给本地执行一次
get kafka groups length
can be executed and delegated to local execution respectively and only once
实际结果
TASK [Gathering Facts] ******************************************************************************************************************************************************
ok: [192.168.1.1]
ok: [192.168.1.2]
ok: [192.168.1.3]
TASK [kafka : get mongodb groups length] ************************************************************************************************************************************
skipping: [192.168.1.3]
非常奇怪的设计,我认为不应跳过,但他确实跳过了,我该怎么办?我希望得到同样的结果
Very strange design, I think it should not be skipped, but he did skip it, what should I do? I expect the same result
推荐答案
来自Ansible文档:
From the Ansible documentation:
您只运行一次任务,因此它仅在第一台主机上运行,并且该主机仅属于kafka
组.这意味着您的group_names
变量仅包含kafka
.
You are only running your task once, so it only runs on the first host, and that host only belongs to the kafka
group. This means that your group_names
variable on that run only includes kafka
.
尝试以下方法:
- name: get kafka groups length
shell: echo "{{ groups['kafka']|length }}"
run_once: true
delegate_to: localhost
when: groups['kafka'] is defined
这篇关于Ansible的when条件结合run_once会意外跳过一些不应跳过的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!