# Using set\_stats

**URL:** <https://forum.ansible.com/t/using-set-stats/34115>\
**Category:** Ansible Project\
**Created:** [October 27, 2020, 10:26am UTC](https://forum.ansible.com/t/using-set-stats/34115 "2020-10-27T10:26:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hieronymus](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hieronymus/32/1624_2.png) [@hieronymus](https://forum.ansible.com/u/hieronymus)\
**Post date:** [October 27, 2020, 10:26am UTC](https://forum.ansible.com/t/using-set-stats/34115/1 "2020-10-27T10:26:02Z")

</div>

Hi all,

Before using set\_stats in Ansible Tower, I’m doing some test to understand it.  
I’m taking the hostname in playbook via set\_stats but I cannot check if my variable are correct.  
Here’s my script portion:

---

<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:** [October 27, 2020, 10:55am UTC](https://forum.ansible.com/t/using-set-stats/34115/2 "2020-10-27T10:55:03Z")

</div>

> I'm taking the hostname in playbook via set\_stats but I cannot check if my  
> variable are correct.  
> ---  
> - hosts: all  
> &nbsp;&nbsp;gather\_facts: yes
> 
> &nbsp;&nbsp;pre\_tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: get the hostname  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_stats:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inv\_host: "{{ inventory\_hostname }}"  
> &nbsp;&nbsp;&nbsp;  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: get result  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ inv\_host}}"  
> [...]  
> And here's the result:  
> fatal: [xx.xx.xx.xx]: FAILED! =\> ... The error was: 'inv\_host' is undefined

The variable "inv\_host" is valid in the context of custom status  
only. The module set\_stats works as expected. For example

> cat pb.yml

- hosts: localhost  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- set\_stats:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inv\_host: "{{ inventory\_hostname }}"

> ANSIBLE\_SHOW\_CUSTOM\_STATS=True ansible-playbook pb.yml

&nbsp;&nbsp;&nbsp;...  
PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost: ok=2 changed=0 unreachable=0 failed=0 skipped=0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescued=0 ignored=0

CUSTOM STATS: \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

&nbsp;&nbsp;RUN: inv\_host: localhost

---

<div class="post-metadata">

**Author:** ![hieronymus](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hieronymus/32/1624_2.png) [@hieronymus](https://forum.ansible.com/u/hieronymus)\
**Post date:** [October 27, 2020, 2:25pm UTC](https://forum.ansible.com/t/using-set-stats/34115/3 "2020-10-27T14:25:22Z")

</div>

Hi Vladimir,

Ok, It’s running in this context by me too, thank you for your help…  
Does this means, that we cannot parse this variable in this role?

Best regards,  
H

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [October 27, 2020, 2:31pm UTC](https://forum.ansible.com/t/using-set-stats/34115/4 "2020-10-27T14:31:54Z")

</div>

The `set_stats` module does not make the “stats” available to the current playbook.

The workflow from tower is effectively to use `set_stats` to accumulate some data for a subsequent playbook in a workflow, then tower collects these stats, and passes them back in as extra-vars. So effectively, they are only useful to the subsequent playbooks in a workflow.

I have an example callback and instructions that showcase this flow outside of tower:

[https://gist.github.com/sivel/eaa83e2b1c36719359e0671afa8cd039](https://gist.github.com/sivel/eaa83e2b1c36719359e0671afa8cd039)

If you want the vars available from within the current playbook, and the subsequent playbook, you will need both a `set_facts` and a `set_stats` task.
