Ansible

Ansible Include_Vars

In the Ansible ecosystem, we have access to the include_vars module that allows us to load the variables from a file, directory, or files that match a glob pattern within a given playbook.

In this tutorial, we will learn how to use this module which allows us to organize our Ansible configurations into more logical and clean units which are easier to reuse and maintain.

Ansible Variables

By default, we mainly define the variables inside the playbook. However, when the playbooks become more complex, you may need to maintain a certain level of organization and maintenance within them. This is where the include_vars module comes to aid.

Using this module, we can separate the variables into specific files which we can import and reuse as necessary. This is particularly useful when working in different environments such as production, staging, etc. where the requirements may differ.

Advantages of Include_Vars Module

The following are some pronounced advantages of separating the variables into specific files:

Separation of Logic and Data – The include_vars module allows us to keep the playbooks concise by separating the tasks from the configuration data.

Environment-specific Configurations – Separating the variables allows us to use different variable files for different environments or hosts.

Reduced Duplication – It allows us to avoid defining the same variables in multiple places.

Ansible Include_Vars Syntax

The following shows the basic syntax of the include_vars module within an Ansible playbook:

- name: Include variables from a file
  include_vars:
    file: file_path

Example Usage:

Let us explore some basic examples of configuring and working with the include_vars module in a playbook.

Example 1: Basic Usage

Suppose we have a directory layout as shown in the following:

.
├── playbook.yml
└── vars
    └── db_config.yml
In the root directory, we have the actual playbook that we wish to execute. We also have the “db_config.yml” file under the vars directory which we use to define the needed variables for database configuration.

Next, in the var/db_config.yml file, we have the following variables:

db_user: root
db_password: mysql

To use the previous variables in a playbook, we can use the include_vars module as shown in the following playbook example:

---
- hosts: all
  tasks:
    - name: Include DB configuration
      include_vars:
        file: vars/db_config.yml
    - name: Print the DB user
      debug:
        msg: "Database user is {{ db_user }}"

Example 2: Including All Files in A Directory

We can also include all the files within a given directory as shown in the following example:

---
- hosts: all
  tasks:
    - name: Include all configuration
      include_vars:
        dir: vars

This should include all the variables that are defined in all the files under the vars directory.

Example 3: Loading Variables Conditionally

We can also use various parameters to include the variables that match a specific condition as shown in the following:

---
- hosts: all
  vars:
    env: staging

  tasks:
    - name: Include environment-specific configuration
      include_vars:
        file: "vars/{{ env }}.yml"

In this case, we load the “staging.yml” as defined in the previous playbook.

Conclusion

You learned about the Ansible include_vars module which allows us to load the variables that are defined in a specific file or directory. You can check the documentation for a more granular control.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list