From c168698d196d4c86bd83f0169544dc444e5040aa Mon Sep 17 00:00:00 2001 From: Marcin Stolarek Date: Wed, 1 Feb 2023 13:11:02 +0100 Subject: [PATCH 1/2] Add support for skip_tags --- etc/tasks.yaml | 9 +++++++++ source/modules/configs/schema.py | 4 ++++ source/modules/runners/run.py | 12 ++++++++++-- tests/02a-checkrun.sh | 1 + 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/etc/tasks.yaml b/etc/tasks.yaml index cb3f1922..a464c1fe 100644 --- a/etc/tasks.yaml +++ b/etc/tasks.yaml @@ -94,6 +94,15 @@ tasks: tags: - tag_false + - name: skip_task_tagged_task_false + play_items: + - run_bin + allowed_for: + - acl_group: root_on_testing_in_testInfra_testInfra2 + allow_limit: False + skip_tags: + - tag_false + - name: task_skipping play_items: - run_skippable diff --git a/source/modules/configs/schema.py b/source/modules/configs/schema.py index c20ec921..9fcc81b5 100644 --- a/source/modules/configs/schema.py +++ b/source/modules/configs/schema.py @@ -194,6 +194,10 @@ "tags": { "type": "list", "required": False + }, + "skip_tags": { + "type": "list", + "required": False } } } diff --git a/source/modules/runners/run.py b/source/modules/runners/run.py index c1d29765..f8e99dcc 100644 --- a/source/modules/runners/run.py +++ b/source/modules/runners/run.py @@ -182,6 +182,7 @@ def run_playitem(self, config: dict, options: dict, inventory: str, lockpath: st def get_tags_for_task(config: dict, options: dict): """Function to get task's tags""" tags = [] + skip_tags = [] for task in config["tasks"]["tasks"]: if task["name"] == options["task"]: tags = task.get("tags", []) @@ -189,12 +190,16 @@ def get_tags_for_task(config: dict, options: dict): if options["dry_mode"]: tags.append("ansible_deployer_dry_mode") - return tags + for task in config["tasks"]["tasks"]: + if task["name"] == options["task"]: + skip_tags = task.get("skip_tags", []) + + return tags, skip_tags @staticmethod def construct_command(playitem: str, inventory: str, config: dict, options: dict): """Create final ansible command from available variables""" - tags = Runners.get_tags_for_task(config, options) + tags, skip_tags = Runners.get_tags_for_task(config, options) if "runner" in playitem and playitem["runner"] == "py.test": command = ["py.test", "--ansible-inventory", inventory] @@ -213,6 +218,9 @@ def construct_command(playitem: str, inventory: str, config: dict, options: dict if tags: command.append("-t") command.append(",".join(tags)) + if skip_tags: + command.append("--skip-tags") + command.append(",".join(skip_tags)) if options["check_mode"]: command.append("-C") command_env=dict(os.environ, ANSIBLE_STDOUT_CALLBACK="yaml", ANSIBLE_NOCOWS="1", diff --git a/tests/02a-checkrun.sh b/tests/02a-checkrun.sh index 27b2be84..32dd2497 100755 --- a/tests/02a-checkrun.sh +++ b/tests/02a-checkrun.sh @@ -9,6 +9,7 @@ check_run_ok "ansible-deployer run --task task_exec_bin_true --stage prod --infr check_run_ok "ansible-deployer run --task task_with_limit --stage testing --infrastructure testInfra --limit testHost1" check_run_ok "ansible-deployer run --task tagged_task_true --stage testing --infrastructure testInfra" check_run_ok "ansible-deployer verify --task task_exec_bin_true --stage prod --infrastructure testInfra" +check_run_ok "ansible-deployer run --task skip_task_tagged_task_false --stage testing --infrastructure testInfra" echo -e " ___ ____ _ _\n / _ \___ \ __ _ ___| |__ ___ ___| | ___ __ _ _ _ __\n | | | |__) / _\` | _____ / __| '_ \ / _ \/ __| |/ / '__| | | | '_ \ \n | |_| / __/ (_| | |_____| | (__| | | | __/ (__| <| | | |_| | | | |\n \___/_____\__,_| \___|_| |_|\___|\___|_|\_\_| \__,_|_| |_|\n \n _ _ _ _ _ _\n (_)_ ____ ____ _| (_) __| | ___ _ __ | |_(_) ___ _ __ ___\n | | '_ \ \ / / _\` | | |/ _\` | / _ \| '_ \| __| |/ _ \| '_ \/ __|\n | | | | \ V / (_| | | | (_| | | (_) | |_) | |_| | (_) | | | \__ \ \n |_|_| |_|\_/ \__,_|_|_|\__,_| \___/| .__/ \__|_|\___/|_| |_|___/\n |_|\n" # Non-existent option values From a3d06a4c21d556c397f1d3e1bd94a55eea620b57 Mon Sep 17 00:00:00 2001 From: Marcin Stolarek Date: Wed, 1 Feb 2023 13:39:00 +0100 Subject: [PATCH 2/2] Add a check for failing task - /bin/false is a failing task --- tests/02a-checkrun.sh | 1 + tests/testing_lib.sh | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/tests/02a-checkrun.sh b/tests/02a-checkrun.sh index 32dd2497..0bcd1638 100755 --- a/tests/02a-checkrun.sh +++ b/tests/02a-checkrun.sh @@ -10,6 +10,7 @@ check_run_ok "ansible-deployer run --task task_with_limit --stage testing --infr check_run_ok "ansible-deployer run --task tagged_task_true --stage testing --infrastructure testInfra" check_run_ok "ansible-deployer verify --task task_exec_bin_true --stage prod --infrastructure testInfra" check_run_ok "ansible-deployer run --task skip_task_tagged_task_false --stage testing --infrastructure testInfra" +check_run_fail "ansible-deployer run --task tagged_task_false --stage testing --infrastructure testInfra" echo -e " ___ ____ _ _\n / _ \___ \ __ _ ___| |__ ___ ___| | ___ __ _ _ _ __\n | | | |__) / _\` | _____ / __| '_ \ / _ \/ __| |/ / '__| | | | '_ \ \n | |_| / __/ (_| | |_____| | (__| | | | __/ (__| <| | | |_| | | | |\n \___/_____\__,_| \___|_| |_|\___|\___|_|\_\_| \__,_|_| |_|\n \n _ _ _ _ _ _\n (_)_ ____ ____ _| (_) __| | ___ _ __ | |_(_) ___ _ __ ___\n | | '_ \ \ / / _\` | | |/ _\` | / _ \| '_ \| __| |/ _ \| '_ \/ __|\n | | | | \ V / (_| | | | (_| | | (_) | |_) | |_| | (_) | | | \__ \ \n |_|_| |_|\_/ \__,_|_|_|\__,_| \___/| .__/ \__|_|\___/|_| |_|___/\n |_|\n" # Non-existent option values diff --git a/tests/testing_lib.sh b/tests/testing_lib.sh index 2c8a7fe7..6af63746 100644 --- a/tests/testing_lib.sh +++ b/tests/testing_lib.sh @@ -15,6 +15,22 @@ check_run_ok() { fi } +check_run_fail() { + CMD=$1 + echo "Check: $CMD" + $CMD + if [ $? -eq 0 ] + then + echo "FAILED(Expected failure got return code of 0): ${CMD}" + echo "$CMD" + eval "$CMD" + exit 1 + else + echo "OK - failed as expected" + fi +} + + check_message_in_output() { CMD=$1 EXPTEXT=$2