仅当主机不属于组时才运行任务

时间:2014-01-08 22:38:19

标签: ansible

我希望只有当前剧本的主持人不属于某个群组才能运行安赛任务。在半伪代码中:

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

我该怎么做?

2 个答案:

答案 0 :(得分:147)

这是另一种方法:

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

group_names是一个神奇变量,如下所示:http://docs.ansible.com/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts

  

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

答案 1 :(得分:14)

您可以在位于group_vars/的vars文件中或直接在hosts文件中设置控制变量,如下所示:

[vagrant:vars]
test_var=true

[location-1]
192.168.33.10 hostname=apollo

[location-2]
192.168.33.20 hostname=zeus

[vagrant:children]
location-1
location-2

运行这样的任务:

- name: "test"
  command: "echo {{test_var}}"
  when: test_var is defined and test_var
相关问题