Execute ansible tasks based on group memberships

Executing a task based on a group membership works the following. Given is an example inventory and role. This is the inventory:

[managed]
mgmt.lithilion.at
squid.lithilion.at

[mgmt]
mgmt.lithilion.at

[proxy]
squid.lithilion.at
And the role:
- name: copy proxy file
  template:
    src: 80proxy.j2
    dest: /etc/apt/apt.conf.d/80proxy
  when:
    - "'proxy' not in group_names"

- name: ensure, that proxy file is not on proxy
  file:
    path: /etc/apt/apt.conf.d/80proxy
    state: absent
  when:
    - "'proxy' in group_names"
The first task is only executed at hosts that are NOT in the group [proxy]. The second task is only executed at hosts that are in the group [proxy].

Previous Blog Entry Next Blog Entry


Last update: 2025-01-14