Set remote environment per task or play - Ansible environment statement
How to set an EXAMPLE environmental variable at play and task Ansible Playbook code level and verify with echo Linux command.


How to set remote environment per Ansible task or play?
I’m going to show you a live demo with some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.
Set remote environment per Ansible task or play
environment
statement
You could set the remote environment with the Ansible statement environment
.
The environment
statement could be applied at the task level or play level.
It’s very useful to set for example proxy in a corporate environment.
Links
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
demo
Set environment per Ansible Playbook task or play level.
code
---
- name: remote environment demo
hosts: all
gather_facts: false
environment:
EXAMPLE: test1
tasks:
- name: diplay EXAMPLE
ansible.builtin.command: "echo $EXAMPLE"
- name: diplay EXAMPLE
ansible.builtin.command: "echo $EXAMPLE"
environment:
EXAMPLE: test2
execution
You need to run the playbook with the verbose option (-v
) in order to see the standard output on the console.
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory ansible\ statements/environment-remote.yml
PLAY [remote environment demo] ********************************************************************
TASK [diplay EXAMPLE] *****************************************************************************
changed: [demo.example.com]
TASK [diplay EXAMPLE] *****************************************************************************
changed: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory ansible\ statements/environment-remote.yml -v
No config file found; using defaults
PLAY [remote environment demo] ********************************************************************
TASK [diplay EXAMPLE] *****************************************************************************
changed: [demo.example.com] => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"}, "changed": true, "cmd": ["echo", "$EXAMPLE"], "delta": "0:00:00.002864", "end": "2022-02-21 09:05:33.059864", "msg": "", "rc": 0, "start": "2022-02-21 09:05:33.057000", "stderr": "", "stderr_lines": [], "stdout": "test1", "stdout_lines": ["test1"]}
TASK [diplay EXAMPLE] *****************************************************************************
changed: [demo.example.com] => {"changed": true, "cmd": ["echo", "$EXAMPLE"], "delta": "0:00:00.002800", "end": "2022-02-21 09:05:33.416884", "msg": "", "rc": 0, "start": "2022-02-21 09:05:33.414084", "stderr": "", "stderr_lines": [], "stdout": "test2", "stdout_lines": ["test2"]}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
idempotency
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory ansible\ statements/environment-remote.yml -v
No config file found; using defaults
PLAY [remote environment demo] ********************************************************************
TASK [diplay EXAMPLE] *****************************************************************************
changed: [demo.example.com] => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"}, "changed": true, "cmd": ["echo", "$EXAMPLE"], "delta": "0:00:00.002864", "end": "2022-02-21 09:05:33.059864", "msg": "", "rc": 0, "start": "2022-02-21 09:05:33.057000", "stderr": "", "stderr_lines": [], "stdout": "test1", "stdout_lines": ["test1"]}
TASK [diplay EXAMPLE] *****************************************************************************
changed: [demo.example.com] => {"changed": true, "cmd": ["echo", "$EXAMPLE"], "delta": "0:00:00.002800", "end": "2022-02-21 09:05:33.416884", "msg": "", "rc": 0, "start": "2022-02-21 09:05:33.414084", "stderr": "", "stderr_lines": [], "stdout": "test2", "stdout_lines": ["test2"]}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
before execution
ansible-pilot $ ssh [email protected]
Last login: Fri Feb 18 16:07:48 2022 from 192.168.0.59
[[email protected] ~]$ echo $EXAMPLE
[[email protected] ~]$
after execution
ansible-pilot $ ssh [email protected]
Last login: Mon Feb 21 07:05:33 2022 from 192.168.251.111
[[email protected] ~]$ echo $EXAMPLE
Recap
Now you know how to set remote environment per Ansible task or play. Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.
Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate