Conditional Role Execution in Ansible: A Guide to Running the Datadog Role Based on Variables

In complex infrastructure environments, there are often scenarios where you need to run specific tasks or roles conditionally. Ansible, being a powerful automation tool, offers various mechanisms to handle such conditional executions. One common use case is the need to execute a specific role only if a particular variable is set. In this article, we’ll discuss how to execute the Datadog role conditionally based on a variable passed at runtime.

The Problem Statement

You want to run the Datadog Ansible role to install and configure the Datadog agent on your servers, but only if a specific variable, say datadog, is set to 1. This ensures that the role is executed only when required, avoiding unnecessary installations and configurations during other deployments.

The initial approach might look like this:

- name: Datadog
  hosts: all
  become: true
  roles:
    - datadog.datadog
  when: datadog is defined and datadog == '1'

However, this configuration leads to an error because the when statement is not supported at the play level in Ansible. It can only be used with tasks. So, how do we achieve this conditional execution?

The Solution: Using include_role with Conditions

To address this issue, we need to move the conditional logic into the tasks section of the playbook. We can use the include_role directive, which allows us to include a role dynamically based on certain conditions. Here’s how we can rewrite the playbook:

---
- name: Datadog Role Execution
  hosts: all
  become: true
  tasks:
    - name: Include Datadog role if condition is met
      include_role:
        name: datadog.datadog
      when: datadog is defined and datadog == '1'

How It Works

  1. include_role Directive: This directive is used to dynamically include and execute a role within the task list. It offers more flexibility than defining roles at the play level.
  2. Conditional Execution with when: The when statement checks if the datadog variable is defined and equals 1. Only if this condition is true, the role will be included and executed.
  3. Avoiding Unnecessary Execution: By using this method, the Datadog role will only be executed when explicitly requested, ensuring a more efficient and controlled deployment process.

Implementing the Solution

Here are the steps to implement this solution in your environment:

  1. Prepare Your Inventory and Playbook: Ensure your inventory file (inventory/production.yaml) and playbook (playbooks/common.yaml) are correctly configured.

  2. Set Up the Datadog Role: Ensure the Datadog role (datadog.datadog) is available either locally in your roles directory or via Ansible Galaxy.

  3. Modify the Playbook: Update your playbook to use the include_role directive with the when condition, as shown above.

  4. Run the Playbook with the Variable: Use the following command to run the playbook with the datadog variable set to 1:

    ansible-playbook -e 'datadog=1'
    

    This command will ensure that the Datadog role is executed only when datadog=1 is passed.

Advanced Use Cases

This method of conditional role execution can be extended to various other scenarios, such as:

  • Multiple Conditions: You can use more complex conditions with and, or, and nested when statements to control the execution of roles based on various variables.
  • Different Roles Based on Variables: You could use multiple include_role statements to include different roles based on different variables, providing a highly dynamic and configurable playbook.

Best Practices for Conditional Role Execution

  1. Use Meaningful Variable Names: Ensure your variables are descriptive to make the playbook more readable.
  2. Document Conditions: Document the purpose of each condition within your playbook to provide context to other users.
  3. Test Thoroughly: Always test your playbooks with and without the conditions to ensure they behave as expected.

Conclusion

By leveraging the include_role directive with conditional logic, you can make your Ansible playbooks more dynamic and responsive to different deployment needs. This approach not only optimizes the execution of roles like Datadog but also makes your automation scripts more maintainable and efficient. Whether you are managing simple setups or complex infrastructures, mastering conditional role execution is a valuable skill in your Ansible toolkit.

With these techniques, you can now control your playbooks more granularly, ensuring that each role is executed exactly when it’s needed. Happy automating!

Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Patreon Buy me a Pizza