DEV Community

Cover image for Handling platform differences in Ansible playbooks and roles
XLAB Steampunk
XLAB Steampunk

Posted on

1

Handling platform differences in Ansible playbooks and roles

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.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay