# Reading  variable values set in a role which is called multiple times using with-items

**URL:** <https://forum.ansible.com/t/reading-variable-values-set-in-a-role-which-is-called-multiple-times-using-with-items/34308>\
**Category:** Ansible Project\
**Created:** [December 9, 2020, 5:24am UTC](https://forum.ansible.com/t/reading-variable-values-set-in-a-role-which-is-called-multiple-times-using-with-items/34308 "2020-12-09T05:24:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AnsibleUser707](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@AnsibleUser707](https://forum.ansible.com/u/AnsibleUser707)\
**Post date:** [December 9, 2020, 5:24am UTC](https://forum.ansible.com/t/reading-variable-values-set-in-a-role-which-is-called-multiple-times-using-with-items/34308/1 "2020-12-09T05:24:19Z")

</div>

Hello everyone,  
First time poster here so apologies if something is not in correct format.

I am trying to read a two values in a script which are set inside a role using set\_fact. The role is called twice using with-items from the script.

But do not know how to get values in calling script.

In below example, I am expecting two values in variable **value\_in\_role\_register**

Could someone please help how to achieve this ?

Here is the role which is emitting value\_in\_role with time stamp.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [December 9, 2020, 9:09am UTC](https://forum.ansible.com/t/reading-variable-values-set-in-a-role-which-is-called-multiple-times-using-with-items/34308/2 "2020-12-09T09:09:47Z")

</div>

> # dummy\_role.yml  
> - name: set x  
> &nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;value\_in\_role: "{{ ansible\_date\_time.iso8601 }}"
> 
> # test.yaml  
> - hosts: local  
> &nbsp;&nbsp;connection: local  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Task 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include\_role:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: dummy\_role  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: "value\_in\_role\_register"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- call-1st-Time  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- call-2nd-Time
> 
> &nbsp;&nbsp;&nbsp;&nbsp;- debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg="value\_in\_role\_register =\> {{ value\_in\_role\_register.results }}"
> 
> # &nbsp;&nbsp;&nbsp;&nbsp;- debug: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg="value\_in\_role =\> {{ value\_in\_role }}"
> 
> # Output (expecting two values in \*value\_in\_role\_register\* but that is not the case
> 
> ok: [local] =\> {  
> &nbsp;&nbsp;&nbsp;&nbsp;"msg": "\*value\_in\_role\_register\* =\> [{'item': u'call-1st-Time',  
> 'ansible\_loop\_var': u'item', 'include\_args': {u'name': u'dummy\_role'}},  
> {'item': u'call-2nd-Time', 'ansible\_loop\_var': u'item', 'include\_args':  
> {u'name': u'dummy\_role'}}]"  
> }

"include\_role" does not return the tasks' results. Instead, you might  
want to iterate the task inside the role and create the list of the  
results there. For example

&nbsp;&nbsp;\> cat roles/dummy\_role/tasks/main.yml  
&nbsp;&nbsp;- shell: "sleep 1 && date +%T"  
&nbsp;&nbsp;&nbsp;&nbsp;register: value\_in\_role  
&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ calls }}"

&nbsp;&nbsp;\> cat playbook.yml  
&nbsp;&nbsp;- hosts: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- include\_role:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: dummy\_role  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;calls: [call-1st-Time,call-2nd-Time,call-3nd-Time]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ dict(value\_in\_role.results|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;json\_query('.[item,stdout]')) }}"

give

&nbsp;&nbsp;ok: [localhost] =\>  
&nbsp;&nbsp;&nbsp;&nbsp;msg:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;call-1st-Time: '10:04:22'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;call-2nd-Time: '10:04:23'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;call-3nd-Time: '10:04:24'

> TASK [debug]  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [local] =\> {  
> &nbsp;&nbsp;&nbsp;&nbsp;"msg": "\*value\_in\_role\* =\> 2020-12-09T04:56:22Z"  
> }

JFYI, the value of "ansible\_date\_time" is set by the module "setup".  
It's not updated automatically. You have to execute "setup" to update  
"ansible\_date\_time".

---

<div class="post-metadata">

**Author:** ![AnsibleUser707](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@AnsibleUser707](https://forum.ansible.com/u/AnsibleUser707)\
**Post date:** [December 9, 2020, 7:36pm UTC](https://forum.ansible.com/t/reading-variable-values-set-in-a-role-which-is-called-multiple-times-using-with-items/34308/3 "2020-12-09T19:36:07Z")

</div>

Thanks Vladimir for complete working example. Yes it resolved the use case I have.

Wondering if it is the issue with ‘set\_fact’ vs ‘register’ used in role when role is called multiple times. Seems like set\_fact (sets only last value) instead of maintaining multiple values as register does when called in a loop.

Yours \>\> register: value\_in\_role

Mine \>\> set\_fact:  
value\_in\_role:

Anyways, thanks again !

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [December 9, 2020, 8:35pm UTC](https://forum.ansible.com/t/reading-variable-values-set-in-a-role-which-is-called-multiple-times-using-with-items/34308/4 "2020-12-09T20:35:58Z")

</div>

> ... Seems like set\_fact (sets only last value) instead of  
> maintaining multiple values as register does when called in a loop.

Correct. The next option would be the addition of an item to  
the list, e.g. \*my\_dates\*, in each invocation of the role

&nbsp;&nbsp;\> cat roles/dummy\_role\_02/tasks/main.yml  
&nbsp;&nbsp;- setup:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gather\_subset: date\_time  
&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_dates: "{{ my\_dates|default() +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ansible\_date\_time.iso8601\_micro] }}"

Then the playbook

&nbsp;&nbsp;- hosts: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;calls: [call-1st-Time,call-2nd-Time,call-3nd-Time]  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- include\_role:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: dummy\_role\_02  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ calls }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ dict(calls|zip(my\_dates)) }}"

gives

&nbsp;&nbsp;msg:  
&nbsp;&nbsp;&nbsp;&nbsp;call-1st-Time: '2020-12-09T20:33:06.776459Z'  
&nbsp;&nbsp;&nbsp;&nbsp;call-2nd-Time: '2020-12-09T20:33:07.267783Z'  
&nbsp;&nbsp;&nbsp;&nbsp;call-3nd-Time: '2020-12-09T20:33:07.862950Z'

If you're interested in profiling see  
[https://ansible-runner.readthedocs.io/en/latest/intro.html#runner-profiling-data-directory](https://ansible-runner.readthedocs.io/en/latest/intro.html#runner-profiling-data-directory)

---

<div class="post-metadata">

**Author:** ![AnsibleUser707](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@AnsibleUser707](https://forum.ansible.com/u/AnsibleUser707)\
**Post date:** [December 9, 2020, 8:43pm UTC](https://forum.ansible.com/t/reading-variable-values-set-in-a-role-which-is-called-multiple-times-using-with-items/34308/5 "2020-12-09T20:43:42Z")

</div>

Thanks for sharing… always good to know multiple approaches to solve a problem 🙂 .
