# using a variable from one task in the next

**URL:** <https://forum.ansible.com/t/using-a-variable-from-one-task-in-the-next/38455>\
**Category:** Ansible Project\
**Created:** [September 21, 2023, 12:02pm UTC](https://forum.ansible.com/t/using-a-variable-from-one-task-in-the-next/38455 "2023-09-21T12:02:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [September 21, 2023, 12:02pm UTC](https://forum.ansible.com/t/using-a-variable-from-one-task-in-the-next/38455/1 "2023-09-21T12:02:28Z")

</div>

I have a 2 tasks in a playbook and from task1 I have a variable collected from the register output as below.

- name: Playbook to collect the actual hostname  
hosts: server1  
become: yes  
tasks:

- name: collect the actual hostname  
shell: ‘hostname’  
register: short\_name

- name: collect the hostname for the next task  
set\_fact:  
reg\_name: “{{ short\_name.stdout\_lines[-1].split(‘.’)[1] }}”

- name: Playbook to test the new hostname collections  
hosts: 127.0.0.1  
become: yes  
tasks:

- name: print the collected hostname  
debug:  
msg: {{reg\_name }}

collecting the host’s shortname using set\_fact is working . However the data stored using set\_fact in reg\_name is not carry to the next task.

Is there an option/module available to have the variable from one task to be made available to the next?

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [September 21, 2023, 12:32pm UTC](https://forum.ansible.com/t/using-a-variable-from-one-task-in-the-next/38455/2 "2023-09-21T12:32:10Z")

</div>

Works for me …

% ansible --version

ansible [core 2.15.4]

% cat foo.yml

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [September 21, 2023, 12:36pm UTC](https://forum.ansible.com/t/using-a-variable-from-one-task-in-the-next/38455/3 "2023-09-21T12:36:59Z")

</div>

If you gather facts you can reference inventory\_hostname\_short vs running a shell command.

[https://docs.ansible.com/ansible/latest/reference\_appendices/special\_variables.html#term-inventory\_hostname\_short](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html#term-inventory_hostname_short)

Walter

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [September 21, 2023, 2:22pm UTC](https://forum.ansible.com/t/using-a-variable-from-one-task-in-the-next/38455/4 "2023-09-21T14:22:40Z")

</div>

Your first play is targeting **server1** , while your second play is targeting a different host.

If the reg\_name is available in the second task, you would need to retrieve it relative to **server1** :

```auto
    **msg: "{{ hostvars['server1']['reg_name'] }}"**

```

You should look at and contrast the values of **ansible\_hostname** , **inventory\_hostname** , and **ansible\_fqdn**.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [September 21, 2023, 2:22pm UTC](https://forum.ansible.com/t/using-a-variable-from-one-task-in-the-next/38455/5 "2023-09-21T14:22:56Z")

</div>

You are confusing scopes, `set_fact` is PER HOST, not a global. So you  
defined the variable on server1 but are trying to read it from  
127.0.0.1.

So to access in the 2nd play you need to go through hostvars:  
{{hostvasr['server1']['reg\_name']

---

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [September 21, 2023, 2:34pm UTC](https://forum.ansible.com/t/using-a-variable-from-one-task-in-the-next/38455/6 "2023-09-21T14:34:38Z")

</div>

yes. Exactly …  
I did set\_fact on a remote\_host and try to re-call the value of the variable in the localhost.

_So to access in the 2nd play you need to go through hostvars:_  
_{{hostvasr[‘server1’][‘reg\_name’]_

Do you mean I have to use like below?

- name: print the collected hostname  
debug:  
msg: “{{hostvars[‘server1’][‘reg\_name’] }}”

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [September 21, 2023, 2:54pm UTC](https://forum.ansible.com/t/using-a-variable-from-one-task-in-the-next/38455/7 "2023-09-21T14:54:42Z")

</div>

Brian / Todd … good eyes!

Walter

---

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [October 31, 2023, 12:49pm UTC](https://forum.ansible.com/t/using-a-variable-from-one-task-in-the-next/38455/8 "2023-10-31T12:49:27Z")

</div>

Hi ,

I am re-opening this thread as I get a new question on above the existing one.  
Sorry for that…(not lazy to open another)

Is there a way to use the output of play1 , which is a list(of variables) to be passed variable to the second play2  
The playbook for illustration only .

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [October 31, 2023, 3:02pm UTC](https://forum.ansible.com/t/using-a-variable-from-one-task-in-the-next/38455/9 "2023-10-31T15:02:24Z")

</div>

in play2 just use `hostvars['localhost']['server_list']`
