Added a 'set_up' and 'tear_down' which are like tasks, but execute before and after roles.

This commit is contained in:
Michael DeHaan 2013-04-20 16:07:06 -04:00
parent e6bf01a6b0
commit d7623d1f91
3 changed files with 37 additions and 1 deletions

View file

@ -25,6 +25,12 @@
--- ---
- hosts: all - hosts: all
set_up:
# set up tasks are executed prior to roles.
- local_action: shell echo "hi this is a setup step about {{ inventory_hostname }}"
roles: roles:
# a role can be listed flat like this: # a role can be listed flat like this:
@ -51,4 +57,11 @@
- shell: echo 'this is a loose task' - shell: echo 'this is a loose task'
tear_down:
# just to provide a syntactic mirroring to 'set_up', tear_down runs dead last in the play.
- local_action: shell echo 'this is a teardown task about {{ inventory_hostname }}'

View file

@ -40,7 +40,7 @@ class Play(object):
'hosts', 'name', 'vars', 'vars_prompt', 'vars_files', 'hosts', 'name', 'vars', 'vars_prompt', 'vars_files',
'tasks', 'handlers', 'user', 'port', 'include', 'tasks', 'handlers', 'user', 'port', 'include',
'sudo', 'sudo_user', 'connection', 'tags', 'gather_facts', 'serial', 'sudo', 'sudo_user', 'connection', 'tags', 'gather_facts', 'serial',
'any_errors_fatal', 'roles' 'any_errors_fatal', 'roles', 'set_up', 'tear_down'
] ]
# ************************************************* # *************************************************
@ -135,6 +135,12 @@ class Play(object):
new_handlers = [] new_handlers = []
new_vars_files = [] new_vars_files = []
set_up = ds.get('set_up', None)
if type(set_up) != list:
set_up = []
for x in set_up:
new_tasks.append(x)
# variables if the role was parameterized (i.e. given as a hash) # variables if the role was parameterized (i.e. given as a hash)
has_dict = {} has_dict = {}
@ -180,15 +186,22 @@ class Play(object):
new_vars_files.append(vars_file) new_vars_files.append(vars_file)
tasks = ds.get('tasks', None) tasks = ds.get('tasks', None)
tear_down = ds.get('tear_down', None)
handlers = ds.get('handlers', None) handlers = ds.get('handlers', None)
vars_files = ds.get('vars_files', None) vars_files = ds.get('vars_files', None)
if type(tasks) != list: if type(tasks) != list:
tasks = [] tasks = []
if type(handlers) != list: if type(handlers) != list:
handlers = [] handlers = []
if type(vars_files) != list: if type(vars_files) != list:
vars_files = [] vars_files = []
if type(tear_down) != list:
tear_down = []
new_tasks.extend(tasks) new_tasks.extend(tasks)
new_tasks.extend(tear_down)
new_handlers.extend(handlers) new_handlers.extend(handlers)
new_vars_files.extend(vars_files) new_vars_files.extend(vars_files)
ds['tasks'] = new_tasks ds['tasks'] = new_tasks

View file

@ -61,6 +61,16 @@ class PluginLoader(object):
self._extra_dirs = [] self._extra_dirs = []
def print_paths(self):
''' Returns a string suitable for printing of the search path '''
# Uses a list to get the order right
ret = []
for i in self._get_paths():
if i not in ret:
ret.append(i)
return os.pathsep.join(ret)
def _get_package_path(self): def _get_package_path(self):
''' Gets the path of a Python package ''' ''' Gets the path of a Python package '''