summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Davis <nitzmahone@users.noreply.github.com>2019-10-08 17:34:15 -0700
committerToshio Kuratomi <a.badger@gmail.com>2019-10-09 10:44:50 -0700
commit6e0eafa3a784b4e71019aa579fc0471123d1dbd8 (patch)
tree1e018ed5262a47b7ce5551e7117f7ad41462dff6
parentbcd118c771dad85aea90f6844e9a7686d785210a (diff)
downloadansible-6e0eafa3a784b4e71019aa579fc0471123d1dbd8.tar.gz
add ANSIBLE_PLAYBOOK_DIR envvar support (#63220)
* add ANSIBLE_PLAYBOOK_DIR envvar support * allows `ANSIBLE_PLAYBOOK_DIR` envvar as a fallback on CLI types that support `--playbook-dir`. This should have been implemented with #59464, but was missed due to an oversight. * added basic integration test * make first-class PLAYBOOK_DIR config entry * update changelog (cherry picked from commit fd229dcbb599f3b5de889f90112bba35d0da31f2)
-rw-r--r--changelogs/fragments/59464-playbook-dir-envvar.yml2
-rw-r--r--lib/ansible/cli/arguments/option_helpers.py2
-rw-r--r--lib/ansible/config/base.yml8
-rw-r--r--test/integration/targets/ansible/playbookdir_cfg.ini2
-rwxr-xr-xtest/integration/targets/ansible/runme.sh9
5 files changed, 22 insertions, 1 deletions
diff --git a/changelogs/fragments/59464-playbook-dir-envvar.yml b/changelogs/fragments/59464-playbook-dir-envvar.yml
new file mode 100644
index 0000000000..892c78148a
--- /dev/null
+++ b/changelogs/fragments/59464-playbook-dir-envvar.yml
@@ -0,0 +1,2 @@
+bugfixes:
+- CLI - the `ANSIBLE_PLAYBOOK_DIR` envvar or `playbook_dir` config can now substitute for the --playbook-dir arg on CLIs that support it (https://github.com/ansible/ansible/issues/59464)
diff --git a/lib/ansible/cli/arguments/option_helpers.py b/lib/ansible/cli/arguments/option_helpers.py
index bafa92313f..cf521a4891 100644
--- a/lib/ansible/cli/arguments/option_helpers.py
+++ b/lib/ansible/cli/arguments/option_helpers.py
@@ -223,7 +223,7 @@ def add_async_options(parser):
def add_basedir_options(parser):
"""Add options for commands which can set a playbook basedir"""
- parser.add_argument('--playbook-dir', default=None, dest='basedir', action='store',
+ parser.add_argument('--playbook-dir', default=C.config.get_config_value('PLAYBOOK_DIR'), dest='basedir', action='store',
help="Since this tool does not use playbooks, use this as a substitute playbook directory."
"This sets the relative path for many features including roles/ group_vars/ etc.")
diff --git a/lib/ansible/config/base.yml b/lib/ansible/config/base.yml
index 40c554537f..2a5c7941fc 100644
--- a/lib/ansible/config/base.yml
+++ b/lib/ansible/config/base.yml
@@ -1668,6 +1668,14 @@ PERSISTENT_COMMAND_TIMEOUT:
ini:
- {key: command_timeout, section: persistent_connection}
type: int
+PLAYBOOK_DIR:
+ name: playbook dir override for non-playbook CLIs (ala --playbook-dir)
+ version_added: "2.9"
+ description:
+ - A number of non-playbook CLIs have a ``--playbook-dir`` argument; this sets the default value for it.
+ env: [{name: ANSIBLE_PLAYBOOK_DIR}]
+ ini: [{key: playbook_dir, section: defaults}]
+ type: path
PLAYBOOK_VARS_ROOT:
name: playbook vars files root
default: top
diff --git a/test/integration/targets/ansible/playbookdir_cfg.ini b/test/integration/targets/ansible/playbookdir_cfg.ini
new file mode 100644
index 0000000000..f4bf8af895
--- /dev/null
+++ b/test/integration/targets/ansible/playbookdir_cfg.ini
@@ -0,0 +1,2 @@
+[defaults]
+playbook_dir = /tmp
diff --git a/test/integration/targets/ansible/runme.sh b/test/integration/targets/ansible/runme.sh
index 515058e71e..d790e481bf 100755
--- a/test/integration/targets/ansible/runme.sh
+++ b/test/integration/targets/ansible/runme.sh
@@ -18,6 +18,15 @@ ansible-config view -c ./ansible-non-existent.cfg 2> err1.txt || grep -Eq 'ERROR
ansible-config view -c ./no-extension 2> err2.txt || grep -q 'Unsupported configuration file extension' err2.txt || (cat err2.txt; rm -f err*.txt; exit 1)
rm -f err*.txt
+# test setting playbook_dir via envvar
+ANSIBLE_PLAYBOOK_DIR=/tmp ansible localhost -m debug -a var=playbook_dir | grep '"playbook_dir": "/tmp"'
+
+# test setting playbook_dir via cmdline
+ansible localhost -m debug -a var=playbook_dir --playbook-dir=/tmp | grep '"playbook_dir": "/tmp"'
+
+# test setting playbook dir via ansible.cfg
+ANSIBLE_CONFIG=./playbookdir_cfg.ini ansible localhost -m debug -a var=playbook_dir | grep '"playbook_dir": "/tmp"'
+
# Test that no tmp dirs are left behind when running ansible-config
TMP_DIR=~/.ansible/tmptest
if [[ -d "$TMP_DIR" ]]; then