From e3a153c4fef9a111735b2cdfd06fbf9cc3902fda Mon Sep 17 00:00:00 2001 From: LegenJCdary Date: Wed, 9 Feb 2022 15:36:00 +0100 Subject: [PATCH] Initial tag handling added --- ansible_deployer/command_line.py | 14 ++++++++++++ etc/hooks/setup_work_dir.sh | 13 +++++++++++ etc/schema/tasks.yaml | 4 ++++ etc/tasks.yaml | 38 ++++++++++++++++++++++++++++++++ tests/02-checkrun.sh | 1 + 5 files changed, 70 insertions(+) diff --git a/ansible_deployer/command_line.py b/ansible_deployer/command_line.py index 5cf3201d..044fd865 100644 --- a/ansible_deployer/command_line.py +++ b/ansible_deployer/command_line.py @@ -385,6 +385,7 @@ def run_task(config: dict, options: dict, inventory: str): Function implementing actual execution of ansible-playbook """ playbooks = get_playbooks(config, options["task"]) + tags = get_tags_for_task(config, options) if len(playbooks) < 1: logger.error("No playbooks found for requested task %s. Nothing to do.", options['task']) logger.error("Program will exit now.") @@ -395,6 +396,10 @@ def run_task(config: dict, options: dict, inventory: str): if options["limit"]: command.append("-l") command.append(options["limit"]) + if tags: + tag_string = ",".join(tags) + command.append("-t") + command.append(tag_string) logger.debug("Running '%s'.", command) try: with subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as \ @@ -499,6 +504,15 @@ def load_global_configuration(cfg_path: str): print(e, file=sys.stderr) sys.exit(51) +def get_tags_for_task(config: dict, options: dict): + """Function to get task's tags""" + tags = [] + for task in config["tasks"]["tasks"]: + if task["name"] == options["task"]: + tags = task["tags"] + + return tags + def main(): """ansible-deploy endpoint function""" global logger, conf diff --git a/etc/hooks/setup_work_dir.sh b/etc/hooks/setup_work_dir.sh index f92a3d32..17048b9f 100755 --- a/etc/hooks/setup_work_dir.sh +++ b/etc/hooks/setup_work_dir.sh @@ -8,6 +8,19 @@ cat << END > ./runBinTrue.yaml shell: "/bin/true" END +cat << END > ./runBin.yaml +- hosts: all + connection: "local" + tasks: + - name: "Run /bin/true" + shell: "/bin/true" + tags: tag_true + - name: "Run /bin/false" + shell: "/bin/false" + tags: tag_false +END + + cat << END > ./test_infra1_inv.yaml [testHosts] testHost1 diff --git a/etc/schema/tasks.yaml b/etc/schema/tasks.yaml index b379b0ed..72937d30 100644 --- a/etc/schema/tasks.yaml +++ b/etc/schema/tasks.yaml @@ -65,3 +65,7 @@ tasks: allow_limit: type: boolean required: True + tags: + type: list + required: True + diff --git a/etc/tasks.yaml b/etc/tasks.yaml index cd5305c6..a248516d 100644 --- a/etc/tasks.yaml +++ b/etc/tasks.yaml @@ -7,6 +7,8 @@ setup_hooks: play_items: - name: run_bin_true file: runBinTrue.yaml + - name: run_bin + file: runBin.yaml tasks: - name: task_exec_bin_true @@ -26,6 +28,7 @@ tasks: - prod - locked allow_limit: True + tags: [] - name: task_empty play_items: [] @@ -36,6 +39,7 @@ tasks: stages: - testing allow_limit: True + tags: [] - name: root_only_task play_items: @@ -47,6 +51,7 @@ tasks: stages: - testing allow_limit: True + tags: [] - name: non_root_task play_items: @@ -58,6 +63,7 @@ tasks: stages: - testing allow_limit: True + tags: [] - name: task_with_limit play_items: @@ -72,4 +78,36 @@ tasks: stages: - testing allow_limit: True + tags: [] + - name: tagged_task_true + play_items: + - run_bin + allowed_for: + - group: root + infra: + - name: testInfra + stages: + - testing + - name: testInfra2 + stages: + - testing + allow_limit: True + tags: + - tag_true + + - name: tagged_task_false + play_items: + - run_bin + allowed_for: + - group: root + infra: + - name: testInfra + stages: + - testing + - name: testInfra2 + stages: + - testing + allow_limit: True + tags: + - tag_false diff --git a/tests/02-checkrun.sh b/tests/02-checkrun.sh index be642842..db9cfd01 100755 --- a/tests/02-checkrun.sh +++ b/tests/02-checkrun.sh @@ -30,6 +30,7 @@ check_run_ok() { # Correct execution. check_run_ok "ansible-deployer run -t task_exec_bin_true -s prod -i testInfra" check_run_ok "ansible-deployer run -t task_with_limit -s testing -i testInfra -l testHost1" +check_run_ok "ansible-deployer run -t tagged_task_true -s testing -i testInfra" # # multiple hosts in limit check_run_ok "ansible-deployer run -t task_with_limit -s testing -i testInfra2 -l xyzHosts"