我传递变量'host'来仅在一个环境的主机上做一些工作:
- name: Perform an action on a host
hosts: "{{ host }}"
roles:
- role: mule_action
当主机位于清单中 not 时,它会产生[警告]:无法匹配提供的主机模式,忽略:MuleQ01
我需要使它失败,以便它在jenkins(我的Ansible协调器)中显示为失败的构建。
我的ansible.cfg如下:
[defaults]
strategy_plugins = /usr/lib/python3.6/site-packages/ansible_mitogen/plugins/strategy
strategy = mitogen_linear
host_key_checking = False
roles_path = roles
unparsed_is_failed = True
host_pattern_mismatch = error
inventory = inventory
运行: ansible 2.9.7 配置文件= /data/ansible.cfg 配置的模块搜索路径= ['/home/mule/.ansible/plugins/modules',/usr/share/ansible/plugins/modules'] ansible python模块位置= /usr/lib/python3.6/site-packages/ansible 可执行位置= / usr / bin / ansible python版本= 3.6.9(默认值,2019年10月17日,11:10:22)[GCC 8.3.0]
答案 0 :(得分:1)
只需检查一下,如果主机不在清单中,那就失败
- fail:
msg: "{{ host }} not in inventory group"
when: host not in hostvars
答案 1 :(得分:1)
除了之前的解决方案,检查是否在清单中定义了单个主机,我发现这个适用于检查一组主机是否定义到清单中,否则执行失败:
- hosts: localhost
become: false
tasks:
- fail:
msg: "Group my_own_group not defined in inventory"
when: "'my_own_group' not in groups.keys()"