Allow include statements from plays to specify tags (see tags.yml example file).
Also be smart and don't run a play at all if no tasks in the play match any of the tags specified. This includes not running the setup actions!
This commit is contained in:
parent
83f23ef861
commit
969c3feb13
4 changed files with 28 additions and 11 deletions
|
@ -3,27 +3,27 @@
|
||||||
#
|
#
|
||||||
# assume: ansible-playbook tags.yml --tags foo
|
# assume: ansible-playbook tags.yml --tags foo
|
||||||
#
|
#
|
||||||
# only tags with the given tags will be run when --tags is specified
|
# try this with:
|
||||||
|
# --tags foo
|
||||||
|
# --tags bar
|
||||||
#
|
#
|
||||||
# (an include statement will also be able to set tags on all included
|
# note the include syntax to tag all tasks included below
|
||||||
# tasks at some point in the future)
|
# it is a short hand over adding "tag:" to each task entry
|
||||||
|
|
||||||
|
- name: example play one
|
||||||
- name: example play
|
|
||||||
hosts: all
|
hosts: all
|
||||||
user: root
|
user: root
|
||||||
tasks:
|
tasks:
|
||||||
- name: hi
|
- name: hi
|
||||||
tags: foo
|
tags: foo
|
||||||
action: shell echo "first play ran"
|
action: shell echo "first task ran"
|
||||||
|
|
||||||
- name: example play
|
- name: example play two
|
||||||
hosts: all
|
hosts: all
|
||||||
user: root
|
user: root
|
||||||
tasks:
|
tasks:
|
||||||
- name: hi
|
- name: hi
|
||||||
tags: bar
|
tags: bar
|
||||||
action: shell echo "second play ran"
|
action: shell echo "second task ran"
|
||||||
|
- include: tasks/base.yml tags=base
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -282,6 +282,9 @@ class PlayBook(object):
|
||||||
def _run_play(self, play):
|
def _run_play(self, play):
|
||||||
''' run a list of tasks for a given pattern, in order '''
|
''' run a list of tasks for a given pattern, in order '''
|
||||||
|
|
||||||
|
if not play.should_run(self.only_tags):
|
||||||
|
return
|
||||||
|
|
||||||
self.callbacks.on_play_start(play.name)
|
self.callbacks.on_play_start(play.name)
|
||||||
|
|
||||||
# push any variables down to the system # and get facts/ohai/other data back up
|
# push any variables down to the system # and get facts/ohai/other data back up
|
||||||
|
|
|
@ -79,6 +79,7 @@ class Play(object):
|
||||||
task_vars = self.vars.copy()
|
task_vars = self.vars.copy()
|
||||||
if 'include' in x:
|
if 'include' in x:
|
||||||
tokens = shlex.split(x['include'])
|
tokens = shlex.split(x['include'])
|
||||||
|
|
||||||
for t in tokens[1:]:
|
for t in tokens[1:]:
|
||||||
(k,v) = t.split("=", 1)
|
(k,v) = t.split("=", 1)
|
||||||
task_vars[k]=v
|
task_vars[k]=v
|
||||||
|
@ -151,6 +152,16 @@ class Play(object):
|
||||||
|
|
||||||
# *************************************************
|
# *************************************************
|
||||||
|
|
||||||
|
def should_run(self, tags):
|
||||||
|
''' does the play match any of the tags? '''
|
||||||
|
for task in self._tasks:
|
||||||
|
for task_tag in task.tags:
|
||||||
|
if task_tag in tags:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
# *************************************************
|
||||||
|
|
||||||
def _update_vars_files_for_host(self, host):
|
def _update_vars_files_for_host(self, host):
|
||||||
|
|
||||||
if not host in self.playbook.SETUP_CACHE:
|
if not host in self.playbook.SETUP_CACHE:
|
||||||
|
|
|
@ -62,6 +62,9 @@ class Task(object):
|
||||||
if len(tokens) > 1:
|
if len(tokens) > 1:
|
||||||
self.module_args = tokens[1]
|
self.module_args = tokens[1]
|
||||||
|
|
||||||
|
import_tags = []
|
||||||
|
if 'tags' in self.module_vars:
|
||||||
|
import_tags = self.module_vars['tags'].split(",")
|
||||||
|
|
||||||
self.name = utils.template(self.name, self.module_vars)
|
self.name = utils.template(self.name, self.module_vars)
|
||||||
self.action = utils.template(self.name, self.module_vars)
|
self.action = utils.template(self.name, self.module_vars)
|
||||||
|
@ -78,7 +81,7 @@ class Task(object):
|
||||||
self.tags.append(apply_tags)
|
self.tags.append(apply_tags)
|
||||||
elif type(apply_tags) == list:
|
elif type(apply_tags) == list:
|
||||||
self.tags.extend(apply_tags)
|
self.tags.extend(apply_tags)
|
||||||
|
self.tags.extend(import_tags)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue