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:
1 2 |
- name: Include Task List include: " |
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:
1 2 |
- name: Include Task List include_tasks: " |
3. include_vars
: Reusing Variable Files
You can use the include_vars
module to include variables from an external file:
1 2 |
- name: Include Variables include_vars: " |
4. include_playbook
: Reusing Playbooks
To include an entire playbook within another playbook, use the include_playbook
module:
1 2 |
- name: Include Playbook include_playbook: " |
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
:
1 2 3 4 5 6 7 8 |
# common_tasks.yml - name: Task 1 ansible.builtin.command: echo "Task 1 executed!" - name: Task 2 ansible.builtin.file: path: "/tmp/task_2_file.txt" state: touch |
Now, let’s create a playbook to include and use the common_tasks.yml
task list:
1 2 3 4 5 6 |
# main_playbook.yml - name: Execute Common Tasks hosts: servers tasks: - name: Include Common Tasks include_tasks: common_tasks.yml |
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:
1 2 3 4 5 6 7 8 9 10 11 |
# common_config_tasks.yml - name: Update Packages ansible.builtin.package: name: "*" state: latest - name: Set Timezone ansible.builtin.timezone: name: "America/New_York" # Additional common configuration tasks |
Now, you can reuse the common_config_tasks.yml
in multiple playbooks:
1 2 3 4 5 6 7 |
# web_servers_playbook.yml - name: Configure Web Servers hosts: web_servers tasks: - include_tasks: common_config_tasks.yml # Other tasks specific to web servers |
1 2 3 4 5 6 7 |
# db_servers_playbook.yml - name: Configure Database Servers hosts: db_servers tasks: - include_tasks: common_config_tasks.yml # Other tasks specific to database servers |
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:
1 2 3 4 |
# variables/dev_vars.yml app_port: 8080 db_host: "localhost" db_port: 3306 |
1 2 3 4 |
# variables/prod_vars.yml app_port: 80 db_host: "prod-db.example.com" db_port: 5432 |
Now, you can include the appropriate variables based on the environment:
1 2 3 4 5 6 7 8 9 |
# main_playbook.yml - name: Deploy Application hosts: app_servers tasks: - name: Include Environment Variables include_vars: "{{ ansible_env.ENVIRONMENT }}_vars.yml" - name: Configure Application # Use the included variables here |
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.