Ansible include_vars Directive: Reusing Variables

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:

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:

Now, let’s create a playbook to include and use the variables defined in common_vars.yml:

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:

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.

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:

Now, you can include the shared variables in multiple playbooks:

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.