ansible enviroment variable - ghdrako/doc_snipets GitHub Wiki
- https://docs.ansible.com/ansible/latest/collections/ansible/builtin/env_lookup.html
- https://www.educba.com/ansible-environment-variables/
Print variable
- name: Variable print
hosts: all
tasks:
- name: Print variable
ansible.builtin.debug:
msg: "Print the value of variable {{ fruit }}"
You could override the playbook variables specify the value from the command line. Variables set on the command line are called extra variables.
$ ansible-playbook -i inventory -e fruit=banana variableprint.yml
Let say we need to retrieve the environment variables from the remote hosts so we can use the ansible_env
inbuilt variable, which retrieves the stored environment variables on the remote operating systems.
$ cat envtest1.yml
---
- name: Ansible playbook to retrieve environment variables on windows servers.
hosts: winservers
tasks:
- debug:
msg: "{{ansible_env}}"
$ ansible-playbook envtest1.yml
For the Linux OS:
$ cat envtest1.yml
---
- name: Ansible playbook to retrieve environment variables on linux servers.
hosts: linuxservers
tasks:
- debug:
msg: "{{ansible_env}}"
$ ansible-playbook envtest1.yml
Lookup
- name: "PRE \| Display inventory variables"
debug:
var: hostvars[inventory_hostname]
tags: debug_info
when: lookup('env', 'debug') is defined and lookup('env', 'debug') == 'true'
tags:
- tst
- prd
Another way to retrieve the variable using the default ansible module: ansible.builtin.env
. We need to get the variable values using the lookup
operation as shown below.
$ cat envtest1.yml
---
- name: Ansible playbook to retrieve environment variables on linux servers.
hosts: linuxservers
tasks:
- debug:
msg: "{{ lookup('env','HOME') }}"
$ ansible-playbook envtest1.yml
the lookup
method retrieves the local server environment variable details that we have set while the ansible_env
retrieves the remote server environment variable details.
set the value
tasks:
- debug:
msg: "Explicit defined variable: {{ lookup('env','USR') | default('USERVAL',True) }}"
ansible-playbook linuxenv.yml
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
set enviroment variable at the task level
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 }}"
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.