1
votes

I pass variable 'host' to do some work on just one host of a environment:

- name: Perform an action on a host
  hosts: "{{ host }}"
  roles:
  - role: mule_action

When the host is not in the inventory it produces [WARNING]: Could not match supplied host pattern, ignoring: MuleQ01

I need this to fail so it shows up in jenkins (my orchestrator for Ansible) as a failed build.

My ansible.cfg looks like this:

[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

Running: ansible 2.9.7 config file = /data/ansible.cfg configured module search path = ['/home/mule/.ansible/plugins/modules',/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python3.6/site-packages/ansible executable location = /usr/bin/ansible python version = 3.6.9 (default, Oct 17 2019, 11:10:22) [GCC 8.3.0]

2

2 Answers

1
votes

Just do a check before and if your host isn't in the inventory, just fail

- fail:
    msg: "{{ host }} not in inventory group"
  when: host not in hostvars
1
votes

In addition to the previous solution, that checks if an individual host is defined in the inventory, I have found out this one appliable to check if a group of hosts is defined into the inventory, failing the execution otherwise:

- hosts: localhost
  become: false
  tasks:
  - fail:
      msg: "Group my_own_group not defined in inventory"
    when: "'my_own_group' not in groups.keys()"