Introduction
The include_vars
directive in Ansible allows you to reuse variables defined in external files within your playbooks. This directive provides a convenient way to store and share common configurations across different playbooks. By using include_vars
, 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_vars
directive, its parameters, and real-world examples to demonstrate its effectiveness in reusing variables in Ansible.
Understanding the Concept of the include_vars
Directive
As Ansible playbooks grow in complexity, it becomes essential to keep them organized and maintainable. The include_vars
directive allows you to include variable definitions from external files directly into your playbooks. This promotes code reusability by enabling you to define variables in separate files and reuse them in multiple playbooks.
How to Use the include_vars
Directive
The include_vars
directive is straightforward to use and offers a simple way to incorporate reusable variables. Let’s explore its usage through practical examples:
Syntax and Parameters
The basic syntax of the include_vars
directive is as follows:
1 2 |
- name: Include Variables include_vars: " |
The directive accepts the following parameter:
variable_file
: (required) Specifies the path to the file containing the variables you want to include. The path can be relative or absolute.
Reusing Variables
Let’s start with basic examples of using the include_vars
directive to reuse variables:
Example 1: Including Variables
Consider a variable file named common_vars.yml
:
1 2 3 4 |
# common_vars.yml app_port: 8080 db_host: "localhost" db_port: 3306 |
Now, let’s create a playbook to include and use the variables defined in common_vars.yml
:
1 2 3 4 5 6 7 8 9 10 11 12 |
# main_playbook.yml - name: Configure Application hosts: app_servers tasks: - name: Include Common Variables include_vars: common_vars.yml - name: Configure Application with Included Variables some_module: port: "{{ app_port }}" db_host: "{{ db_host }}" db_port: "{{ db_port }}" |
In this example, the include_vars
directive is used to include the variables defined in common_vars.yml
into the main_playbook.yml
. This allows us to reuse the common configurations for the app_servers
group.
Real-World Examples
Let’s explore some real-world scenarios where the include_vars
directive proves useful.
Example 1: Environment-Specific Variables
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 10 11 12 |
# main_playbook.yml - name: Deploy Application hosts: app_servers tasks: - name: Include Environment Variables include_vars: "{{ ansible_env.ENVIRONMENT }}_vars.yml" - name: Configure Application some_module: port: "{{ app_port }}" db_host: "{{ db_host }}" db_port: "{{ db_port }}" |
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.
Example 2: Shared Variable Definitions
In a playbook that configures various servers, you may have a common set of variable definitions used across different playbooks:
1 2 3 4 5 6 7 8 9 |
# shared_vars.yml web_server: app_port: 8080 domain: example.com database_server: db_host: "localhost" db_port: 3306 db_name: mydb |
Now, you can include the shared variables in multiple playbooks:
1 2 3 4 5 6 7 8 |
# web_servers_playbook.yml - name: Configure Web Servers hosts: web_servers tasks: - name: Include Shared Variables include_vars: shared_vars.yml # Use the included variables for web servers configuration |
1 2 3 4 5 6 7 8 |
# db_servers_playbook.yml - name: Configure Database Servers hosts: db_servers tasks: - name: Include Shared Variables include_vars: shared_vars.yml # Use the included variables for database servers configuration |
In this example, the include_vars
directive is used to include the shared variable definitions from shared_vars.yml
into both the web_servers_playbook.yml
and db_servers_playbook.yml
. This allows you to maintain role-specific playbooks with reusable variable configurations.
Conclusion
The include_vars
directive in Ansible provides a powerful way to reuse variables and promote code modularity and reusability. By defining variables in separate files and including them in multiple playbooks, you can avoid code duplication and manage complex configurations efficiently. Throughout this blog post, we explored the concept of the include_vars
directive and provided real-world examples to demonstrate its practical applications.