Ansible的when条件结合run_once会意外跳过一些不应跳过的任务 [英] Ansible's when condition combined with run_once will accidentally skip some tasks that should not be skipped

查看:242
本文介绍了Ansible的when条件结合run_once会意外跳过一些不应跳过的任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的清单主机如下:

# 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:

group_names是当前主机所在的所有组的列表(数组)

group_names is a list (array) of all the groups the current host is in

您只运行一次任务,因此它仅在第一台主机上运行,​​并且该主机仅属于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会意外跳过一些不应跳过的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆