Ansible

Ansible Env Lookup

Ansible is a free and open-source automation tool that allows us to manage and coordinate the configurations, software installations, and tasks on multiple systems.

Ansible achieves flexibility using lookups which allow us to load the required data from external sources such as environment variables.

In this tutorial, we will learn about the lookup feature in Ansible which allows us to retrieve information from the environment variables.

Ansible Env Lookup

As mentioned, the env lookup module allows us to read the value of environment variables. This is very tricky when we don’t want to hardcode the values into the Ansible playbooks or when we need to retrieve the values that might change depending on the environment.

Syntax:

The general syntax of the env lookup is shown as follows:

{{ lookup('env', 'ENVIRONMENT_VARIABLE_NAME') }}

The env plugin fetches the value of the specified environment variable. If the variable is not set, it returns an empty string.

However, we can use the default filter provided by Ansible to configure a fallback value in case the environment variable is not set.

Examples:

Let us explore some examples to understand how to use this plugin in Ansible playbooks.

Example 1: Basic Usage

Suppose we have an environment variable USER_NAME as follows:

export USER_NAME="ansible_admin"

In the Ansible playbook or task, we can retrieve this value using the lookup plugin as shown in the following:

- debug:
    msg: "Hello, {{ lookup('env', 'USER_NAME') }}"

Once we run the provided task, it should print “Hello, ansible_admin”.

Example 2: Using the Default Filter

We can also use Ansible’s default filter to provide a fallback value if the environment variable is not set as follows:

- debug:
    msg: "Hello, {{ lookup('env', 'USER_NAME') | default('root') }}"

If the USER_NAME is not configured, Ansible prints “Hello, root”.

Example 3: Set the Retrieved Value as Variable

Ansible also allows us to set a variable in a playbook with the value that is retrieved from an environment. An example is as follows:

vars:
  user: "{{ lookup('env', 'USER_NAME') | default('root') }}"
- debug:
    msg: "Hello, {{ user }}"

Example 4: Conditional Execution

We can also execute the tasks conditionally based on the specific value of an environment variable. An example is as follows:

- name: Check if DEBUG_MODE is set
  debug:
    msg: "Debug mode is enabled"
  when: lookup('env', 'DEBUG_MODE') == "true"

In this case, Ansible only prints the debug message if the DEBUG_MODE environment variable is true.

Conclusion

We learned about the Ansible env lookup plugin which allows us to read the values of the environment variables that are configured on the system.

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