mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02: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
|
@ -35,6 +35,8 @@ def main(args):
|
||||||
parser = utils.base_parser(constants=C, usage=usage, connect_opts=True, runas_opts=True)
|
parser = utils.base_parser(constants=C, usage=usage, connect_opts=True, runas_opts=True)
|
||||||
parser.add_option('-e', '--extra-vars', dest="extra_vars", default=None,
|
parser.add_option('-e', '--extra-vars', dest="extra_vars", default=None,
|
||||||
help="set additional key=value variables from the CLI")
|
help="set additional key=value variables from the CLI")
|
||||||
|
parser.add_option('-t', '--tags', dest='tags', default='all',
|
||||||
|
help="only run plays and tasks tagged with these values")
|
||||||
|
|
||||||
options, args = parser.parse_args(args)
|
options, args = parser.parse_args(args)
|
||||||
|
|
||||||
|
@ -53,6 +55,7 @@ def main(args):
|
||||||
options.sudo = True
|
options.sudo = True
|
||||||
options.sudo_user = options.sudo_user or C.DEFAULT_SUDO_USER
|
options.sudo_user = options.sudo_user or C.DEFAULT_SUDO_USER
|
||||||
extra_vars = utils.parse_kv(options.extra_vars)
|
extra_vars = utils.parse_kv(options.extra_vars)
|
||||||
|
only_tags = options.tags.split(",")
|
||||||
|
|
||||||
# run all playbooks specified on the command line
|
# run all playbooks specified on the command line
|
||||||
for playbook in args:
|
for playbook in args:
|
||||||
|
@ -78,7 +81,8 @@ def main(args):
|
||||||
sudo_user=options.sudo_user,
|
sudo_user=options.sudo_user,
|
||||||
sudo_pass=sudopass,
|
sudo_pass=sudopass,
|
||||||
extra_vars=extra_vars,
|
extra_vars=extra_vars,
|
||||||
private_key_file=options.private_key_file
|
private_key_file=options.private_key_file,
|
||||||
|
only_tags=only_tags,
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
|
|
||||||
|
|
29
examples/playbooks/tags.yml
Normal file
29
examples/playbooks/tags.yml
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
---
|
||||||
|
# tags allow us to run all of a playbook or part of it.
|
||||||
|
#
|
||||||
|
# assume: ansible-playbook tags.yml --tags foo
|
||||||
|
#
|
||||||
|
# only tags with the given tags will be run when --tags is specified
|
||||||
|
#
|
||||||
|
# (an include statement will also be able to set tags on all included
|
||||||
|
# tasks at some point in the future)
|
||||||
|
|
||||||
|
|
||||||
|
- name: example play
|
||||||
|
hosts: all
|
||||||
|
user: root
|
||||||
|
tasks:
|
||||||
|
- name: hi
|
||||||
|
tags: foo
|
||||||
|
action: shell echo "first play ran"
|
||||||
|
|
||||||
|
- name: example play
|
||||||
|
hosts: all
|
||||||
|
user: root
|
||||||
|
tasks:
|
||||||
|
- name: hi
|
||||||
|
tags: bar
|
||||||
|
action: shell echo "second play ran"
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -61,7 +61,8 @@ class PlayBook(object):
|
||||||
stats = None,
|
stats = None,
|
||||||
sudo = False,
|
sudo = False,
|
||||||
sudo_user = C.DEFAULT_SUDO_USER,
|
sudo_user = C.DEFAULT_SUDO_USER,
|
||||||
extra_vars = None):
|
extra_vars = None,
|
||||||
|
only_tags = None):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
playbook: path to a playbook file
|
playbook: path to a playbook file
|
||||||
|
@ -88,6 +89,9 @@ class PlayBook(object):
|
||||||
if extra_vars is None:
|
if extra_vars is None:
|
||||||
extra_vars = {}
|
extra_vars = {}
|
||||||
|
|
||||||
|
if only_tags is None:
|
||||||
|
only_tags = [ 'all' ]
|
||||||
|
|
||||||
self.module_path = module_path
|
self.module_path = module_path
|
||||||
self.forks = forks
|
self.forks = forks
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
|
@ -105,6 +109,7 @@ class PlayBook(object):
|
||||||
self.extra_vars = extra_vars
|
self.extra_vars = extra_vars
|
||||||
self.global_vars = {}
|
self.global_vars = {}
|
||||||
self.private_key_file = private_key_file
|
self.private_key_file = private_key_file
|
||||||
|
self.only_tags = only_tags
|
||||||
|
|
||||||
self.inventory = ansible.inventory.Inventory(host_list)
|
self.inventory = ansible.inventory.Inventory(host_list)
|
||||||
|
|
||||||
|
@ -286,8 +291,16 @@ class PlayBook(object):
|
||||||
if play.vars_files and len(play.vars_files) > 0:
|
if play.vars_files and len(play.vars_files) > 0:
|
||||||
rc = self._do_setup_step(play, play.vars_files)
|
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():
|
for task in play.tasks():
|
||||||
|
|
||||||
|
# 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)
|
self._run_task(play, task, False)
|
||||||
|
|
||||||
# run notify actions
|
# run notify actions
|
||||||
|
|
|
@ -26,8 +26,10 @@ import os
|
||||||
class Play(object):
|
class Play(object):
|
||||||
|
|
||||||
__slots__ = [
|
__slots__ = [
|
||||||
'hosts', 'name', 'vars', 'vars_prompt', 'vars_files', 'handlers', 'remote_user', 'remote_port',
|
'hosts', 'name', 'vars', 'vars_prompt', 'vars_files',
|
||||||
'sudo', 'sudo_user', 'transport', 'playbook', '_ds', '_handlers', '_tasks'
|
'handlers', 'remote_user', 'remote_port',
|
||||||
|
'sudo', 'sudo_user', 'transport', 'playbook',
|
||||||
|
'_ds', '_handlers', '_tasks'
|
||||||
]
|
]
|
||||||
|
|
||||||
# *************************************************
|
# *************************************************
|
||||||
|
@ -37,6 +39,7 @@ class Play(object):
|
||||||
|
|
||||||
# TODO: more error handling
|
# TODO: more error handling
|
||||||
|
|
||||||
|
|
||||||
hosts = ds.get('hosts')
|
hosts = ds.get('hosts')
|
||||||
if hosts is None:
|
if hosts is None:
|
||||||
raise errors.AnsibleError('hosts declaration is required')
|
raise errors.AnsibleError('hosts declaration is required')
|
||||||
|
|
|
@ -24,7 +24,8 @@ class Task(object):
|
||||||
|
|
||||||
__slots__ = [
|
__slots__ = [
|
||||||
'name', 'action', 'only_if', 'async_seconds', 'async_poll_interval',
|
'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):
|
def __init__(self, play, ds, module_vars=None):
|
||||||
|
@ -38,6 +39,8 @@ class Task(object):
|
||||||
self.play = play
|
self.play = play
|
||||||
self.name = ds.get('name', None)
|
self.name = ds.get('name', None)
|
||||||
self.action = ds.get('action', '')
|
self.action = ds.get('action', '')
|
||||||
|
self.tags = [ 'all' ]
|
||||||
|
|
||||||
self.notified_by = []
|
self.notified_by = []
|
||||||
|
|
||||||
if self.name is None:
|
if self.name is None:
|
||||||
|
@ -67,4 +70,15 @@ class Task(object):
|
||||||
if 'first_available_file' in ds:
|
if 'first_available_file' in ds:
|
||||||
self.module_vars['first_available_file'] = ds.get('first_available_file')
|
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…
Reference in a new issue