Repository: ansible/awx

Issue:
Ability to add AWX Inventory name as a variable in playbook
##### ISSUE TYPE
 - Feature Idea

##### SUMMARY
<!-- Briefly describe the problem or desired enhancement. -->
Per a customer's request, they would like to query the name of the Inventory utilized as a variable and use that variable in a playbook.

~~~
---
- name: Test connectivity
  hosts: all

  tasks:
    - name: Run hostname command
      command: hostname
    - name: Run Inventory Name
      command: echo {{ inventory_name }}
~~~


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
@@ -1397,6 +1397,13 @@ def awx_meta_vars(self):
                 r['{}_user_email'.format(name)] = created_by.email
                 r['{}_user_first_name'.format(name)] = created_by.first_name
                 r['{}_user_last_name'.format(name)] = created_by.last_name
+
+        inventory = getattr_dne(self, 'inventory')
+        if inventory:
+            for name in ('awx', 'tower'):
+                r['{}_inventory_id'.format(name)] = inventory.pk
+                r['{}_inventory_name'.format(name)] = inventory.name
+
         return r
 
     def get_queue_name(self):


Test patch:
diff --git a/awx/main/tests/unit/models/test_unified_job_unit.py b/awx/main/tests/unit/models/test_unified_job_unit.py
--- a/awx/main/tests/unit/models/test_unified_job_unit.py
+++ b/awx/main/tests/unit/models/test_unified_job_unit.py
@@ -9,7 +9,8 @@
     Job,
     User,
     Project,
-    JobTemplate
+    JobTemplate,
+    Inventory
 )
 
 
@@ -81,11 +82,13 @@ class TestMetaVars:
 
     def test_job_metavars(self):
         maker = User(username='joe', pk=47, id=47)
+        inv = Inventory(name='example-inv', id=45)
         assert Job(
             name='fake-job',
             pk=42, id=42,
             launch_type='manual',
-            created_by=maker
+            created_by=maker,
+            inventory=inv
         ).awx_meta_vars() == {
             'tower_job_id': 42,
             'awx_job_id': 42,
@@ -100,7 +103,11 @@ def test_job_metavars(self):
             'awx_user_last_name': '',
             'tower_user_last_name': '',
             'awx_user_id': 47,
-            'tower_user_id': 47
+            'tower_user_id': 47,
+            'tower_inventory_id': 45,
+            'awx_inventory_id': 45,
+            'tower_inventory_name': 'example-inv',
+            'awx_inventory_name': 'example-inv'
         }
 
     def test_project_update_metavars(self):
diff --git a/awx/main/tests/unit/test_tasks.py b/awx/main/tests/unit/test_tasks.py
--- a/awx/main/tests/unit/test_tasks.py
+++ b/awx/main/tests/unit/test_tasks.py
@@ -256,6 +256,7 @@ class TestExtraVarSanitation(TestJobExecution):
 
     def test_vars_unsafe_by_default(self, job, private_data_dir):
         job.created_by = User(pk=123, username='angry-spud')
+        job.inventory = Inventory(pk=123, name='example-inv') 
 
         task = tasks.RunJob()
         task.build_extra_vars_file(job, private_data_dir)
@@ -268,13 +269,14 @@ def test_vars_unsafe_by_default(self, job, private_data_dir):
                        'awx_user_name', 'tower_job_launch_type',
                        'awx_project_revision',
                        'tower_project_revision', 'tower_user_name',
-                       'awx_job_launch_type']:
+                       'awx_job_launch_type',
+                       'awx_inventory_name', 'tower_inventory_name']:
             assert hasattr(extra_vars[unsafe], '__UNSAFE__')
 
         # ensure that non-strings are marked as safe
         for safe in ['awx_job_template_id', 'awx_job_id', 'awx_user_id',
                      'tower_user_id', 'tower_job_template_id',
-                     'tower_job_id']:
+                     'tower_job_id', 'awx_inventory_id', 'tower_inventory_id']:
             assert not hasattr(extra_vars[safe], '__UNSAFE__')