Ansible include Directive: Reusing Task Lists and Playbooks

Introduction

The include directive in Ansible allows you to reuse task lists, playbooks, and other YAML files within your Ansible playbooks. This directive provides a convenient way to break down complex tasks into smaller, reusable components and share common configurations across different playbooks. By using include, you can organize your Ansible codebase and promote code reuse, making your playbooks more modular and maintainable. This blog post explores the usage of the include directive, its variations, and real-world examples to demonstrate its effectiveness in reusing YAML files in Ansible.

Understanding the Concept of the include Directive

As Ansible playbooks grow in complexity, it becomes essential to keep them organized and maintainable. The include directive allows you to include content from external files directly into your playbooks. This promotes code reusability by enabling you to break down tasks and configurations into separate files and reuse them in multiple playbooks.

How to Use the include Directive

The include directive is straightforward to use and offers various options for incorporating reusable content. Let’s explore its usage through practical examples:

Syntax and Variations

1. include: Reusing Task Lists

The basic syntax for reusing a task list using the include directive is as follows:

The task_file should contain a list of tasks in YAML format.

2. include_tasks: Reusing Task Lists (Preferred Method)

The include_tasks module can be used as a more explicit and preferred method to include task lists:

3. include_vars: Reusing Variable Files

You can use the include_vars module to include variables from an external file:

4. include_playbook: Reusing Playbooks

To include an entire playbook within another playbook, use the include_playbook module:

Reusing Task Lists

Let’s start with basic examples of using the include_tasks directive to reuse task lists:

Example 1: Including a Task List

Consider a task list defined in a separate file named common_tasks.yml:

Now, let’s create a playbook to include and use the common_tasks.yml task list:

In this example, the include_tasks directive is used to include the common_tasks.yml task list into the main_playbook.yml. This allows us to reuse the tasks defined in common_tasks.yml for the servers group.

Real-World Examples

Let’s explore some real-world scenarios where the include directive proves useful.

Example 1: Shared Task List for Configuration

In a playbook that configures various servers, you may have a common task list for basic configurations:

Now, you can reuse the common_config_tasks.yml in multiple playbooks:

In this example, the common_config_tasks.yml task list is included into both the web_servers_playbook.yml and db_servers_playbook.yml. This allows you to maintain role-specific playbooks with reusable configuration tasks.

Example 2: Variable Inclusion

In a playbook that sets up different environments, you may have environment-specific variables defined in separate files:

Now, you can include the appropriate variables based on the environment:

In this example, the include_vars directive is used to include environment-specific variables based on the value of the ENVIRONMENT environment variable. This allows you to customize playbook behavior for different environments.

Conclusion

The include directive in Ansible provides a powerful way to reuse task lists, playbooks, and variable files. By breaking down tasks and configurations into separate files and reusing them in multiple playbooks, you can promote code modularity and reusability. Throughout this blog post, we explored the concept of the include directive and its variations, along with real-world examples to demonstrate its practical applications.