From 4add72310764d1f64a6a60eef89c72736f1528c5 Mon Sep 17 00:00:00 2001 From: sommersoft Date: Wed, 17 Mar 2021 12:52:51 -0500 Subject: [PATCH] ansible-pull: Run All Playbooks When Multiple Are Supplied (#73172) * ansible-pull: run all playbooks when multiple are supplied * add test for ansible-pull with multiple playbooks supplied from cli * add changelog fragment --- .../72708_ansible_pull_multiple_playbooks.yml | 2 ++ lib/ansible/cli/pull.py | 19 +++++++++++++------ .../pull-integration-test/multi_play_1.yml | 6 ++++++ .../pull-integration-test/multi_play_2.yml | 6 ++++++ test/integration/targets/pull/runme.sh | 18 ++++++++++++++++++ 5 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 changelogs/fragments/72708_ansible_pull_multiple_playbooks.yml create mode 100644 test/integration/targets/pull/pull-integration-test/multi_play_1.yml create mode 100644 test/integration/targets/pull/pull-integration-test/multi_play_2.yml diff --git a/changelogs/fragments/72708_ansible_pull_multiple_playbooks.yml b/changelogs/fragments/72708_ansible_pull_multiple_playbooks.yml new file mode 100644 index 00000000000..14a155937a9 --- /dev/null +++ b/changelogs/fragments/72708_ansible_pull_multiple_playbooks.yml @@ -0,0 +1,2 @@ +bugfixes: +- ansible-pull - Run all playbooks that when multiple are supplied via the command line (https://github.com/ansible/ansible/issues/72708) \ No newline at end of file diff --git a/lib/ansible/cli/pull.py b/lib/ansible/cli/pull.py index 55b5a2b0fd8..e3a3d43565c 100644 --- a/lib/ansible/cli/pull.py +++ b/lib/ansible/cli/pull.py @@ -311,19 +311,26 @@ class PullCLI(CLI): @staticmethod def select_playbook(path): playbook = None + errors = [] if context.CLIARGS['args'] and context.CLIARGS['args'][0] is not None: - playbook = os.path.join(path, context.CLIARGS['args'][0]) - rc = PullCLI.try_playbook(playbook) - if rc != 0: - display.warning("%s: %s" % (playbook, PullCLI.PLAYBOOK_ERRORS[rc])) - return None + playbooks = [] + for book in context.CLIARGS['args']: + book_path = os.path.join(path, book) + rc = PullCLI.try_playbook(book_path) + if rc != 0: + errors.append("%s: %s" % (book_path, PullCLI.PLAYBOOK_ERRORS[rc])) + continue + playbooks.append(book_path) + if 0 < len(errors): + display.warning("\n".join(errors)) + elif len(playbooks) == len(context.CLIARGS['args']): + playbook = " ".join(playbooks) return playbook else: fqdn = socket.getfqdn() hostpb = os.path.join(path, fqdn + '.yml') shorthostpb = os.path.join(path, fqdn.split('.')[0] + '.yml') localpb = os.path.join(path, PullCLI.DEFAULT_PLAYBOOK) - errors = [] for pb in [hostpb, shorthostpb, localpb]: rc = PullCLI.try_playbook(pb) if rc == 0: diff --git a/test/integration/targets/pull/pull-integration-test/multi_play_1.yml b/test/integration/targets/pull/pull-integration-test/multi_play_1.yml new file mode 100644 index 00000000000..0ec0da6b6d2 --- /dev/null +++ b/test/integration/targets/pull/pull-integration-test/multi_play_1.yml @@ -0,0 +1,6 @@ +- name: test multiple playbooks for ansible-pull + hosts: all + gather_facts: False + tasks: + - name: debug output + debug: msg="test multi_play_1" diff --git a/test/integration/targets/pull/pull-integration-test/multi_play_2.yml b/test/integration/targets/pull/pull-integration-test/multi_play_2.yml new file mode 100644 index 00000000000..1fe5a584339 --- /dev/null +++ b/test/integration/targets/pull/pull-integration-test/multi_play_2.yml @@ -0,0 +1,6 @@ +- name: test multiple playbooks for ansible-pull + hosts: all + gather_facts: False + tasks: + - name: debug output + debug: msg="test multi_play_2" diff --git a/test/integration/targets/pull/runme.sh b/test/integration/targets/pull/runme.sh index dcadc495e85..347971a4fd8 100755 --- a/test/integration/targets/pull/runme.sh +++ b/test/integration/targets/pull/runme.sh @@ -49,6 +49,20 @@ function pass_tests { fi } +function pass_tests_multi { + # test for https://github.com/ansible/ansible/issues/72708 + if ! grep 'test multi_play_1' "${temp_log}"; then + cat "${temp_log}" + echo "Did not run multiple playbooks" + exit 1 + fi + if ! grep 'test multi_play_2' "${temp_log}"; then + cat "${temp_log}" + echo "Did not run multiple playbooks" + exit 1 + fi +} + export ANSIBLE_INVENTORY export ANSIBLE_HOST_PATTERN_MISMATCH @@ -67,3 +81,7 @@ JSON_EXTRA_ARGS='{"docker_registries_login": [{ "docker_password": "'"${PASSWORD ANSIBLE_CONFIG='' ansible-pull -d "${pull_dir}" -U "${repo_dir}" -e "${JSON_EXTRA_ARGS}" "$@" --tags untagged,test_ev | tee "${temp_log}" 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