# How To Add Debug Messages To Ansible Tower Email Notification?

**URL:** <https://forum.ansible.com/t/how-to-add-debug-messages-to-ansible-tower-email-notification/33362>\
**Category:** AWX Project\
**Created:** [July 9, 2020, 5:46pm UTC](https://forum.ansible.com/t/how-to-add-debug-messages-to-ansible-tower-email-notification/33362 "2020-07-09T17:46:03Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcus\_Hayhurst](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Marcus\_Hayhurst](https://forum.ansible.com/u/Marcus_Hayhurst)\
**Post date:** [July 9, 2020, 5:46pm UTC](https://forum.ansible.com/t/how-to-add-debug-messages-to-ansible-tower-email-notification/33362/1 "2020-07-09T17:46:03Z")

</div>

Hello everyone!

Is it possible to add a debug message to the Tower 3.7.0 email notification? For example, I have a Play that shows a certain package if it was found and then displays the version of said package via the debug message but I would like that information sent in an email notification along with some other Ansible facts such as distro and version.

---

<div class="post-metadata">

**Author:** ![wmedley](https://avatars.discourse-cdn.com/v4/letter/w/258eb7/32.png) [@wmedley](https://forum.ansible.com/u/wmedley)\
**Post date:** [July 9, 2020, 5:49pm UTC](https://forum.ansible.com/t/how-to-add-debug-messages-to-ansible-tower-email-notification/33362/2 "2020-07-09T17:49:20Z")

</div>

define a fact using the set\_fact module including the information you want to pass, then pass the stdout of that fact to the body of the email message

- name: “e-mail report”  
mail:

---

<div class="post-metadata">

**Author:** ![Marcus\_Hayhurst](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Marcus\_Hayhurst](https://forum.ansible.com/u/Marcus_Hayhurst)\
**Post date:** [July 14, 2020, 5:42pm UTC](https://forum.ansible.com/t/how-to-add-debug-messages-to-ansible-tower-email-notification/33362/3 "2020-07-14T17:42:01Z")

</div>

Thank you! I used the “register” module instead but your suggestion worked. The problem now is an email is sent from every host in my Inventory that the Job was executed against. Is there anyway to combine all of the debug messages into one message and place that in the body of my email?

Here is the Playbook I have put together:

`

---

<div class="post-metadata">

**Author:** ![wmedley](https://avatars.discourse-cdn.com/v4/letter/w/258eb7/32.png) [@wmedley](https://forum.ansible.com/u/wmedley)\
**Post date:** [July 14, 2020, 5:46pm UTC](https://forum.ansible.com/t/how-to-add-debug-messages-to-ansible-tower-email-notification/33362/4 "2020-07-14T17:46:16Z")

</div>

add run\_once = yes and see if that works.

---

<div class="post-metadata">

**Author:** ![Marcus\_Hayhurst](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Marcus\_Hayhurst](https://forum.ansible.com/u/Marcus_Hayhurst)\
**Post date:** [July 14, 2020, 6:21pm UTC](https://forum.ansible.com/t/how-to-add-debug-messages-to-ansible-tower-email-notification/33362/5 "2020-07-14T18:21:28Z")

</div>

That did it except now I only receive one email with the debug message for first machine the job ran against. Is there a way to get all the machines debug messages into that email body? I’m guessing this is some sort of Jinja for loop to extract the debug msg array?

---

<div class="post-metadata">

**Author:** ![wmedley](https://avatars.discourse-cdn.com/v4/letter/w/258eb7/32.png) [@wmedley](https://forum.ansible.com/u/wmedley)\
**Post date:** [July 14, 2020, 6:33pm UTC](https://forum.ansible.com/t/how-to-add-debug-messages-to-ansible-tower-email-notification/33362/6 "2020-07-14T18:33:57Z")

</div>

ok so if you are looking for a single email with data from all the hosts that it is ran against that is a bit more complicated. You need to write the data from each host as a new line to a local file, then include the contents of that file in the email.

so instead of registering your results, create a local file and then add new lines to that file using lineinfile and then output the results in your email…

[https://docs.ansible.com/ansible/latest/modules/lineinfile\_module.html](https://docs.ansible.com/ansible/latest/modules/lineinfile_module.html)

---

<div class="post-metadata">

**Author:** ![Mike\_Charchuk](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@Mike\_Charchuk](https://forum.ansible.com/u/Mike_Charchuk)\
**Post date:** [July 14, 2020, 6:43pm UTC](https://forum.ansible.com/t/how-to-add-debug-messages-to-ansible-tower-email-notification/33362/7 "2020-07-14T18:43:08Z")

</div>

In a workflow I use set\_stats with aggregate set on to accumulate output from multiple hosts with the playbook/job in serial mode. I then send an email with the data in a separate job/playbook.

Not sure how the output would look if it wasn’t done in serial mode.

Mike

---

<div class="post-metadata">

**Author:** ![Marcus\_Hayhurst](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Marcus\_Hayhurst](https://forum.ansible.com/u/Marcus_Hayhurst)\
**Post date:** [July 17, 2020, 6:48pm UTC](https://forum.ansible.com/t/how-to-add-debug-messages-to-ansible-tower-email-notification/33362/8 "2020-07-17T18:48:23Z")

</div>

Would you be able to provide these specific task(s) from your Playbook? I keep getting undefined variable errors. Here’s the set\_stats and mail portion from my Playbook:

`

- name: Collect Info About XAGT  
set\_stats:  
data:  
xagt\_email: “{% if xagt\_presence is defined %} XAGT Version ‘{{ ansible\_facts.packages[‘xagt’][0][‘version’] }}’ Found on ‘{{ ansible\_hostname }}’{% else %} XAGT Not Found on ‘{{ ansible\_hostname }}’{% endif %}”  
per\_host: no  
aggregate: yes

- name: XAGT Email Report  
delegate\_to: localhost  
mail:

---

<div class="post-metadata">

**Author:** ![Nuno\_Jordao](https://avatars.discourse-cdn.com/v4/letter/n/a4c791/32.png) [@Nuno\_Jordao](https://forum.ansible.com/u/Nuno_Jordao)\
**Post date:** [July 18, 2020, 4:37pm UTC](https://forum.ansible.com/t/how-to-add-debug-messages-to-ansible-tower-email-notification/33362/9 "2020-07-18T16:37:55Z")

</div>

Another alternative is to create a jinja template iterating the host\_vars, where all variables are stored, and generate an html table, for example, to be sent in one email by the localhost.

---

<div class="post-metadata">

**Author:** ![Mani\_Mohan](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@Mani\_Mohan](https://forum.ansible.com/u/Mani_Mohan)\
**Post date:** [July 19, 2020, 4:03am UTC](https://forum.ansible.com/t/how-to-add-debug-messages-to-ansible-tower-email-notification/33362/10 "2020-07-19T04:03:53Z")

</div>

You can use map extract filter against ansible\_play\_hosts for the debug msg of individual host, which will extract the data from each host and store in to a final result variable via set\_fact.

Both this set\_fact and email tasks keep it in a block and add run\_once to the block.

Based on your code,

- set\_fact:  
Final\_output: “{{ ansible\_play\_hosts | map { ‘extract’, hostvars, ‘xagt\_debug[‘msg’]’ | list } }}  
run\_once: yes

Thanks and Regards  
Mani Mohan

---

<div class="post-metadata">

**Author:** ![Marcus\_Hayhurst](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Marcus\_Hayhurst](https://forum.ansible.com/u/Marcus_Hayhurst)\
**Post date:** [July 20, 2020, 7:10pm UTC](https://forum.ansible.com/t/how-to-add-debug-messages-to-ansible-tower-email-notification/33362/11 "2020-07-20T19:10:25Z")

</div>

I receive this error when using your suggestion:

`  
TASK [set\_fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [my\_machines\_hostname]: FAILED! =\> {“msg”: “template error while templating string: expected token ‘end of print statement’, got ‘{’. String: “{{ ansible\_play\_hosts | map { ‘extract’, hostvars, ‘xagt\_debug[‘msg’]’ | list } }}"”}

`

Here are the relevant tasks I have in my Playbook:

`

- name: Is XAGT Installed?  
debug:  
msg: “{% if xagt\_presence is defined %} XAGT Version ‘{{ ansible\_facts.packages[‘xagt’][0][‘version’] }}’ Found on ‘{{ ansible\_hostname }}’{% else %} XAGT Not Found on ‘{{ ansible\_hostname }}’{% endif %}”  
register: xagt\_debug

---

<div class="post-metadata">

**Author:** ![Mani\_Mohan](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@Mani\_Mohan](https://forum.ansible.com/u/Mani_Mohan)\
**Post date:** [July 21, 2020, 2:30pm UTC](https://forum.ansible.com/t/how-to-add-debug-messages-to-ansible-tower-email-notification/33362/12 "2020-07-21T14:30:01Z")

</div>

My bad !  
For map parenthesis needs to be used not the curly braces. Pls refer below

> > - set\_fact:  
> > Final\_output: “{{ ansible\_play\_hosts | map ( ‘extract’, hostvars, ‘xagt\_debug[‘msg’]’ | list ) }}  
> > run\_once: yes

Thanks and Regards  
Mani Mohan

---

<div class="post-metadata">

**Author:** ![Marcus\_Hayhurst](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Marcus\_Hayhurst](https://forum.ansible.com/u/Marcus_Hayhurst)\
**Post date:** [July 23, 2020, 6:41pm UTC](https://forum.ansible.com/t/how-to-add-debug-messages-to-ansible-tower-email-notification/33362/13 "2020-07-23T18:41:30Z")

</div>

Still not working:

`

TASK [Set Fact Final output]

93  
fatal: [REDACTED]: FAILED! =\> {“msg”: “template error while templating string: expected token ‘,’, got ‘msg’. String: {{ ansible\_play\_hosts | map ( ‘extract’, hostvars, ‘xagt\_debug[‘msg’]’ | list ) }}”}

`

`

- name: Set Fact Final output  
set\_fact:  
Final\_output: “{{ ansible\_play\_hosts | map ( ‘extract’, hostvars, ‘xagt\_debug[‘msg’]’ | list ) }}”  
run\_once: yes

`

---

<div class="post-metadata">

**Author:** ![Mani\_Mohan](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@Mani\_Mohan](https://forum.ansible.com/u/Mani_Mohan)\
**Post date:** [July 24, 2020, 2:27am UTC](https://forum.ansible.com/t/how-to-add-debug-messages-to-ansible-tower-email-notification/33362/14 "2020-07-24T02:27:05Z")

</div>

Looks like, map is having issues reading the data in variable ‘xagt\_debug[‘msg’]’  
Try setting a teporary fact with data available in the variable ‘xagt\_debug[‘msg’]’, and then try the map extract with that variable.

it looks like this

- name: Is XAGT Installed?  
debug:  
msg: “{% if xagt\_presence is defined %} XAGT Version ‘{{ ansible\_facts.packages[‘xagt’][0][‘version’] }}’ Found on ‘{{ ansible\_hostname }}’{% else %} XAGT Not Found on ‘{{ ansible\_hostname }}’{% endif %}”  
register: xagt\_debug

- set\_fact:  
temp\_var: “{{ xagt\_debug[‘msg’] }}”

- set\_fact:  
Final\_output: “{{ ansible\_play\_hosts | map { ‘extract’, hostvars, ‘temp\_var’ | list } }}"  
run\_once: yes

Thanks and Regards

Mani Mohan

---

<div class="post-metadata">

**Author:** ![Marcus\_Hayhurst](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Marcus\_Hayhurst](https://forum.ansible.com/u/Marcus_Hayhurst)\
**Post date:** [August 11, 2020, 1:15pm UTC](https://forum.ansible.com/t/how-to-add-debug-messages-to-ansible-tower-email-notification/33362/15 "2020-08-11T13:15:08Z")

</div>

Hello again,

When trying your suggestion, I get this error:

“msg”: “template error while templating string: expected token ‘end of print statement’, got ‘{’. String: {{ ansible\_play\_hosts | map { ‘extract’, hostvars, ‘temp\_var’ | list } }}”

- name: SET FACT FOR XAGT PACKAGE  
set\_fact:  
xagt\_presence: “{{ ansible\_facts.packages[‘xagt’] }}”  
cacheable: yes  
ignore\_errors: True

---

<div class="post-metadata">

**Author:** ![Marcus\_Hayhurst](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Marcus\_Hayhurst](https://forum.ansible.com/u/Marcus_Hayhurst)\
**Post date:** [August 20, 2020, 5:17pm UTC](https://forum.ansible.com/t/how-to-add-debug-messages-to-ansible-tower-email-notification/33362/16 "2020-08-20T17:17:11Z")

</div>

Looks like we are making progress:

`

- name: IS XAGT INSTALLED?  
debug:  
msg: “{% if xagt\_presence is defined %} XAGT Version ‘{{ ansible\_facts.packages[‘xagt’][0][‘version’] }}’ Found on ‘{{ ansible\_hostname }}’{% else %} XAGT Not Found on ‘{{ ansible\_hostname }}’{% endif %}”  
register: xagt\_debug

---

<div class="post-metadata">

**Author:** ![Mani\_Mohan](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@Mani\_Mohan](https://forum.ansible.com/u/Mani_Mohan)\
**Post date:** [August 20, 2020, 5:34pm UTC](https://forum.ansible.com/t/how-to-add-debug-messages-to-ansible-tower-email-notification/33362/17 "2020-08-20T17:34:23Z")

</div>

Yup, something to do with the formatting may be, but yeah most of the times when formatting results from register doesn’t work, fallback option is to set it as a fact in separate task and use the variable, like we do it any other programming languages.

So you are able to see all the accumulated results in Final\_output variable ? That debug task also should run once.

Thanks and Regards  
Mani Mohan

---

<div class="post-metadata">

**Author:** ![Marcus\_Hayhurst](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Marcus\_Hayhurst](https://forum.ansible.com/u/Marcus_Hayhurst)\
**Post date:** [August 21, 2020, 1:51pm UTC](https://forum.ansible.com/t/how-to-add-debug-messages-to-ansible-tower-email-notification/33362/18 "2020-08-21T13:51:48Z")

</div>

Doh! Sorry Mani I spoke to soon! I was looking at the wrong “set\_fact” in my PLAY RECAP. Even though I was wrong I finally was able to get an output via email of all my hosts using the “Final\_output” mapping:

`[" XAGT Version '29.7.8' Found on 'hostname redacted'", " XAGT Version '29.7.8' Found on 'hostname redacted'", " XAGT Not Found on 'hostname redacted'", " XAGT Version '29.7.8' Found on 'hostname redacted'", " XAGT Version '29.7.8' Found on 'hostname redacted'", " XAGT Version '29.7.8' Found on 'hostname redacted'", " XAGT Not Found on 'hostname redacted'", " XAGT Version '29.7.8' Found on 'hostname redacted'"]`

But I thought the mapping function would provide a list and not everything enclosed in brackets? Would you know how I could get this in a nice list for the email (with maybe one space between each line)?

Here is what worked for the “Final\_output” fact

`

- set\_fact:  
temp\_var: “{{ xagt\_debug[‘msg’] }}”
