Sending output from a completed job via the mail module

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

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

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

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
  • 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

  • register the results into job_results
  1. Use the mail module to email the contents of job_results