# how to use a variable from one task to another task

**URL:** <https://forum.ansible.com/t/how-to-use-a-variable-from-one-task-to-another-task/35475>\
**Category:** Ansible Project\
**Created:** [July 8, 2021, 6:52am UTC](https://forum.ansible.com/t/how-to-use-a-variable-from-one-task-to-another-task/35475 "2021-07-08T06:52:06Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Charles\_Daniel](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Charles\_Daniel](https://forum.ansible.com/u/Charles_Daniel)\
**Post date:** [July 8, 2021, 6:52am UTC](https://forum.ansible.com/t/how-to-use-a-variable-from-one-task-to-another-task/35475/1 "2021-07-08T06:52:06Z")

</div>

Team,

I would like to use the variable i got from one ansible task to another task where hosts are different.

Please help me use the variable to use it on another task!!

---

<div class="post-metadata">

**Author:** ![Charles\_Daniel](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Charles\_Daniel](https://forum.ansible.com/u/Charles_Daniel)\
**Post date:** [July 8, 2021, 6:53am UTC](https://forum.ansible.com/t/how-to-use-a-variable-from-one-task-to-another-task/35475/2 "2021-07-08T06:53:34Z")

</div>

When i try to use/inoke on another task, i get error as sec\_tunnel\_ip is undefined.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [July 8, 2021, 7:21am UTC](https://forum.ansible.com/t/how-to-use-a-variable-from-one-task-to-another-task/35475/3 "2021-07-08T07:21:32Z")

</div>

Those variables are scoped to a host, so they’re not available to other hosts.  
A common trick is to assign them to a dummy host and access them that way.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [July 8, 2021, 7:23am UTC](https://forum.ansible.com/t/how-to-use-a-variable-from-one-task-to-another-task/35475/4 "2021-07-08T07:23:37Z")

</div>

[https://stackoverflow.com/questions/39207616/if-set-fact-is-scoped-to-a-host-can-i-use-dummy-host-as-a-global-variable-map](https://stackoverflow.com/questions/39207616/if-set-fact-is-scoped-to-a-host-can-i-use-dummy-host-as-a-global-variable-map)

---

<div class="post-metadata">

**Author:** ![Charles\_Daniel](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Charles\_Daniel](https://forum.ansible.com/u/Charles_Daniel)\
**Post date:** [July 8, 2021, 7:32am UTC](https://forum.ansible.com/t/how-to-use-a-variable-from-one-task-to-another-task/35475/5 "2021-07-08T07:32:49Z")

</div>

Thanks a lot, Dick Visser, I just wanted to access it, even with a dummy hostname is ok. let me try that.

---

<div class="post-metadata">

**Author:** ![Charles\_Daniel](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Charles\_Daniel](https://forum.ansible.com/u/Charles_Daniel)\
**Post date:** [July 8, 2021, 7:56am UTC](https://forum.ansible.com/t/how-to-use-a-variable-from-one-task-to-another-task/35475/6 "2021-07-08T07:56:19Z")

</div>

One more help needed, When I try to do regex to get the ip output, it comes along with the unicode characters such as - [u’  
how to eliminate it while grepping it through regex\_search itself.

Actual output: “- [u’172.16.121.6’]\n”

I want it to be(expected): 172.16.121.6

- name: Obtain secondary tunnel interface ip address  
ios\_command:  
commands:
- “show interface tunnel1 | sec Internet address”  
register: output
- debug:  
msg: “{{ output.stdout}}”
- set\_fact:  
intf: |
- {{output.stdout | map(‘regex\_search’,‘(([0-9]+(.)[0-9]+(.)[0-9]+(.)[0-9]+))’) | list }}  
register: sec\_tunnel\_ip
- debug:  
msg={{ sec\_tunnel\_ip }}

Output:

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [July 8, 2021, 10:04am UTC](https://forum.ansible.com/t/how-to-use-a-variable-from-one-task-to-another-task/35475/7 "2021-07-08T10:04:32Z")

</div>

You're sending only the output of the debug task with the regex\_search  
applied to the output.stdout variable.  
We need to see the output.stdout variable before you mangled it.

---

<div class="post-metadata">

**Author:** ![Charles\_Daniel](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Charles\_Daniel](https://forum.ansible.com/u/Charles_Daniel)\
**Post date:** [July 8, 2021, 10:18am UTC](https://forum.ansible.com/t/how-to-use-a-variable-from-one-task-to-another-task/35475/8 "2021-07-08T10:18:07Z")

</div>

Hi Dick, Below is the complete output of the playbook,

[root@tower1 temp\_play]# ansible-playbook testplay2.yml -i /etc/ansible/temp\_play/hosts

PLAY [Get the secondary router tunnel ip address] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [Obtain secondary tunnel interface ip address] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [172.16.12.1]

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [172.16.12.1] =\> {  
“msg”: [  
“Internet address is [172.16.121.6/32](http://172.16.121.6/32)”  
]  
}

TASK [set\_fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [172.16.12.1]

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [172.16.12.1] =\> {  
“msg”: {  
“ansible\_facts”: {  
“intf”: “- [u’172.16.121.6’]\n”  
},  
“changed”: false,  
“failed”: false  
}  
}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
172.16.12.1 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

[root@tower1 temp\_play]#

Thanks,  
Charles Daniel

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [July 8, 2021, 10:35am UTC](https://forum.ansible.com/t/how-to-use-a-variable-from-one-task-to-another-task/35475/9 "2021-07-08T10:35:52Z")

</div>

> Hi Dick, Below is the complete output of the playbook,
> 
> [root@tower1 temp\_play]# ansible-playbook testplay2.yml -i /etc/ansible/temp\_play/hosts
> 
> PLAY [Get the secondary router tunnel ip address] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> TASK [Obtain secondary tunnel interface ip address] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [172.16.12.1]
> 
> TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [172.16.12.1] =\> {  
> "msg": [  
> "Internet address is 172.16.121.6/32 \<[http://172.16.121.6/32&gt;&quot](http://172.16.121.6/32&gt;&quot);  
> ]  
> }

Hello Charles,

the output looks like a string, so the map filter doesn't makes sense here.

Please try:

{{output.stdout | regex\_search('(([0-9]+(.)[0-9]+(.)[0-9]+(.)[0-9]+))') }}

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Pushparaj\_Gothandam](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/pushparaj_gothandam/32/5538_2.png) [@Pushparaj\_Gothandam](https://forum.ansible.com/u/Pushparaj_Gothandam)\
**Post date:** [July 8, 2021, 11:30am UTC](https://forum.ansible.com/t/how-to-use-a-variable-from-one-task-to-another-task/35475/10 "2021-07-08T11:30:59Z")

</div>

Try “{{ output.stdout[0] }}” on the debug task

---

<div class="post-metadata">

**Author:** ![Charles\_Daniel](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Charles\_Daniel](https://forum.ansible.com/u/Charles_Daniel)\
**Post date:** [July 8, 2021, 1:20pm UTC](https://forum.ansible.com/t/how-to-use-a-variable-from-one-task-to-another-task/35475/11 "2021-07-08T13:20:18Z")

</div>

returns error as a pattern not matching!!.

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [172.16.12.1] =\> {  
“hostvars[‘SEC\_HOST’][‘sec\_tunnel\_ip1’]”: “- [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]\n”  
}

---

<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:** [July 8, 2021, 2:32pm UTC](https://forum.ansible.com/t/how-to-use-a-variable-from-one-task-to-another-task/35475/12 "2021-07-08T14:32:37Z")

</div>

no need for dummy, just use hostvars[\<host that has var\>][\<varname\>]

if you set the vars on a group you can  
hostvars[group[\<groupname\>][0]][\<varname\>] ( 0 is 'first host in  
group, but you can use random index \<= # of hosts in group)

---

<div class="post-metadata">

**Author:** ![Charles\_Daniel](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Charles\_Daniel](https://forum.ansible.com/u/Charles_Daniel)\
**Post date:** [July 8, 2021, 5:38pm UTC](https://forum.ansible.com/t/how-to-use-a-variable-from-one-task-to-another-task/35475/13 "2021-07-08T17:38:08Z")

</div>

Thanks Brian
