只有当前剧本的主持人不属于某个组时,我才能运行一个ansible任务.在半伪代码中:
- name: my command
command: echo stuff
when: "if {{ ansible_hostname }} not in {{ ansible_current_groups }}"
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
小智 160
这是另一种方法:
- name: my command
command: echo stuff
when: "'groupname' not in group_names"
Run Code Online (Sandbox Code Playgroud)
group_names是一个神奇的变量,如下所示:http://docs.ansible.com/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts:
group_names是当前主机所在的所有组的列表(数组).
nva*_*mei 15
您可以在位于group_vars/hosts文件中或直接在hosts文件中的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
Run Code Online (Sandbox Code Playgroud)
并运行这样的任务:
- name: "test"
command: "echo {{test_var}}"
when: test_var is defined and test_var
Run Code Online (Sandbox Code Playgroud)
我想出了这个:
- name: my command
command: echo stuff
when: inventory_hostname not in groups.get('your-group', [])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
89125 次 |
| 最近记录: |