# Sending output from a completed job via the mail module

**URL:** <https://forum.ansible.com/t/sending-output-from-a-completed-job-via-the-mail-module/31209>\
**Category:** Ansible Project\
**Tags:** awx\
**Created:** [September 5, 2019, 2:57pm UTC](https://forum.ansible.com/t/sending-output-from-a-completed-job-via-the-mail-module/31209 "2019-09-05T14:57:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [September 5, 2019, 2:57pm UTC](https://forum.ansible.com/t/sending-output-from-a-completed-job-via-the-mail-module/31209/1 "2019-09-05T14:57:07Z")

</div>

We have a playbook where, once its completed, we’d like to email the stdout to certain users. Right now I am testing with the command module in the playbook as follows:

- name: Get job output  
command: curl -k -u admin:{{ tower\_pass }} -X GET https:///api/v2/jobs/{{ tower\_id }}/stdout/  
register: output

-name: Print output  
debug:  
msg: “{{ output.stdout }}”

Obviously it doesn’t work or else I wouldn’t be posting this. The error I get is:

The task includes an option with an undefined variable. The error was: ‘tower\_id’ is undefined\n\nThe error appears to be in ‘/var/lib/awx/projects/tsose/tsose\_uptime.yml’: line 15, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Get job output\n ^ here\n

Any ideas on how we can get a job’s stdout via the API once the job is completed?

Thanks,  
Harry

---

<div class="post-metadata">

**Author:** ![Cade\_Lambert](https://avatars.discourse-cdn.com/v4/letter/c/278dde/32.png) [@Cade\_Lambert](https://forum.ansible.com/u/Cade_Lambert)\
**Post date:** [September 5, 2019, 4:22pm UTC](https://forum.ansible.com/t/sending-output-from-a-completed-job-via-the-mail-module/31209/2 "2019-09-05T16:22:16Z")

</div>

Couple of things:

1. Ansible has a uri module for API calls, it’ll do the same thing but best practice is to use a module if there’s one available.
2. Where are you getting tower\_id from? It looks like it’s undefined.
3. Are you kicking the template off with an API call too? That’d be the best way, then register the results and parse out the job ID from those results.

Something like

- API call to initiate template and register results into variable1
- API call to get the job output, using the variable to parse the job ID, then register those results to variable2
- Mail module to email the contents of variable2

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [September 5, 2019, 6:06pm UTC](https://forum.ansible.com/t/sending-output-from-a-completed-job-via-the-mail-module/31209/3 "2019-09-05T18:06:57Z")

</div>

I’ll admit, this playbook is running in Ansible Tower, which is where I’m getting tower\_id from. I know that I could reach out to Red Hat for Tower questions, but in the past I’ve had them reject my support requests for these types of questions. They say that they don’t support with any playbook code/authoring, which is why I was reaching out here.

I tried the URI module and got the same error. Basically, I want to email the stdout of a playbook, so do I need to spawn a new playbook to do that?

Thanks,  
Harry

---

<div class="post-metadata">

**Author:** ![Cade\_Lambert](https://avatars.discourse-cdn.com/v4/letter/c/278dde/32.png) [@Cade\_Lambert](https://forum.ansible.com/u/Cade_Lambert)\
**Post date:** [September 5, 2019, 6:30pm UTC](https://forum.ansible.com/t/sending-output-from-a-completed-job-via-the-mail-module/31209/4 "2019-09-05T18:30:10Z")

</div>

tower\_id would be the job id for the job in which the template was run, so you need to pull that job id from somewhere. So basically, yeah, you’d have to create a new playbook to kick off the playbook you want to email the results on. Unfortunately, I’m short on time currently but here’s the outline of what I’d do. It might not be the slickest way but it’d work.

1. API request to run template: [https://docs.ansible.com/ansible-tower/3.5.0/html/towerapi/api\_ref.html#/Job\_Templates/Job\_Templates\_job\_templates\_launch\_create](https://docs.ansible.com/ansible-tower/3.5.0/html/towerapi/api_ref.html#/Job_Templates/Job_Templates_job_templates_launch_create)

- register the results into a job\_run

1. Use the results from job\_run to get the job id. Throw it into a debug task if you need to see the output of job\_run. Usually it’s something like job\_run.results.id

2. API request to get the output from the job, using the job id: [https://docs.ansible.com/ansible-tower/3.5.0/html/towerapi/api\_ref.html#/Jobs/Jobs\_jobs\_read\_0](https://docs.ansible.com/ansible-tower/3.5.0/html/towerapi/api_ref.html#/Jobs/Jobs_jobs_read_0)

- register the results into job\_results

1. Use the mail module to email the contents of job\_results
