# 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:** 17\
**Page:** 2

<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 27, 2013, 9:47am UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/21 "2013-02-27T09:47:54Z")

</div>

Thanks Daniel! This was my problem too!

For my "email facts" I still have one problem. How do I handle double quotes in the output?

If my template is: ( "words ) the mail module complains rightfully: ValueError: No closing quotation. So can I escape the template output in my playbook?

Also, what do you think about allowing inline templates? Like:

&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- shell: echo "$item"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_template: \>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ hostvars|replace('"', '\"') }}

or maybe making all values jinja templates:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mail: \>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;body="{% filter replace('"', '\"') %}{% include welcome.j2 %}{% endfilter %}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;subject=welcome

I could make a patch if there is support.

Cheers,  
Devin

---

<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, 9:48am UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/22 "2013-02-27T09:48:24Z")

</div>

> This bug is now fixed on devel.

Thank you, Daniel; it is indeed.

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

---

<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 27, 2013, 1:51pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/23 "2013-02-27T13:51:22Z")

</div>

> Also, what do you think about allowing inline templates? Like:
> 
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- shell: echo "$item"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_template: \>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ hostvars|replace('"', '\"') }}

One goal of Ansible is to be readable like pseudocode, so I'd rather  
not have this, it's a slippery slope.

That all being said, I also disagree that it complains rightfully, I  
believe it complains non-rightfully, and that the $TEMPLATE macro  
needs to escape quotes when it needs to.

I'll need a minimal example to to reproduce it.

> or maybe making all values jinja templates:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mail: \>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;body="{% filter replace('"', '\"') %}{% include welcome.j2 %}{% endfilter %}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;subject=welcome
> 
> I could make a patch if there is support.

I'd rather not -- using Jinja2 inside of the playbook is not a good  
idea, because it quickly turns our templating to Perlishness IMHO.  
I'd rather fix the root cause.

---

<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, 2:03pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/24 "2013-02-27T14:03:37Z")

</div>

> I'll need a minimal example to to reproduce it.

A template containing

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"hello

suffices.

---

<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, 2:13pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/25 "2013-02-27T14:13:46Z")

</div>

Michael DeHaan wrote:

> > Also, what do you think about allowing inline templates? Like:
> > 
> > &nbsp;&nbsp;tasks:  
> > &nbsp;&nbsp;&nbsp;&nbsp;- shell: echo "$item"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_template: \>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ hostvars|replace('"', '\"') }}
> 
> One goal of Ansible is to be readable like pseudocode, so I'd rather  
> not have this, it's a slippery slope.
> 
> That all being said, I also disagree that it complains rightfully, I  
> believe it complains non-rightfully, and that the $TEMPLATE macro  
> needs to escape quotes when it needs to.

It doesn't know whether you are using ' or ", or if you even want quotes.  
Maybe you are using $TEMPLATE to invoke some Jinja2 to generate your entire  
action line, in which case messing with the quotes would break it.

Daniel

---

<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, 2:17pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/26 "2013-02-27T14:17:30Z")

</div>

Devin Bayer wrote:

> > 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.
> 
> Thanks Daniel! This was my problem too!
> 
> For my "email facts" I still have one problem. How do I handle double  
> quotes in the output?

Best way to get out of quote-hell is to use the new args: way of specifying  
arguments. This means you can include anything you want, and ansible will  
take care of quoting for you.

Daniel

---

<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 27, 2013, 2:39pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/27 "2013-02-27T14:39:54Z")

</div>

> > I'll need a minimal example to to reproduce it.
> 
> A template containing
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"hello
> 
> suffices.

If the $TEMPLATE macro automatically included output in " " and  
escaped internal quotes that would fix it for most people.

I can't think of a use where someone would use $TEMPLATE inside a template.

We could essentially make that a "don't do that" scenario.

---

<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, 3:09pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/28 "2013-02-27T15:09:01Z")

</div>

> Best way to get out of quote-hell is to use the new args: way of specifying  
> arguments. This means you can include anything you want, and ansible will  
> take care of quoting for you.

You mean like this? [https://p.6core.net/p/V3bhkFxeAhsoceyP4ZGPCvFk](https://p.6core.net/p/V3bhkFxeAhsoceyP4ZGPCvFk)

Not really ...

(Setting args: body: "HELLO" works.  
Replacing $TEMPLATE() by $FILE() produces same trace.  
)

&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, 3:24pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/29 "2013-02-27T15:24:57Z")

</div>

Jan-Piet Mens wrote:

> > Best way to get out of quote-hell is to use the new args: way of  
> > specifying  
> > arguments. This means you can include anything you want, and ansible  
> > will  
> > take care of quoting for you.
> 
> You mean like this? [https://p.6core.net/p/V3bhkFxeAhsoceyP4ZGPCvFk](https://p.6core.net/p/V3bhkFxeAhsoceyP4ZGPCvFk)
> 
> Not really ...
> 
> (Setting args: body: "HELLO" works.  
> Replacing $TEMPLATE() by $FILE() produces same trace.  
> )

What is in your file? Non-ASCII characters?

Daniel

---

<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 27, 2013, 4:42pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/30 "2013-02-27T16:42:17Z")

</div>

So there's this story about a guy who goes to a doctor and says "my  
arm hurts when I do this", and the doctor says "don't do that then".

Easiest way to see hostvars is to just write it to the local server  
with "template"

{{ hostvars | to\_json }}

and use the fetch module to pull it back if you want.

---

<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, 6:13pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/31 "2013-02-27T18:13:46Z")

</div>

> What is in your file? Non-ASCII characters?

Plain ASCII.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[https://gist.github.com/jpmens/5045984/raw/b53e7fe94cec694030f22e73b134756581e0b978/mail3.j2](https://gist.github.com/jpmens/5045984/raw/b53e7fe94cec694030f22e73b134756581e0b978/mail3.j2)

---

<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, 6:18pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/32 "2013-02-27T18:18:12Z")

</div>

> and the doctor says "don't do that then".

So you're saying if stuff which is documented breaks, we shouldn't use  
it? Am I getting this right?

> Easiest way to see hostvars is to just write it to the local server  
> with "template"
> 
> {{ hostvars | to\_json }}
> 
> and use the fetch module to pull it back if you want.

Well, that's one way, yes. We could also shell out and lpr it. (Pardon  
the sarcasm.)

I chipped in because I thought OP's request wasn't a bad idea. I'll  
stand down if this is OT. My intention, in posting bug reports, is to  
improve on the quality of stuff.

&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, 6:50pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/33 "2013-02-27T18:50:35Z")

</div>

Jan-Piet Mens wrote:

> > What is in your file? Non-ASCII characters?
> 
> Plain ASCII.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[https://gist.github.com/jpmens/5045984/raw/b53e7fe94cec694030f22e73b134756581e0b978/mail3.j2](https://gist.github.com/jpmens/5045984/raw/b53e7fe94cec694030f22e73b134756581e0b978/mail3.j2)

Thanks. This should work on devel now, there were some issues with how the  
args: were getting transferred that should be fixed now.

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:09pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/34 "2013-02-27T19:09:46Z")

</div>

> This should work on devel now, there were some issues with how the  
> args: were getting transferred that should be fixed now.

Works, thanks.

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

---

<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 28, 2013, 10:05am UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/35 "2013-02-28T10:05:26Z")

</div>

Thanks for all the help and bug-fixes guys. I now have a nice welcome message sent when somebody gets a new box:

[https://gist.github.com/akvadrako/5055631](https://gist.github.com/akvadrako/5055631)

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 28, 2013, 12:55pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/36 "2013-02-28T12:55:39Z")

</div>

Cool!

Send me a patch for "nice\_yaml" to the "filter plugin" module in core,  
and I'll gladly include it.

(Probably to\_nice\_yaml to be consistent)

---

<div class="post-metadata">

**Author:** ![Dag\_Wieers](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@Dag\_Wieers](https://forum.ansible.com/u/Dag_Wieers)\
**Post date:** [March 13, 2013, 4:39pm UTC](https://forum.ansible.com/t/how-to-email-all-facts/12425/37 "2013-03-13T16:39:23Z")

</div>

Nice, that's what I wrote the mail module initially for.

The other (slightly evil) use-case was to nag other teams if our process depended on their (non-automated) intervention(s). Our playbook would bail-out while sending the email (again) until the matter was cleared and we could continue the process.

(Read: complex processes in a large enterprise environment)

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