From 778c983ef9f9cad3d8fd47c8a4efcceeb45fa3b2 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 23 Nov 2016 16:30:46 -0500 Subject: [PATCH] ansible_playbook_python (#18530) * ansible_playbook_python fixes #18471 * fix tests * removed dupe --- CHANGELOG.md | 1 + docsite/rst/playbooks_variables.rst | 2 ++ lib/ansible/plugins/action/__init__.py | 2 +- lib/ansible/vars/__init__.py | 2 ++ test/units/vars/test_variable_manager.py | 13 +++++-------- 5 files changed, 11 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index add858cf147..b14552b34de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ Ansible Changes By Release any user can add back via config options if they don't use those package managers or othewise avoid the errors. * Blocks can now have a `name` field, to aid in playbook readability. * default strategy is now configurable via ansible.cfg or environment variable. +* 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). ###Deprecations: * Specifying --tags (or --skip-tags) multiple times on the command line diff --git a/docsite/rst/playbooks_variables.rst b/docsite/rst/playbooks_variables.rst index 483d43abc0f..4f6e770bfa6 100644 --- a/docsite/rst/playbooks_variables.rst +++ b/docsite/rst/playbooks_variables.rst @@ -681,6 +681,8 @@ period, without the rest of the domain. .. versionadded:: 2.2 ``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``). +.. versionadded:: 2.3 +``ansible_playbook_python`` is the path to the python executable used to invoke the Ansible command line tool. These vars may be useful for filling out templates with multiple hostnames or for injecting the list into the rules for a load balancer. diff --git a/lib/ansible/plugins/action/__init__.py b/lib/ansible/plugins/action/__init__.py index 509d8b3382a..e0adba27153 100644 --- a/lib/ansible/plugins/action/__init__.py +++ b/lib/ansible/plugins/action/__init__.py @@ -694,7 +694,7 @@ class ActionBase(with_metaclass(ABCMeta, object)): pass # remove some KNOWN keys - for hard in ['ansible_rsync_path']: + for hard in ['ansible_rsync_path', 'ansible_playbook_python']: if hard in fact_keys: remove_keys.add(hard) diff --git a/lib/ansible/vars/__init__.py b/lib/ansible/vars/__init__.py index eba7916ebba..374065eed37 100644 --- a/lib/ansible/vars/__init__.py +++ b/lib/ansible/vars/__init__.py @@ -20,6 +20,7 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type import os +import sys from collections import defaultdict, MutableMapping @@ -392,6 +393,7 @@ class VariableManager: variables = dict() variables['playbook_dir'] = loader.get_basedir() + variables['ansible_playbook_python'] = sys.executable if host: variables['group_names'] = sorted([group.name for group in host.get_groups() if group.name != 'all']) diff --git a/test/units/vars/test_variable_manager.py b/test/units/vars/test_variable_manager.py index 3c3c8e9c625..659ca72c850 100644 --- a/test/units/vars/test_variable_manager.py +++ b/test/units/vars/test_variable_manager.py @@ -47,14 +47,11 @@ class TestVariableManager(unittest.TestCase): v = VariableManager() vars = v.get_vars(loader=fake_loader, use_cache=False) - if 'omit' in vars: - del vars['omit'] - if 'vars' in vars: - del vars['vars'] - if 'ansible_version' in vars: - del vars['ansible_version'] - if 'ansible_check_mode' in vars: - del vars['ansible_check_mode'] + + #FIXME: not sure why we remove all and only test playbook_dir + for remove in ['omit', 'vars', 'ansible_version', 'ansible_check_mode', 'ansible_playbook_python']: + if remove in vars: + del vars[remove] self.assertEqual(vars, dict(playbook_dir='.'))