From de07dc85f500474fd1a7fd0cbe24bb6d6e95843b Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Mon, 27 Nov 2023 09:51:19 -0500 Subject: [PATCH] ansible-pull: expand destinantion directoy to avoid purgin in / (#82030) (#82223) * expand destinantion directoy to avoid purgin in / bad things could happen and help alone is not enough (cherry picked from commit 8825e60add98f3400497928105a0747cbfa2346f) --- changelogs/fragments/pull_unfrack_dest.yml | 2 ++ lib/ansible/cli/pull.py | 7 ++++--- test/integration/targets/ansible-pull/runme.sh | 5 ++++- 3 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/pull_unfrack_dest.yml diff --git a/changelogs/fragments/pull_unfrack_dest.yml b/changelogs/fragments/pull_unfrack_dest.yml new file mode 100644 index 00000000000..71724232033 --- /dev/null +++ b/changelogs/fragments/pull_unfrack_dest.yml @@ -0,0 +1,2 @@ +bugfixes: + - ansible-pull now will expand relative paths for the ``-d|--directory`` option is now expanded before use. diff --git a/lib/ansible/cli/pull.py b/lib/ansible/cli/pull.py index dc8f055b44d..470849891ee 100755 --- a/lib/ansible/cli/pull.py +++ b/lib/ansible/cli/pull.py @@ -29,6 +29,7 @@ from ansible.plugins.loader import module_loader from ansible.utils.cmd_functions import run_cmd from ansible.utils.display import Display + display = Display() @@ -102,8 +103,8 @@ class PullCLI(CLI): 'This is a useful way to disperse git requests') self.parser.add_argument('-f', '--force', dest='force', default=False, action='store_true', help='run the playbook even if the repository could not be updated') - self.parser.add_argument('-d', '--directory', dest='dest', default=None, - help='absolute path of repository checkout directory (relative paths are not supported)') + self.parser.add_argument('-d', '--directory', dest='dest', default=None, type=opt_help.unfrack_path(), + help='path to the directory to which Ansible will checkout the repository.') self.parser.add_argument('-U', '--url', dest='url', default=None, help='URL of the playbook repository') self.parser.add_argument('--full', dest='fullclone', action='store_true', help='Do a full clone, instead of a shallow one.') self.parser.add_argument('-C', '--checkout', dest='checkout', @@ -134,7 +135,6 @@ class PullCLI(CLI): hostname = socket.getfqdn() # use a hostname dependent directory, in case of $HOME on nfs options.dest = os.path.join(C.ANSIBLE_HOME, 'pull', hostname) - options.dest = os.path.expandvars(os.path.expanduser(options.dest)) if os.path.exists(options.dest) and not os.path.isdir(options.dest): raise AnsibleOptionsError("%s is not a valid or accessible directory." % options.dest) @@ -307,6 +307,7 @@ class PullCLI(CLI): if context.CLIARGS['purge']: os.chdir('/') try: + display.debug("removing: %s" % context.CLIARGS['dest']) shutil.rmtree(context.CLIARGS['dest']) except Exception as e: display.error(u"Failed to remove %s: %s" % (context.CLIARGS['dest'], to_text(e))) diff --git a/test/integration/targets/ansible-pull/runme.sh b/test/integration/targets/ansible-pull/runme.sh index 347971a4fd8..582e809929d 100755 --- a/test/integration/targets/ansible-pull/runme.sh +++ b/test/integration/targets/ansible-pull/runme.sh @@ -84,4 +84,7 @@ pass_tests ANSIBLE_CONFIG='' ansible-pull -d "${pull_dir}" -U "${repo_dir}" "$@" multi_play_1.yml multi_play_2.yml | tee "${temp_log}" -pass_tests_multi \ No newline at end of file +pass_tests_multi + +# fail if we try do delete /var/tmp +ANSIBLE_CONFIG='' ansible-pull -d var/tmp -U "${repo_dir}" --purge "$@"