Browse code

ansible_playbook_python (#18530)

* ansible_playbook_python

fixes #18471

* fix tests

* removed dupe

Brian Coca authored on 2016/11/24 06:30:46
Showing 5 changed files
... ...
@@ -13,6 +13,7 @@ Ansible Changes By Release
13 13
   any user can add back via config options if they don't use those package managers or othewise avoid the errors.
14 14
 * Blocks can now have a `name` field, to aid in playbook readability.
15 15
 * default strategy is now configurable via ansible.cfg or environment variable.
16
+* Added 'ansible_playbook_python' which contains 'current python executable', it can be blank in some cases in which Ansible is not invoked via the standard CLI (sys.executable limitation).
16 17
 
17 18
 ###Deprecations:
18 19
 * Specifying --tags (or --skip-tags) multiple times on the command line
... ...
@@ -681,6 +681,8 @@ period, without the rest of the domain.
681 681
 .. versionadded:: 2.2
682 682
 ``ansible_play_batch`` is available as a list of hostnames that are in scope for the current 'batch' of the play. The batch size is defined by ``serial``, when not set it is equivalent to the whole play (making it the same as ``ansible_play_hosts``).
683 683
 
684
+.. versionadded:: 2.3
685
+``ansible_playbook_python`` is the path to the python executable used to invoke the Ansible command line tool.
684 686
 
685 687
 These vars may be useful for filling out templates with multiple hostnames or for injecting the list into the rules for a load balancer.
686 688
 
... ...
@@ -694,7 +694,7 @@ class ActionBase(with_metaclass(ABCMeta, object)):
694 694
                         pass
695 695
 
696 696
                 # remove some KNOWN keys
697
-                for hard in ['ansible_rsync_path']:
697
+                for hard in ['ansible_rsync_path', 'ansible_playbook_python']:
698 698
                     if hard in fact_keys:
699 699
                         remove_keys.add(hard)
700 700
 
... ...
@@ -20,6 +20,7 @@ from __future__ import (absolute_import, division, print_function)
20 20
 __metaclass__ = type
21 21
 
22 22
 import os
23
+import sys
23 24
 
24 25
 from collections import defaultdict, MutableMapping
25 26
 
... ...
@@ -392,6 +393,7 @@ class VariableManager:
392 392
 
393 393
         variables = dict()
394 394
         variables['playbook_dir'] = loader.get_basedir()
395
+        variables['ansible_playbook_python'] = sys.executable
395 396
 
396 397
         if host:
397 398
             variables['group_names'] = sorted([group.name for group in host.get_groups() if group.name != 'all'])
... ...
@@ -47,14 +47,11 @@ class TestVariableManager(unittest.TestCase):
47 47
 
48 48
         v = VariableManager()
49 49
         vars = v.get_vars(loader=fake_loader, use_cache=False)
50
-        if 'omit' in vars:
51
-            del vars['omit']
52
-        if 'vars' in vars:
53
-            del vars['vars']
54
-        if 'ansible_version' in vars:
55
-            del vars['ansible_version']
56
-        if 'ansible_check_mode' in vars:
57
-            del vars['ansible_check_mode']
50
+
51
+        #FIXME: not sure why we remove all and only test playbook_dir
52
+        for remove in ['omit', 'vars', 'ansible_version', 'ansible_check_mode', 'ansible_playbook_python']:
53
+            if remove in vars:
54
+                del vars[remove]
58 55
 
59 56
         self.assertEqual(vars, dict(playbook_dir='.'))
60 57