Question:
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:
1 2 3 4 |
- name: my command command: echo stuff when: "if {{ ansible_hostname }} not in {{ ansible_current_groups }}" |
How should I do this?
Answer:
Here’s another way to do this:
1 2 3 4 |
- name: my command command: echo stuff when: "'groupname' not in group_names" |
group_names
is a magic variable as documented here: https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html :
group_names is a list (array) of all the groups the current host is in.