2012-05-26 04:37:34 +00:00
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2012-05-26 05:20:53 +00:00
|
|
|
from ansible import errors
|
2012-05-26 05:49:23 +00:00
|
|
|
from ansible import utils
|
2012-05-26 04:37:34 +00:00
|
|
|
|
2012-08-19 20:52:55 +00:00
|
|
|
|
2012-05-26 04:37:34 +00:00
|
|
|
class Task(object):
|
|
|
|
|
2012-08-07 00:07:02 +00:00
|
|
|
__slots__ = [
|
2012-05-26 04:37:34 +00:00
|
|
|
'name', 'action', 'only_if', 'async_seconds', 'async_poll_interval',
|
2012-08-07 00:07:02 +00:00
|
|
|
'notify', 'module_name', 'module_args', 'module_vars',
|
2012-08-18 12:46:51 +00:00
|
|
|
'play', 'notified_by', 'tags', 'register', 'with_items',
|
2012-08-19 20:52:55 +00:00
|
|
|
'delegate_to', 'first_available_file', 'ignore_errors',
|
2012-10-12 16:39:45 +00:00
|
|
|
'local_action', 'transport', 'sudo', 'sudo_user', 'sudo_pass'
|
2012-05-26 04:37:34 +00:00
|
|
|
]
|
|
|
|
|
2012-07-17 23:09:36 +00:00
|
|
|
# to prevent typos and such
|
|
|
|
VALID_KEYS = [
|
2012-08-18 12:46:51 +00:00
|
|
|
'name', 'action', 'only_if', 'async', 'poll', 'notify', 'with_items',
|
|
|
|
'first_available_file', 'include', 'tags', 'register', 'ignore_errors',
|
2012-10-12 16:39:45 +00:00
|
|
|
'delegate_to', 'local_action', 'transport', 'sudo', 'sudo_user',
|
|
|
|
'sudo_pass'
|
2012-07-17 23:09:36 +00:00
|
|
|
]
|
|
|
|
|
2012-05-26 05:49:23 +00:00
|
|
|
def __init__(self, play, ds, module_vars=None):
|
2012-05-26 05:20:53 +00:00
|
|
|
''' constructor loads from a task or handler datastructure '''
|
2012-10-11 11:56:01 +00:00
|
|
|
|
|
|
|
# code to allow for saying "modulename: args" versus "action: modulename args"
|
2012-07-17 23:09:36 +00:00
|
|
|
for x in ds.keys():
|
2012-10-12 15:28:10 +00:00
|
|
|
if x in play.playbook.modules_list:
|
2012-10-09 04:21:13 +00:00
|
|
|
ds['action'] = x + " " + ds.get(x, None)
|
|
|
|
ds.pop(x)
|
|
|
|
elif not x in Task.VALID_KEYS:
|
2012-07-17 23:09:36 +00:00
|
|
|
raise errors.AnsibleError("%s is not a legal parameter in an Ansible task or handler" % x)
|
|
|
|
|
2012-05-26 05:49:23 +00:00
|
|
|
self.module_vars = module_vars
|
2012-05-26 05:20:53 +00:00
|
|
|
self.play = play
|
2012-07-15 13:17:04 +00:00
|
|
|
|
|
|
|
# load various attributes
|
2012-08-19 20:52:55 +00:00
|
|
|
self.name = ds.get('name', None)
|
|
|
|
self.tags = [ 'all' ]
|
|
|
|
self.register = ds.get('register', None)
|
2012-10-12 16:39:45 +00:00
|
|
|
self.sudo = ds.get('sudo', play.sudo)
|
|
|
|
if self.sudo is True:
|
|
|
|
self.sudo_user = ds.get('sudo_user', play.sudo_user)
|
|
|
|
self.sudo_pass = ds.get('sudo_pass', play.playbook.sudo_pass)
|
|
|
|
else:
|
|
|
|
self.sudo_user = None
|
|
|
|
self.sudo_pass = None
|
2012-09-25 19:47:17 +00:00
|
|
|
|
2012-08-19 20:52:55 +00:00
|
|
|
# Both are defined
|
|
|
|
if ('action' in ds) and ('local_action' in ds):
|
|
|
|
raise errors.AnsibleError("the 'action' and 'local_action' attributes can not be used together")
|
|
|
|
# Both are NOT defined
|
|
|
|
elif (not 'action' in ds) and (not 'local_action' in ds):
|
|
|
|
raise errors.AnsibleError("task missing an 'action' attribute")
|
|
|
|
# Only one of them is defined
|
|
|
|
elif 'local_action' in ds:
|
|
|
|
self.action = ds.get('local_action', '')
|
2012-09-17 18:28:14 +00:00
|
|
|
self.delegate_to = '127.0.0.1'
|
2012-08-19 20:52:55 +00:00
|
|
|
else:
|
|
|
|
self.action = ds.get('action', '')
|
|
|
|
self.delegate_to = ds.get('delegate_to', None)
|
2012-09-17 16:28:37 +00:00
|
|
|
self.transport = ds.get('transport', play.transport)
|
2012-07-11 23:51:26 +00:00
|
|
|
|
2012-09-25 19:47:17 +00:00
|
|
|
# delegate_to can use variables
|
|
|
|
if not (self.delegate_to is None):
|
2012-10-11 11:56:01 +00:00
|
|
|
self.delegate_to = utils.template(None, self.delegate_to, self.module_vars)
|
|
|
|
# delegate_to: localhost should use local transport
|
|
|
|
if self.delegate_to in ['127.0.0.1', 'localhost']:
|
|
|
|
self.transport = 'local'
|
2012-09-25 19:47:17 +00:00
|
|
|
|
2012-07-15 13:17:04 +00:00
|
|
|
# notified by is used by Playbook code to flag which hosts
|
|
|
|
# need to run a notifier
|
2012-05-26 04:37:34 +00:00
|
|
|
self.notified_by = []
|
|
|
|
|
2012-07-15 13:17:04 +00:00
|
|
|
# if no name is specified, use the action line as the name
|
2012-05-26 04:37:34 +00:00
|
|
|
if self.name is None:
|
|
|
|
self.name = self.action
|
|
|
|
|
2012-07-15 13:17:04 +00:00
|
|
|
# load various attributes
|
2012-05-26 04:37:34 +00:00
|
|
|
self.only_if = ds.get('only_if', 'True')
|
|
|
|
self.async_seconds = int(ds.get('async', 0)) # not async by default
|
|
|
|
self.async_poll_interval = int(ds.get('poll', 10)) # default poll = 10 seconds
|
|
|
|
self.notify = ds.get('notify', [])
|
2012-07-15 13:17:04 +00:00
|
|
|
self.first_available_file = ds.get('first_available_file', None)
|
|
|
|
self.with_items = ds.get('with_items', None)
|
2012-08-18 12:46:51 +00:00
|
|
|
|
2012-08-01 16:13:07 +00:00
|
|
|
self.ignore_errors = ds.get('ignore_errors', False)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-15 13:17:04 +00:00
|
|
|
# notify can be a string or a list, store as a list
|
2012-05-26 04:37:34 +00:00
|
|
|
if isinstance(self.notify, basestring):
|
|
|
|
self.notify = [ self.notify ]
|
|
|
|
|
2012-07-15 13:17:04 +00:00
|
|
|
# split the action line into a module name + arguments
|
2012-05-26 04:37:34 +00:00
|
|
|
tokens = self.action.split(None, 1)
|
|
|
|
if len(tokens) < 1:
|
2012-08-02 12:37:43 +00:00
|
|
|
raise errors.AnsibleError("invalid/missing action in task. name: %s" % self.name)
|
2012-05-26 04:37:34 +00:00
|
|
|
self.module_name = tokens[0]
|
|
|
|
self.module_args = ''
|
|
|
|
if len(tokens) > 1:
|
|
|
|
self.module_args = tokens[1]
|
|
|
|
|
2012-07-15 13:17:04 +00:00
|
|
|
import_tags = self.module_vars.get('tags',[])
|
|
|
|
if type(import_tags) in [str,unicode]:
|
|
|
|
# allow the user to list comma delimited tags
|
|
|
|
import_tags = import_tags.split(",")
|
2012-05-26 05:49:23 +00:00
|
|
|
|
2012-09-17 12:02:30 +00:00
|
|
|
self.name = utils.template(None, self.name, self.module_vars)
|
|
|
|
self.action = utils.template(None, self.action, self.module_vars)
|
2012-05-26 05:49:23 +00:00
|
|
|
|
2012-07-15 13:17:04 +00:00
|
|
|
# handle mutually incompatible options
|
|
|
|
if self.with_items is not None and self.first_available_file is not None:
|
|
|
|
raise errors.AnsibleError("with_items and first_available_file are mutually incompatible in a single task")
|
|
|
|
|
|
|
|
# make first_available_file accessable to Runner code
|
|
|
|
if self.first_available_file:
|
|
|
|
self.module_vars['first_available_file'] = self.first_available_file
|
2012-08-07 00:07:02 +00:00
|
|
|
|
|
|
|
# process with_items so it can be used by Runner code
|
2012-07-15 13:17:04 +00:00
|
|
|
if self.with_items is None:
|
|
|
|
self.with_items = [ ]
|
|
|
|
self.module_vars['items'] = self.with_items
|
2012-05-26 05:20:53 +00:00
|
|
|
|
2012-08-18 12:46:51 +00:00
|
|
|
# allow runner to see delegate_to option
|
|
|
|
self.module_vars['delegate_to'] = self.delegate_to
|
|
|
|
|
2012-08-01 16:13:07 +00:00
|
|
|
# make ignore_errors accessable to Runner code
|
|
|
|
self.module_vars['ignore_errors'] = self.ignore_errors
|
|
|
|
|
2012-07-15 16:29:53 +00:00
|
|
|
# tags allow certain parts of a playbook to be run without running the whole playbook
|
2012-07-11 23:51:26 +00:00
|
|
|
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)
|
2012-07-12 00:30:30 +00:00
|
|
|
self.tags.extend(import_tags)
|
2012-08-07 00:07:02 +00:00
|
|
|
|