Injecting information about the running job into a template

I’m sending my users an email, not a notification, via a task at the end of the play. I want to send them a link to the job they just ran. I would like to make that variable a part of the template. What variable do I use?

- name: Email the requestor the status of their renewal  
  community.general.mail:
    host: mail.host
    port: 25
    subject: "Certificate Renewal by Definition invoked for {{ fqdn }}"
    body: "{{ lookup('template', ('certificatedefinitionlog_body.txt.j2')) }}"
    to:
    - "{{ email_contact }}"
    charset: us-ascii
  delegate_to: localhost

Given this line as a part of certificatedefinitionlog_body.txt.j2, how do I inject information about the running task? And, actually, this job is the last job template in a workflow job. How would I link to that workflow job? Or is there a variable I can use to inject the System variable for Miscellaneous\Base URL?

{{ ansible_user }} launched a Certificate renewal by Definition Document on your certificate, {{ fqdn }}. The renewal log can be viewed at {{ ??? }}

2 Likes

ChatGPT and my searches indicated I should be able to use variables with names like {{tower.??}}, {{awx.??}}, and {{job.??}}. That advice was bogus. As nearly as I can tell, this is not something many people are doing. That’s a surprise to me. I have mildly trained people running AWX automations from Powershell, and I want to email them a link to their workflow job log. I would not have expected this to be as hard as it turns out to be.

Heres a list of variable set while running a playbook: Special Variables — Ansible Community Documentation

Heres a list of variables set while running a job template from tower (they are listed towards the end of this section. and these are in addition to the variables above.): 16. Job Templates — Ansible Tower User Guide v3.5.0

And heres an example of a playbook that will print all the variables available to a host: How can I print out the actual values of all the variables used by an Ansible playbook? - Stack Overflow

1 Like