# Environment variables

**URL:** <https://forum.ansible.com/t/environment-variables/38521>\
**Category:** Ansible Developer\
**Created:** [October 15, 2023, 5:43pm UTC](https://forum.ansible.com/t/environment-variables/38521 "2023-10-15T17:43:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Y.G\_Kumar](https://avatars.discourse-cdn.com/v4/letter/y/8e8cbc/32.png) [@Y.G\_Kumar](https://forum.ansible.com/u/Y.G_Kumar)\
**Post date:** [October 15, 2023, 5:43pm UTC](https://forum.ansible.com/t/environment-variables/38521/1 "2023-10-15T17:43:13Z")

</div>

Hi All,

Please don’t yell at me for posting in this group.  
I have already written to the ansible project group.  
They are saying it is not possible to achieve the following result.  
I have written the below code and it is not working.

---

<div class="post-metadata">

**Author:** ![Avinash\_Jadhav](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Avinash\_Jadhav](https://forum.ansible.com/u/Avinash_Jadhav)\
**Post date:** [October 15, 2023, 5:46pm UTC](https://forum.ansible.com/t/environment-variables/38521/2 "2023-10-15T17:46:44Z")

</div>

Please try this one

---

<div class="post-metadata">

**Author:** ![Y.G\_Kumar](https://avatars.discourse-cdn.com/v4/letter/y/8e8cbc/32.png) [@Y.G\_Kumar](https://forum.ansible.com/u/Y.G_Kumar)\
**Post date:** [October 16, 2023, 7:09am UTC](https://forum.ansible.com/t/environment-variables/38521/3 "2023-10-16T07:09:21Z")

</div>

This is not working either:

---

<div class="post-metadata">

**Author:** ![Y.G\_Kumar](https://avatars.discourse-cdn.com/v4/letter/y/8e8cbc/32.png) [@Y.G\_Kumar](https://forum.ansible.com/u/Y.G_Kumar)\
**Post date:** [October 16, 2023, 12:06pm UTC](https://forum.ansible.com/t/environment-variables/38521/4 "2023-10-16T12:06:25Z")

</div>

Just to reiterate my requirement. I just want to create a remote environment variable by assigning to it the value contained in a registered variable while executing the shell command  
as shown below…

---

<div class="post-metadata">

**Author:** ![tremble](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tremble/32/247_2.png) [@tremble](https://forum.ansible.com/u/tremble)\
**Post date:** [October 16, 2023, 12:31pm UTC](https://forum.ansible.com/t/environment-variables/38521/5 "2023-10-16T12:31:45Z")

</div>

Attempting to read between the lines, you’ve misunderstood what the “environment” parameter does. The “environment” parameter sets the shell environment variables for a specific _task_[1]. It doesn’t affect later tasks in the play. The fragments you posted both attempt to set the environment variable for a single task based on the future output of said task. Ansible isn’t prescient, and can’t do this. Hence you see the error. What you probably wanted to do was retrieve the output, and then use it in a future task.

Based on your response to Avinash you’re trying to consume that environment variable inside “pure.yml”, which could be achieved as follows.

“”"

- hosts: localhost  
tasks:

- name: “subtask”  
shell: lsblk --nodeps | grep disk | wc -l  
register: disk\_count\_result

- name: “set fact”  
set\_fact:  
disk\_count: “{{ disk\_count\_result.stdout | int }}”

- include\_tasks: pure.yml

environment:  
MY\_DISK\_COUNT: “{{ disk\_count }}”

“”"

That said, I suspect you’re trying to convert a bash script into an Ansible playbook. In which case “set\_fact” is the closest analogue to setting environment variables, and you’re probably best totally ignoring the “environment” parameter. Once you use “set\_fact” in a task, all tasks after it can access the fact it sets.

For example:

“”"

- hosts: localhost  
tasks:

- name: “Get number of disks”  
shell: lsblk --nodeps | grep disk | wc -l  
register: disk\_count\_result

- name: “set disk\_count fact”  
set\_fact:  
disk\_count: “{{ disk\_count\_result.stdout | int }}”

- name: “Do something with disk\_count”  
shell: “echo {{ disk\_count }}”

- name: “Do something with MY\_DISK\_COUNT exposed as an environment variable to this specific task”  
shell: “export | grep COUNT”  
environment:  
MY\_DISK\_COUNT: “{{ disk\_count }}”

- name: “repeat same command without exposing MY\_DISK\_COUNT to this specific task”  
shell: “export | grep COUNT”  
“”"

However, as you appear to already know. The question you’ve posted is _not_ what this list is for:

- It’s not a bug in Ansible (which should be reported in GitHub as an issue, but might get traction here)
- It’s not an attempt to start a discussion about how some future feature might function.
- It’s not a question about developing a custom module for Ansible.
- It’s not an announcement about a milestone in the Ansible development lifecycle.

This kind of question is best suited for the ansible-project mailing list which “is for sharing Ansible tips, answering questions about playbooks and roles, and general user discussion”. But, you should include more than just the output of a single task and its error. I would also strongly recommend spending some time working through some Ansible tutorials. I suspect you’ve misunderstood some of the underlying fundamentals of how Ansible works, which is only going to lead to further frustration.

Mark  
[1] In the case of block, import\_tasks and include\_tasks, it applies to tasks run as part of the block/import/include.
