Answers:
这是另一种方法:
- name: my command
command: echo stuff
when: "'groupname' not in group_names"
group_names
是此处记录的魔术变量:https : //docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#accessing-information-about-other-hosts-with-magic-variables:
group_names是当前主机所在的所有组的列表(数组)。
when: inventory_hostname not in groups.certain_groups
inventory_hostname in groups['groupname']
因为在浆液本身不存在的情况下更健壮,所以Ansible会抛出类似“确保变量名不包含无效字符(如'-')的错误:'StrictUndefined'类型的参数不可迭代”的错误
您可以在位于group_vars/
主机文件中或直接位于主机文件中的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
This one looks easy to fix. It seems that there is a value started with a quote, and the YAML parser is expecting to see the line ended with the same kind of quote.