Make adding tags to a playbook work as shorthand to tagging all tasks in the play.

This commit is contained in:
Michael DeHaan 2012-07-12 00:58:48 -04:00
parent cf313cde96
commit 611e5b0c90
2 changed files with 32 additions and 4 deletions

View file

@ -6,14 +6,27 @@
# try this with:
# --tags foo
# --tags bar
# --tags extra
#
# note the include syntax to tag all tasks included below
# it is a short hand over adding "tag:" to each task entry
# the value of a 'tags:' element can be a string or list
# of tag names. Variables are not usable in tag names.
- name: example play one
hosts: all
user: root
# any tags applied to the play are shorthand to applying
# the tag to all tasks in it. Here, each task is given
# the tag extra
tags:
- extra
tasks:
# this task will run if you don't specify any tags,
# if you specify 'foo' or if you specify 'extra'
- name: hi
tags: foo
action: shell echo "first task ran"
@ -23,7 +36,9 @@
user: root
tasks:
- name: hi
tags: bar
tags:
- bar
action: shell echo "second task ran"
- include: tasks/base.yml tags=base

View file

@ -29,7 +29,7 @@ class Play(object):
'hosts', 'name', 'vars', 'vars_prompt', 'vars_files',
'handlers', 'remote_user', 'remote_port',
'sudo', 'sudo_user', 'transport', 'playbook',
'_ds', '_handlers', '_tasks'
'tags', '_ds', '_handlers', '_tasks'
]
# *************************************************
@ -62,9 +62,17 @@ class Play(object):
self.sudo = ds.get('sudo', self.playbook.sudo)
self.sudo_user = ds.get('sudo_user', self.playbook.sudo_user)
self.transport = ds.get('connection', self.playbook.transport)
self.tags = ds.get('tags', None)
self._tasks = self._load_tasks(self._ds, 'tasks')
self._handlers = self._load_tasks(self._ds, 'handlers')
if self.tags is None:
self.tags = []
elif type(self.tags) in [ str, unicode ]:
self.tags = [ self.tags ]
elif type(self.tags) != list:
self.tags = []
if self.sudo_user != 'root':
self.sudo = True
@ -99,6 +107,11 @@ class Play(object):
mv = task_vars.copy()
mv['item'] = item
results.append(Task(self,y,module_vars=mv))
for x in results:
if self.tags is not None:
x.tags.extend(self.tags)
return results
# *************************************************