Search code examples
ansibleansible-2.xansible-inventory

How to check in Ansible if group exist in Inventory file or not?


My case is:

If group doesn't exist in inventory file then I want to throw an error instead of getting a warning like: 'No host matched'.

Ansible version: 2.4


Solution

  • Add a play checking for group existence to the top of your playbook. Use groups magic variable in the condition:

    ---
    - hosts: localhost
      gather_facts: no
      connection: local
      tasks:
        - fail:
          when: "'my_group' not in groups"