mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-29 08:01:24 -07:00
Basic support for tagging tasks and selecting a subset of tasks to run with --tags.
This commit is contained in:
parent
fd7e96d33e
commit
83f23ef861
5 changed files with 71 additions and 8 deletions
|
@ -61,7 +61,8 @@ class PlayBook(object):
|
|||
stats = None,
|
||||
sudo = False,
|
||||
sudo_user = C.DEFAULT_SUDO_USER,
|
||||
extra_vars = None):
|
||||
extra_vars = None,
|
||||
only_tags = None):
|
||||
|
||||
"""
|
||||
playbook: path to a playbook file
|
||||
|
@ -87,7 +88,10 @@ class PlayBook(object):
|
|||
|
||||
if extra_vars is None:
|
||||
extra_vars = {}
|
||||
|
||||
|
||||
if only_tags is None:
|
||||
only_tags = [ 'all' ]
|
||||
|
||||
self.module_path = module_path
|
||||
self.forks = forks
|
||||
self.timeout = timeout
|
||||
|
@ -105,6 +109,7 @@ class PlayBook(object):
|
|||
self.extra_vars = extra_vars
|
||||
self.global_vars = {}
|
||||
self.private_key_file = private_key_file
|
||||
self.only_tags = only_tags
|
||||
|
||||
self.inventory = ansible.inventory.Inventory(host_list)
|
||||
|
||||
|
@ -286,9 +291,17 @@ class PlayBook(object):
|
|||
if play.vars_files and len(play.vars_files) > 0:
|
||||
rc = self._do_setup_step(play, play.vars_files)
|
||||
|
||||
# run all the top level tasks, these get run on every node
|
||||
for task in play.tasks():
|
||||
self._run_task(play, task, False)
|
||||
|
||||
# only run the task if the requested tags match
|
||||
should_run = False
|
||||
for x in self.only_tags:
|
||||
for y in task.tags:
|
||||
if (x==y):
|
||||
should_run = True
|
||||
break
|
||||
if should_run:
|
||||
self._run_task(play, task, False)
|
||||
|
||||
# run notify actions
|
||||
for handler in play.handlers():
|
||||
|
|
|
@ -26,8 +26,10 @@ import os
|
|||
class Play(object):
|
||||
|
||||
__slots__ = [
|
||||
'hosts', 'name', 'vars', 'vars_prompt', 'vars_files', 'handlers', 'remote_user', 'remote_port',
|
||||
'sudo', 'sudo_user', 'transport', 'playbook', '_ds', '_handlers', '_tasks'
|
||||
'hosts', 'name', 'vars', 'vars_prompt', 'vars_files',
|
||||
'handlers', 'remote_user', 'remote_port',
|
||||
'sudo', 'sudo_user', 'transport', 'playbook',
|
||||
'_ds', '_handlers', '_tasks'
|
||||
]
|
||||
|
||||
# *************************************************
|
||||
|
@ -37,6 +39,7 @@ class Play(object):
|
|||
|
||||
# TODO: more error handling
|
||||
|
||||
|
||||
hosts = ds.get('hosts')
|
||||
if hosts is None:
|
||||
raise errors.AnsibleError('hosts declaration is required')
|
||||
|
|
|
@ -24,7 +24,8 @@ class Task(object):
|
|||
|
||||
__slots__ = [
|
||||
'name', 'action', 'only_if', 'async_seconds', 'async_poll_interval',
|
||||
'notify', 'module_name', 'module_args', 'module_vars', 'play', 'notified_by',
|
||||
'notify', 'module_name', 'module_args', 'module_vars',
|
||||
'play', 'notified_by', 'tags'
|
||||
]
|
||||
|
||||
def __init__(self, play, ds, module_vars=None):
|
||||
|
@ -38,6 +39,8 @@ class Task(object):
|
|||
self.play = play
|
||||
self.name = ds.get('name', None)
|
||||
self.action = ds.get('action', '')
|
||||
self.tags = [ 'all' ]
|
||||
|
||||
self.notified_by = []
|
||||
|
||||
if self.name is None:
|
||||
|
@ -67,4 +70,15 @@ class Task(object):
|
|||
if 'first_available_file' in ds:
|
||||
self.module_vars['first_available_file'] = ds.get('first_available_file')
|
||||
|
||||
# tags allow certain parts of a playbook to be run without
|
||||
# running the whole playbook
|
||||
apply_tags = ds.get('tags', None)
|
||||
if apply_tags is not None:
|
||||
if type(apply_tags) in [ str, unicode ]:
|
||||
self.tags.append(apply_tags)
|
||||
elif type(apply_tags) == list:
|
||||
self.tags.extend(apply_tags)
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue