Repository: ansible/awx

Issue:
new job environment variable: awx_workflow_launch_type
<!-- Issues are for **concrete, actionable bugs and feature requests** only - if you're just asking for debugging help or technical support, please use:

- http://webchat.freenode.net/?channels=ansible-awx
- https://groups.google.com/forum/#!forum/awx-project

We have to limit this because of limited volunteer time to respond to issues! -->

##### ISSUE TYPE
 - Feature Idea

##### SUMMARY
<!-- Briefly describe the problem or desired enhancement. -->
The job environment provides the variable `awx_job_launch_type`

> tower_job_launch_type: The description to indicate how the job was started:
> - manual: Job was started manually by a user.
> - relaunch: Job was started via relaunch.
> - callback: Job was started via host callback.
> - scheduled: Job was started from a schedule.
> - dependency: Job was started as a dependency of another job.
> - workflow: Job was started from a workflow job.
> - sync: Job was started from a project sync.
> - scm: Job was created as an Inventory SCM sync.

(tower_ replaced by awx_ works)
ref: https://docs.ansible.com/ansible-tower/latest/html/userguide/job_templates.html 

Could we have a variable `awx_workflow_launch_type` too, as the indication of how the workflow job was started might be of interest?

> awx_workflow_launch_type: The description to indicate how the job was started:
> - manual: Workflow was started manually by a user.
> - relaunch: Workflow was started via relaunch.
> - callback: Workflow was started via host callback.
> - scheduled: Workflow was started from a schedule.
> - dependency: Workflow was started as a dependency of another job.




Discussion:

Resolving patch:
diff --git a/awx/main/models/unified_jobs.py b/awx/main/models/unified_jobs.py
--- a/awx/main/models/unified_jobs.py
+++ b/awx/main/models/unified_jobs.py
@@ -1453,6 +1453,7 @@ def awx_meta_vars(self):
             for name in ('awx', 'tower'):
                 r['{}_workflow_job_id'.format(name)] = wj.pk
                 r['{}_workflow_job_name'.format(name)] = wj.name
+                r['{}_workflow_job_launch_type'.format(name)] = wj.launch_type
                 if schedule:
                     r['{}_parent_job_schedule_id'.format(name)] = schedule.pk
                     r['{}_parent_job_schedule_name'.format(name)] = schedule.name


Test patch:
diff --git a/awx/main/tests/functional/models/test_unified_job.py b/awx/main/tests/functional/models/test_unified_job.py
--- a/awx/main/tests/functional/models/test_unified_job.py
+++ b/awx/main/tests/functional/models/test_unified_job.py
@@ -154,6 +154,7 @@ def test_workflow_job_metavars(self, admin_user, job_template):
         assert data['awx_user_id'] == admin_user.id
         assert data['awx_user_name'] == admin_user.username
         assert data['awx_workflow_job_id'] == workflow_job.pk
+        assert data['awx_workflow_job_launch_type'] == workflow_job.launch_type
 
     def test_scheduled_job_metavars(self, job_template, admin_user):
         schedule = Schedule.objects.create(
@@ -197,6 +198,8 @@ def test_scheduled_workflow_job_node_metavars(self, workflow_job_template):
             'tower_workflow_job_name': 'workflow-job',
             'awx_workflow_job_id': workflow_job.id,
             'tower_workflow_job_id': workflow_job.id,
+            'awx_workflow_job_launch_type': workflow_job.launch_type,
+            'tower_workflow_job_launch_type': workflow_job.launch_type,
             'awx_parent_job_schedule_id': schedule.id,
             'tower_parent_job_schedule_id': schedule.id,
             'awx_parent_job_schedule_name': 'job-schedule',