题
我希望只有当前剧本的主机才能运行 ansible 任务 不属于 到某个群体。在半伪代码中:
- 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
是一个魔法变量,如这里记录在这里: http://docs.ansible.com/playbooks_variables.html#magic-variables-and-how-to-access-information-about-otht-hosts :
group_names是当前主机所在的所有组的列表(数组)。
其他提示
您可以在位于以下位置的 vars 文件中设置控制变量 group_vars/
或者直接在主机文件中,如下所示:
[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
不隶属于 StackOverflow