Using set_stats

67 views
Skip to first unread message

Hiero-nymo

unread,
Oct 27, 2020, 6:26:02 AM10/27/20
to Ansible Project
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:
---
- hosts: all
  gather_facts: yes

  pre_tasks:
    - name: get the hostname
      set_stats:
        data:
          inv_host: "{{ inventory_hostname }}"
   
    - name: get result
      debug:
        msg: "{{ inv_host}}"
[...]
And here's the result:
fatal: [xx.xx.xx.xx]: FAILED! => ... The error was: 'inv_host' is undefined

Is this module only works in Tower or should be run in 'normal' ansible too?
Do anyone has an idea about set_stats? I didn't really found information about this module.

Thanks in advance and best regards,
H.




Vladimir Botka

unread,
Oct 27, 2020, 6:55:33 AM10/27/20
to Hiero-nymo, ansible...@googlegroups.com
On Tue, 27 Oct 2020 03:26:02 -0700 (PDT)
Hiero-nymo <jer.m...@gmail.com> wrote:

> I'm taking the hostname in playbook via set_stats but I cannot check if my
> variable are correct.
> ---
> - hosts: all
> gather_facts: yes
>
> pre_tasks:
> - name: get the hostname
> set_stats:
> data:
> inv_host: "{{ inventory_hostname }}"
>
> - name: get result
> debug:
> 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

shell> cat pb.yml
- hosts: localhost
tasks:
- set_stats:
data:
inv_host: "{{ inventory_hostname }}"

shell> ANSIBLE_SHOW_CUSTOM_STATS=True ansible-playbook pb.yml
...
PLAY RECAP **************************************************
localhost: ok=2 changed=0 unreachable=0 failed=0 skipped=0
rescued=0 ignored=0

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

RUN: inv_host: localhost

--
Vladimir Botka

Hiero-nymo

unread,
Oct 27, 2020, 10:25:22 AM10/27/20
to Ansible Project
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

Matt Martz

unread,
Oct 27, 2020, 10:32:57 AM10/27/20
to ansible...@googlegroups.com
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:


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.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/2d4afa06-7ade-4956-81ba-64cb043ca90en%40googlegroups.com.


--
Matt Martz
@sivel
sivel.net
Reply all
Reply to author
Forward
0 new messages