仅当主机不属于组时才运行任务 [英] Run task only if host does not belong to a group

查看:59
本文介绍了仅当主机不属于组时才运行任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅当当前剧本的主机不属于某个组时,我才希望能够执行ansible任务.用半伪代码:

I'd like to able to run an ansible task only if the host of the current playbook does not belong to a certain group. In semi pseudo code:

- name: my command
  command: echo stuff
  when: "if {{ ansible_hostname }} not in {{ ansible_current_groups }}"

我应该怎么做?

推荐答案

这是另一种方法:

- name: my command
  command: echo stuff
  when: "'groupname' not in group_names"

group_names是一个神奇的变量,如此处所述:

group_names is a magic variable as documented here: https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#accessing-information-about-other-hosts-with-magic-variables :

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

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

这篇关于仅当主机不属于组时才运行任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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