I'm toying with the idea of having a playbook that runs a certain task when a server is in a group and another task when it is not in a group.

Say I have office1 with all servers and I have a primary DHCP server:



I want to make sure that I only have one DHCP server:

- include: tasks/add-dhcp.yml
  when: inventory_hostname in dhcp
- include: tasks/remove-dhcp.yml
  when: inventory_hostname not in dhcp

So that when I change DHCP to server3, it will be installed on 3 and removed from 2. The roll will then run on all office1 servers when a new DHCP deploy is done.

Any ideas with this? Is it a bad idea? How would you solve this problem?


If this approach works for you, then go with it. There is nothing good or bad in the way you define configuration. Other than that, you can use variables.

The conditional should however look like below:

- include_tasks: tasks/remove-dhcp.yml
  when: `dhcp` not in group_names

group_names is a magic variable which contains a list of all groups that the executing target belongs to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.