Search code examples
ansible

Run task only if host does not belong to a group


I'd like to able to run an ansible task only if the host of the current playbook does not belong to a certain group. In semi pseudo code:

- name: my command
  command: echo stuff
  when: "if {{ ansible_hostname }} not in {{ ansible_current_groups }}"

How should I do this?


Solution

  • Here's another way to do this:

    - name: my command
      command: echo stuff
      when: "'groupname' not in group_names"
    

    group_names is a magic variable as documented here:

    List of groups the current host is part of, it always reflects the inventory_hostname and ignores delegation.