Updated May 18, 2023
Introduction to Ansible environment variables
Ansible Environment variables are used to set the environment variable for action on the remote host using the environment keyword, which can be set at the playbook or task level and doesn’t affect the Ansible configuration file or the environment set for the user. It doesn’t automatically include the facts gathered by gather_facts but needs to have explicitly gather_facts in the playbook and then select the environment to enable them to collect into facts.
Syntax
To work with the environment variables, we can use a few syntaxes.
- To set the remote environment :
environment:
- To retrieve the remote server’s environment variable,
ansible_env
- Windows module to work with the environment variable
win_environment
- Built-in module,
ansible.builtin.env
Parameters supported by win_environment module-level – string (required)
- machine: Used to set an environment variable for all the users.
- user: Used to set an environment variable for the current user that executed the playbook.
- process: Used to set the environment variable for the current process. Not useful as the other two.
- name – string (required)
Name of the environment variable.
- state – string (optional)
- absent
- present
- value – string (optional)
It is a must when state=present and can be omitted for state=absent.
How does the environment variable work in Ansible?
As mentioned in the Syntax section, each syntax has a different way to set, remove and retrieve the environment variables on the remote host.
For example,
We need to retrieve the environment variables from the remote hosts to use the ansible_env inbuilt variable, which retrieves the stored environment variables on the remote operating systems.
---
- name: Ansible playbook to retrieve environment variables on windows servers.
hosts: winservers
tasks:
- debug:
msg: "{{ansible_env}}"
ansible-playbook envtest1.yml
Output:
For the Linux OS:
---
- name: Ansible playbook to retrieve environment variables on linux servers.
hosts: linuxservers
tasks:
- debug:
msg: "{{ansible_env}}"
ansible-playbook envtest1.yml
Output:
You can use this variable(s) in the playbook that we will explain later in the examples.
Another way to retrieve the variable is using the default ansible module: ansible.builtin.env. As shown below, we need to get the variable values using the lookup operation.
---
- name: Ansible playbook to retrieve environment variables on linux servers.
hosts: linuxservers
tasks:
- debug:
msg: "{{ lookup('env','HOME') }}"
ansible-playbook envtest1.yml
Output:
Examples of Ansible environment variables
Here are the following examples mentioned below
Example #1
Copy the installer to the Windows user’s temporary drive using the environment variable.
This playbook copies the 7-zip installer to the user’s temporary drive, installs the 7-zip, and removes the installer from the temp drive.
---
- name: Ansible playbook to install 7zip using environment variable
hosts: winservers
tasks:
- name: Copy 7zip file to the destination path
win_copy:
src: 'https://cdn.educba.com/etc/installers/7z1900-x64.msi'
dest: '{{ ansible_env.TEMP }}\7z1900-x64.msi'
- name: Install the 7zip
win_package:
path: '{{ansible_env.TEMP}}\7z1900-x64.msi'
product_id: "{23170F69-40C1-2702-1900-000001000000}"
- name: Delete the installer
win_file:
path: '{{ansible_env.TEMP}}\7z1900-x64.msi'
state: absent
ansible-playbook envtest1.yml
cat envtest1.yml
Output:
In this example, we are retrieving the Windows environment temp directory using {{ansible_env.Temp}}.
Example #2
Playbook to retrieve the Unix environment variables.
We can use the ansible_env and lookup functions to retrieve stored environment variables.
---
- name: Ansible playbook to get linux environment variables.
hosts: linuxservers
tasks:
- debug:
msg: "{{ ansible_env }}"
The above playbook retrieves all the environment variables inside the unix os.
ansible-playbook linuxenv.yml
Now we need to retrieve the USER env variable. Again, we can use both methods.
---
- name: Ansible playbook to get linux environment variables.
hosts: linuxservers
tasks:
- debug:
msg: "First method : {{ ansible_env.HOME}}"
- debug:
msg: "Second method: {{(lookup('env','HOME'))}}"
ansible-playbook linuxenv.yml
Output:
If you notice the difference, both have different outputs because the lookup method retrieves the local server environment variable details we have set. In contrast, the ansible_env retrieves the remote server environment variable details.
If the variable value is not defined, we can set its default value as shown below.
---
- name: Ansible playbook to get linux environment variables.
hosts: linuxservers
tasks:
- debug:
msg: "Undefined variable: {{ lookup('env','USR') }}"
ansible-playbook linuxenv.yml
Output:
Output is an empty string. Therefore, we can set the value as shown below.
tasks:
- debug:
msg: "Explicit defined variable: {{ lookup('env','USR') | default('USERVAL',True) }}"
ansible-playbook linuxenv.yml
Output:
Example #3
Setting (adding/removing) environment variables in the Windows system.
To work with the Windows servers environment variable set, easy way to use its available Windows module called win_enviornment.
This playbook adds the environment variable at the system level (Machine level) and removes the user environment variable on the Windows servers.
---
- name: Set windows environment variables
hosts: winservers
tasks:
- name: Adding environment variable at the machine level
win_environment:
state: present
name: ScriptDir
value: 'D:\prod\scripts'
level: machine
- name: Removing environment variable at the user level
win_environment:
state: absent
name: Testvar
level: user
Example #4
Setting up the remote environment in the task.
In this example, we are preparing the remote server’s environment to use the environment variable, particularly at the task level. Other tasks can’t see their value. So we are setting two environment values to be used by the task.
---
- name: Setting up the remote environment to use an environment variable.
hosts: linuxservers
vars:
destpath: '/tmp/userdir'
tasks:
- name: Install cobbler
ansible.builtin.package:
name: cobbler
state: present
environment:
http_proxy: http://proxy.example.com:8080
PATH: "{{ destpath }}"
After running this task, when you run ansible_env, you won’t find the environment variable.
To set the environment variable at the play level,
---
- name: Setting up the remote environment to use an environment variable.
hosts: linuxservers
vars:
destpath: '/tmp/userdir'
environment:
http_proxy: http://proxy.example.com:8080
PATH: "{{ destpath }}"
tasks:
- name: task1
- name: task2
It can be used for multiple tasks.
Conclusion
Working with an environment variable in Ansible is easy and useful. However, we often need to provide the environment variable value to the playbook to remain consistent throughout the deployment. For example, if we need to copy the file to the user’s home drive of the Windows servers, then we can directly access the home drive path with the environment variable.
Recommended Articles
This is a guide to Ansible environment variables. Here we discuss how the environment variable works in Ansible, along with the examples and outputs. You may also have a look at the following articles to learn more –