AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible environment Keyword: Set Environment Variables Per Task or Play — Video Tutorial

How to set environment variables per task or play in Ansible using the environment keyword. Configure proxy, PATH, and app-specific variables with examples.

Watch Video

Watch "Ansible environment Keyword: Set Environment Variables Per Task or Play" on YouTube

What You'll Learn

Full Tutorial Content

How to set remote environment per Ansible task or play? I'm going to show you a live Playbook 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 - [Setting the remote environment](https://docs.ansible.com/ansible/latest/user_guide/playbooks_environment.html) ## Playbook Set environment per Ansible Playbook task or play level. code ```yaml --- - name: remote environment Playbook 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. ```bash ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory ansible\ statements/environment-remote.yml PLAY [remote environment Playbook] ******************************************************************** 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 Playbook] ******************************************************************** 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 *******************************************************

About This Tutorial

Read the full written article: Ansible environment Keyword: Set Environment Variables Per Task or Play

Topics Covered

Related Video Tutorials