# How to email all facts

**URL:** <https://forum.ansible.com/t/how-to-email-all-facts/12425>\
**Category:** Ansible Project\
**Created:** [February 26, 2013, 2:56pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425 "2013-02-26T14:56:24Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Devin\_Bayer](https://avatars.discourse-cdn.com/v4/letter/d/9de053/32.png) [@Devin\_Bayer](https://forum.ansible.com/u/Devin_Bayer)\
**Post date:** [February 26, 2013, 2:56pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/1 "2013-02-26T14:56:24Z")

</div>

Hi. We would like a way to email all the facts from a host. What’s the easiest way to achieve this?

I have tried:

ansible host.domain -m mail -a ‘to=b@t-0.be from=root@localhost body=${ansible\_facts} subject=welcome’

and also a playbook with a $hosts variable, but I just get emails like: ${ansible\_facts}

Thanks,  
Devin

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 26, 2013, 3:02pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/2 "2013-02-26T15:02:12Z")

</div>

${ansible\_facts} isn't an actual variable in ansible

However, you have lots of options.

One easy way of getting all of the variables is:

${hostvars.{$ansible\_hostname}}

(The second set of {} are for escaping any "."'s that may occur in an  
IP address)

Or if you want a specific host named 'example', for example:

${hostvars.example}

You will need to have already talked to the host in a previous play to  
get the facts, otherwise you just get assigned variables.

---

<div class="post-metadata">

**Author:** ![Devin\_Bayer1](https://avatars.discourse-cdn.com/v4/letter/d/54ee81/32.png) [@Devin\_Bayer1](https://forum.ansible.com/u/Devin_Bayer1)\
**Post date:** [February 26, 2013, 3:29pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/3 "2013-02-26T15:29:42Z")

</div>

Hi Michael -

that works if I use $ansible\_fqdn. Thanks! But it only seems to work in a playbook - so is this the right way to do it:

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [February 26, 2013, 3:34pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/4 "2013-02-26T15:34:12Z")

</div>

Devin Bayer wrote:

> > ${ansible\_facts} isn't an actual variable in ansible
> > 
> > However, you have lots of options.
> > 
> > One easy way of getting all of the variables is:
> > 
> > ${hostvars.{$ansible\_hostname}}
> 
> Hi Michael -
> 
> that works if I use $ansible\_fqdn. Thanks! But it only seems to work in a  
> playbook - so is this the right way to do it:

Facts in general only work inside of a playbook.

> ---  
> - hosts: ${hosts}  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- mail: \>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to=b@t-0.be  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from=root@${ansible\_fqdn}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;body="${hostvars.{$ansible\_fqdn}}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;subject=welcome
> 
> Also, any idea about how to improve the output formatting? Right now it's  
> just dict.\_\_str\_\_ all on one line.

The mail module is basically why $TEMPLATE exists. You can have  
body="$TEMPLATE(templates/file.j2)" and then use Jinja2 to format hostvars  
to your liking.

Daniel

---

<div class="post-metadata">

**Author:** ![Devin\_Bayer1](https://avatars.discourse-cdn.com/v4/letter/d/54ee81/32.png) [@Devin\_Bayer1](https://forum.ansible.com/u/Devin_Bayer1)\
**Post date:** [February 26, 2013, 7:50pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/5 "2013-02-26T19:50:49Z")

</div>

Hi Daniel - thanks for the help - I'm really having trouble finding this stuff in the documentation.

> Facts in general only work inside of a playbook.

OK.

> The mail module is basically why $TEMPLATE exists. You can have  
> body="$TEMPLATE(templates/file.j2)" and then use Jinja2 to format hostvars  
> to your liking.

Should this work:

&nbsp;&nbsp;{{ hostvars[ansible\_fqdn] }}

because I get this exception:

&nbsp;&nbsp;jinja2.exceptions.UndefinedError: 'hostvars' is undefined

~ Devin

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 26, 2013, 8:12pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/6 "2013-02-26T20:12:43Z")

</div>

> Hi Daniel - thanks for the help - I'm really having trouble finding this stuff in the documentation.

Probably because we don't have a "how to email facts" topic.

Seriously though, tell me what you'd like to see, and we'll work on it 🙂

---

<div class="post-metadata">

**Author:** ![Devin\_Bayer1](https://avatars.discourse-cdn.com/v4/letter/d/54ee81/32.png) [@Devin\_Bayer1](https://forum.ansible.com/u/Devin_Bayer1)\
**Post date:** [February 26, 2013, 8:25pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/7 "2013-02-26T20:25:01Z")

</div>

> > Hi Daniel - thanks for the help - I'm really having trouble finding this stuff in the documentation.
> 
> Probably because we don't have a "how to email facts" topic.

I can imagine.

> Seriously though, tell me what you'd like to see, and we'll work on it 🙂

Stuff like $TEMPLATE, so maybe a plugins page?

And the available variables on the command line and in playbooks. So maybe a variables page?

Cheers,  
Devin

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 26, 2013, 8:31pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/8 "2013-02-26T20:31:17Z")

</div>

> > > Hi Daniel - thanks for the help - I'm really having trouble finding this stuff in the documentation.
> > 
> > Probably because we don't have a "how to email facts" topic.
> 
> I can imagine.
> 
> > Seriously though, tell me what you'd like to see, and we'll work on it 🙂
> 
> Stuff like $TEMPLATE, so maybe a plugins page?

Noted. I don't even use that one myself, but yes, we should cover more.

This is mostly covered here:

[http://ansible.cc/docs/playbooks2.html#lookup-plugins-accessing-outside-data](http://ansible.cc/docs/playbooks2.html#lookup-plugins-accessing-outside-data)

But we should list individual ones.

> And the available variables on the command line and in playbooks. So maybe a variables page?

Most of those are already there, though dispersed.

As far as CLI options, we've mostly shown those through examples in  
the relevant sections.

Variables:

[http://ansible.cc/docs/playbooks2.html#magic-variables-and-how-to-access-information-about-other-hosts](http://ansible.cc/docs/playbooks2.html#magic-variables-and-how-to-access-information-about-other-hosts)

---

<div class="post-metadata">

**Author:** ![Devin\_Bayer1](https://avatars.discourse-cdn.com/v4/letter/d/54ee81/32.png) [@Devin\_Bayer1](https://forum.ansible.com/u/Devin_Bayer1)\
**Post date:** [February 26, 2013, 9:13pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/9 "2013-02-26T21:13:10Z")

</div>

> Noted. I don't even use that one myself, but yes, we should cover more.
> 
> This is mostly covered here:
> 
> [http://ansible.cc/docs/playbooks2.html#lookup-plugins-accessing-outside-data](http://ansible.cc/docs/playbooks2.html#lookup-plugins-accessing-outside-data)

Yes, I see it now. The documentation is fairly complete - when first reading it I was impressed. But I find it hard to reference, since as you said everything is spread around.

> Variables:
> 
> [http://ansible.cc/docs/playbooks2.html#magic-variables-and-how-to-access-information-about-other-hosts](http://ansible.cc/docs/playbooks2.html#magic-variables-and-how-to-access-information-about-other-hosts)

Maybe it got lost in the thread, but the jinja2 example doesn't seem to work, even using it exactly from trunk:

&nbsp;&nbsp;{{ hostvars['my.host.fqdn']['ansible\_distribution'] }}

results in:

&nbsp;&nbsp;jinja2.exceptions.UndefinedError: 'hostvars' is undefined

Thanks,  
Devin

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 26, 2013, 9:27pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/10 "2013-02-26T21:27:49Z")

</div>

That's a typo! Good catch, missing an underscore.

Try this:

&nbsp;&nbsp;{{ host\_vars['my.host.fqdn']['ansible\_distribution'] }}

---

<div class="post-metadata">

**Author:** ![Devin\_Bayer1](https://avatars.discourse-cdn.com/v4/letter/d/54ee81/32.png) [@Devin\_Bayer1](https://forum.ansible.com/u/Devin_Bayer1)\
**Post date:** [February 26, 2013, 9:39pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/11 "2013-02-26T21:39:41Z")

</div>

Oh, that looked hopeful, but I'm afraid:

&nbsp;&nbsp;jinja2.exceptions.UndefinedError: 'host\_vars' is undefined

~ devin

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 26, 2013, 9:41pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/12 "2013-02-26T21:41:35Z")

</div>

In a template?

The template plugin $TEMPLATE() macro may not have access to it. It's  
new and rarely used. Probably fixable. Let me know if that's the  
case.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 26, 2013, 9:41pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/13 "2013-02-26T21:41:54Z")

</div>

> > > That's a typo! Good catch, missing an underscore.
> > > 
> > > Try this:
> > > 
> > > {{ host\_vars['my.host.fqdn']['ansible\_distribution'] }}
> > 
> > Oh, that looked hopeful, but I'm afraid:
> > 
> > &nbsp;&nbsp;jinja2.exceptions.UndefinedError: 'host\_vars' is undefined
> 
> In a template?

To be clear, I mean, the template action, there, it definitely works.

---

<div class="post-metadata">

**Author:** ![Devin\_Bayer1](https://avatars.discourse-cdn.com/v4/letter/d/54ee81/32.png) [@Devin\_Bayer1](https://forum.ansible.com/u/Devin_Bayer1)\
**Post date:** [February 26, 2013, 9:47pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/14 "2013-02-26T21:47:57Z")

</div>

Do you mean like this:

TASK: [template src=templates/welcome.j2 dest=/tmp/abc] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [banks....] =\> {'msg': "'host\_vars' is undefined", 'failed': True}

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [February 26, 2013, 9:51pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/15 "2013-02-26T21:51:21Z")

</div>

I actuall do this by:

ansible -m setup \<hostname\> |mail me@mydomain.com

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 26, 2013, 9:55pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/16 "2013-02-26T21:55:35Z")

</div>

Show me your welcome.j2 please.

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [February 26, 2013, 10:01pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/17 "2013-02-26T22:01:35Z")

</div>

Devin Bayer wrote:

> > Noted. I don't even use that one myself, but yes, we should cover  
> > more.
> > 
> > This is mostly covered here:
> > 
> > [http://ansible.cc/docs/playbooks2.html#lookup-plugins-accessing-outside-data](http://ansible.cc/docs/playbooks2.html#lookup-plugins-accessing-outside-data)
> 
> Yes, I see it now. The documentation is fairly complete - when first  
> reading it I was impressed. But I find it hard to reference, since as you  
> said everything is spread around.
> 
> > Variables:
> > 
> > [http://ansible.cc/docs/playbooks2.html#magic-variables-and-how-to-access-information-about-other-hosts](http://ansible.cc/docs/playbooks2.html#magic-variables-and-how-to-access-information-about-other-hosts)
> 
> Maybe it got lost in the thread, but the jinja2 example doesn't seem to  
> work, even using it exactly from trunk:
> 
> &nbsp;&nbsp;{{ hostvars['my.host.fqdn']['ansible\_distribution'] }}
> 
> results in:
> 
> &nbsp;&nbsp;jinja2.exceptions.UndefinedError: 'hostvars' is undefined

What version of Jinja2? What version of ansible? What OS?

Daniel

---

<div class="post-metadata">

**Author:** ![jpmens1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jpmens1](https://forum.ansible.com/u/jpmens1)\
**Post date:** [February 27, 2013, 7:36am UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/18 "2013-02-27T07:36:22Z")

</div>

There seems to be a problem with indexing `hostvars' in $TEMPLATE().

I've documented a possible solution (and the traceback when indexing  
hostvars) at [1]

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP

[1] [https://gist.github.com/jpmens/5045984](https://gist.github.com/jpmens/5045984)

---

<div class="post-metadata">

**Author:** ![jpmens1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jpmens1](https://forum.ansible.com/u/jpmens1)\
**Post date:** [February 27, 2013, 8:23am UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/19 "2013-02-27T08:23:07Z")

</div>

> There seems to be a problem with indexing `hostvars' in $TEMPLATE().
> 
> [1] [https://gist.github.com/jpmens/5045984](https://gist.github.com/jpmens/5045984)

For the record: Daniel says I'm missing a "name:" on the action. Adding  
that indeed fixes the problem, but I think that's a bug, as 'name:' is  
supposed to be optional. (Gist updated with a comment.)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [February 27, 2013, 9:19am UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/20 "2013-02-27T09:19:45Z")

</div>

Jan-Piet Mens wrote:

> > There seems to be a problem with indexing `hostvars' in $TEMPLATE().
> > 
> > [1] [https://gist.github.com/jpmens/5045984](https://gist.github.com/jpmens/5045984)
> 
> For the record: Daniel says I'm missing a "name:" on the action. Adding  
> that indeed fixes the problem, but I think that's a bug, as 'name:' is  
> supposed to be optional. (Gist updated with a comment.)

This bug is now fixed on devel.

Daniel

[Next page](https://forum.ansible.com/t/how-to-email-all-facts/12425.md?page=2)
