If we want to use Ansible to deploy things on different targets, we need to handle differences between our targets. Thankfully, Ansible provides different language constructs for conditionally enabling certain parts of the playbook or role.
The most basic tool is the when keyword that we can use to disable a task execution based on some criterion. And if we have more than one target-specific tasks to execute, we can group them into a block and conditionally execute the whole sequence.
But adding too many conditionals can turn readable Ansible playbooks into a giant mess. This is where dynamic reuse comes into play (pun intended 😉). Various include statements allow us to place the platform-specific contents into separate files. And with some creative file naming, we can get rid of most of the conditionals and replace them with parameterized includes.
If you found these tips useful, you are welcome to subscribe to our monthly newsletter and get more tips & tricks on Ansible Collections.
Top comments (0)