diff --git a/strategy/artifacting.py b/strategy/tagfilter.py similarity index 69% rename from strategy/artifacting.py rename to strategy/tagfilter.py index 9d36bda..a4d7b9c 100644 --- a/strategy/artifacting.py +++ b/strategy/tagfilter.py @@ -13,6 +13,7 @@ # limitations under the License. # +import itertools import os import linear @@ -21,10 +22,17 @@ import linear class StrategyModule(linear.StrategyModule): def _queue_task(self, host, task, task_vars, play_context): """Wipe the notification system and return for config tasks.""" - task.notify = None - skip_tags = os.environ.get('OS_ANSIBLE_SKIP_TAGS', 'config') - skip_tags = skip_tags.split(',') - if any([True for i in skip_tags if i in task.tags]): + skip_handlers = task_vars.get('skip_handlers', True) + if skip_handlers: + task.notify = None + skip_tags = task_vars.get('skip_tags') + if skip_tags: + if not hasattr(skip_tags, '__iter__'): + skip_tags = (skip_tags,) + else: + skip_tags = () + if any([True for (i, j) in itertools.product(skip_tags, task.tags) + if i in j]): return else: return super(StrategyModule, self)._queue_task( diff --git a/tests/test-strategy-tagfilter.yml b/tests/test-strategy-tagfilter.yml new file mode 100644 index 0000000..eb0737f --- /dev/null +++ b/tests/test-strategy-tagfilter.yml @@ -0,0 +1,125 @@ +--- +# Copyright 2017, Logan Vig +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +- name: Test the tagfilter execution strategy with a list of skip tags + hosts: localhost + strategy: tagfilter + gather_facts: no + vars: + skip_tags: + - skipit + - tagskip + tasks: + - name: Test skipped task + debug: + msg: "This task is skipped" + register: skipped_task + tags: + # Multiple tags specified before the skip tag to make sure the .product() + # loop in the strategy is working properly. (ie. ensure each element in + # the skip_tags list is being checked against each element in this tag + # list.) + - test-tag1 + - test-tag2 + - test-tag3 + - test-tag4 + - test-skipit + - name: Test unskipped task + command: /bin/true + register: run_task + notify: Skipped Handler + tags: + - test-runit + handlers: + - name: Skipped Handler + debug: + msg: "This handler is always skipped" + register: skipped_handler + post_tasks: + - name: Check task run states + assert: + that: + - "skipped_task is not defined" + - "run_task | changed" + - "skipped_handler is not defined" + +- name: Test the tagfilter execution strategy with a string skip tag + hosts: localhost + strategy: tagfilter + gather_facts: no + vars: + skip_tags: skipit + tasks: + - name: Test skipped task + debug: + msg: "This task is skipped" + register: skipped_task + tags: + - test-skipit + - name: Test unskipped task + command: /bin/true + register: run_task + notify: Skipped Handler + tags: + - test-runit + handlers: + - name: Skipped Handler + debug: + msg: "This handler is always skipped" + register: skipped_handler + post_tasks: + - name: Check task run states + assert: + that: + - "skipped_task is not defined" + - "run_task | changed" + - "skipped_handler is not defined" + +- name: Test the tagfilter execution strategy with handlers enabled + hosts: localhost + strategy: tagfilter + gather_facts: no + vars: + skip_handlers: False + skip_tags: skipit + tasks: + - name: Test skipped task + command: /bin/true + register: skipped_task + notify: Skipped Handler + tags: + - test-skipit + - name: Test unskipped task + command: /bin/true + register: run_task + notify: Run Handler + tags: + - test-runit + handlers: + - name: Skipped Handler + debug: + msg: "This handler is always skipped" + register: skipped_handler + - name: Run Handler + command: /bin/true + register: run_handler + post_tasks: + - name: Check task run states + assert: + that: + - "skipped_task is not defined" + - "run_task | changed" + - "skipped_handler is not defined" + - "run_handler | changed" diff --git a/tests/test.yml b/tests/test.yml index f79b6cb..3f0389f 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -17,3 +17,5 @@ - include: test-config_template.yml - include: test-filters.yml + +- include: test-strategy-tagfilter.yml diff --git a/tox.ini b/tox.ini index 6a8643c..e4909ad 100644 --- a/tox.ini +++ b/tox.ini @@ -32,6 +32,7 @@ setenv = ANSIBLE_CONNECTION_PLUGINS={toxinidir}/connection ANSIBLE_FILTER_PLUGINS={toxinidir}/filter ANSIBLE_LOOKUP_PLUGINS={toxinidir}/lookup + ANSIBLE_STRATEGY_PLUGINS={toxinidir}/strategy ANSIBLE_LIBRARY={toxinidir}/library