Ansible

Ansible Environment Variables

Ansible is an open-source software provisioning, configuration management, and application deployment tool. It uses declarative language to describe the system’s desired state and leverages SSH to communicate and enforce that state on the target machines.

Environment variables are some of the most useful tools in dynamic environments. But what exactly are environment variables?

Environment variables are key-value pairs that can influence the behavior of the tools that run in a system. They act as global settings which allows the different processes to communicate and configure their behavior without modifying the code.

This tutorial teaches us how to work with environment variables in Ansible using various methods and techniques.

Environment Variables in Ansible

Ansible has numerous environment variables that we can modify and configure to match our desired needs. Such environment variables dictate everything from where Ansible looks for its inventory, how it logs the messages, to the number of parallel tasks it should run.

It is good to remember that environment variables may be missing or introduced depending on the installed Ansible version and the installed modules.

Setting the Ansible Environment Variables

We can set the environment variables in multiple ways:

Using the Shell

Before running an Ansible command, we can export the variable in the system shell and then reference it as follows:

export ANSIBLE_INVENTORY=/path/to/my/inventory.ini
ansible-playbook my_playbook.yml

This sets a different path for the ANSIBLE_INVENTORY variable which determines where Ansible will look for the inventory list.

Ansible Configuration File

We can also configure the environment variables in Ansible’s configuration file (ansible.cfg) using the env section:

[defaults]
inventory = /path/to/my/inventory.ini

The given entry uses the Ansible [defaults] block.

In line with the Ansible Command

The third method to configure the environment variables in Ansible is passing them directly in Ansible ad hoc commands.

ANSIBLE_INVENTORY=/path/to/my/inventory.ini ansible-playbook my_playbook.yml

Accessing the Ansible Environment Variables

We can access the environment variables in an Ansible playbook and roles using the lookup plugin. An example is as follows:

---
- hosts: localhost
  tasks:
    - name: Print HOME environment variable
      debug:
        msg: "{{ lookup('env', 'HOME') }}"

Conclusion

We covered the functionality of environment variables in the context of Ansible. Using this tutorial, you learned the various methods and techniques of setting the environment variables and looking them up in Ansible playbooks.

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