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/>.
|
|
|
|
|
|
|
|
#############################################
|
|
|
|
|
|
|
|
from ansible import utils
|
|
|
|
from ansible import errors
|
|
|
|
from ansible.playbook.task import Task
|
|
|
|
import shlex
|
|
|
|
import os
|
|
|
|
|
|
|
|
class Play(object):
|
|
|
|
|
2012-08-07 00:07:02 +00:00
|
|
|
__slots__ = [
|
|
|
|
'hosts', 'name', 'vars', 'vars_prompt', 'vars_files',
|
2012-07-11 23:51:26 +00:00
|
|
|
'handlers', 'remote_user', 'remote_port',
|
2012-08-07 00:07:02 +00:00
|
|
|
'sudo', 'sudo_user', 'transport', 'playbook',
|
2012-07-14 23:49:42 +00:00
|
|
|
'tags', 'gather_facts', '_ds', '_handlers', '_tasks'
|
2012-05-26 04:37:34 +00:00
|
|
|
]
|
|
|
|
|
2012-07-17 23:09:36 +00:00
|
|
|
# to catch typos and so forth -- these are userland names
|
|
|
|
# and don't line up 1:1 with how they are stored
|
|
|
|
VALID_KEYS = [
|
|
|
|
'hosts', 'name', 'vars', 'vars_prompt', 'vars_files',
|
|
|
|
'tasks', 'handlers', 'user', 'port', 'include',
|
|
|
|
'sudo', 'sudo_user', 'connection', 'tags', 'gather_facts'
|
|
|
|
]
|
|
|
|
|
2012-05-26 04:37:34 +00:00
|
|
|
# *************************************************
|
|
|
|
|
|
|
|
def __init__(self, playbook, ds):
|
2012-05-26 05:20:53 +00:00
|
|
|
''' constructor loads from a play datastructure '''
|
|
|
|
|
2012-07-17 23:09:36 +00:00
|
|
|
for x in ds.keys():
|
|
|
|
if not x in Play.VALID_KEYS:
|
|
|
|
raise errors.AnsibleError("%s is not a legal parameter in an Ansible Playbook" % x)
|
|
|
|
|
2012-05-26 05:20:53 +00:00
|
|
|
# TODO: more error handling
|
2012-07-11 23:51:26 +00:00
|
|
|
|
2012-05-31 20:06:21 +00:00
|
|
|
hosts = ds.get('hosts')
|
|
|
|
if hosts is None:
|
|
|
|
raise errors.AnsibleError('hosts declaration is required')
|
|
|
|
elif isinstance(hosts, list):
|
|
|
|
hosts = ';'.join(hosts)
|
2012-07-15 18:19:11 +00:00
|
|
|
hosts = utils.template(hosts, playbook.extra_vars)
|
2012-05-31 20:06:21 +00:00
|
|
|
|
2012-07-14 23:49:42 +00:00
|
|
|
self._ds = ds
|
|
|
|
self.playbook = playbook
|
2012-08-07 00:07:02 +00:00
|
|
|
self.hosts = hosts
|
2012-07-14 23:49:42 +00:00
|
|
|
self.name = ds.get('name', self.hosts)
|
|
|
|
self.vars = ds.get('vars', {})
|
|
|
|
self.vars_files = ds.get('vars_files', [])
|
|
|
|
self.vars_prompt = ds.get('vars_prompt', {})
|
|
|
|
self.vars = self._get_vars(self.playbook.basedir)
|
|
|
|
self._tasks = ds.get('tasks', [])
|
|
|
|
self._handlers = ds.get('handlers', [])
|
|
|
|
self.remote_user = ds.get('user', self.playbook.remote_user)
|
|
|
|
self.remote_port = ds.get('port', self.playbook.remote_port)
|
|
|
|
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.gather_facts = ds.get('gather_facts', True)
|
2012-07-14 17:23:14 +00:00
|
|
|
|
|
|
|
self._update_vars_files_for_host(None)
|
|
|
|
|
2012-05-26 04:37:34 +00:00
|
|
|
self._tasks = self._load_tasks(self._ds, 'tasks')
|
|
|
|
self._handlers = self._load_tasks(self._ds, 'handlers')
|
|
|
|
|
2012-07-12 04:58:48 +00:00
|
|
|
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 = []
|
|
|
|
|
2012-05-26 04:37:34 +00:00
|
|
|
if self.sudo_user != 'root':
|
|
|
|
self.sudo = True
|
|
|
|
|
|
|
|
# *************************************************
|
|
|
|
|
|
|
|
def _load_tasks(self, ds, keyname):
|
|
|
|
''' handle task and handler include statements '''
|
|
|
|
|
2012-06-09 19:42:45 +00:00
|
|
|
tasks = ds.get(keyname, [])
|
2012-05-26 04:37:34 +00:00
|
|
|
results = []
|
2012-06-09 19:42:45 +00:00
|
|
|
for x in tasks:
|
|
|
|
task_vars = self.vars.copy()
|
2012-05-26 04:37:34 +00:00
|
|
|
if 'include' in x:
|
|
|
|
tokens = shlex.split(x['include'])
|
2012-07-12 00:30:30 +00:00
|
|
|
|
2012-05-26 04:37:34 +00:00
|
|
|
for t in tokens[1:]:
|
|
|
|
(k,v) = t.split("=", 1)
|
2012-08-10 01:08:43 +00:00
|
|
|
task_vars[k] = utils.template(v, task_vars)
|
2012-07-17 22:56:31 +00:00
|
|
|
include_file = utils.template(tokens[0], task_vars)
|
2012-05-26 04:37:34 +00:00
|
|
|
data = utils.parse_yaml_from_file(utils.path_dwim(self.playbook.basedir, include_file))
|
|
|
|
elif type(x) == dict:
|
2012-06-09 19:42:45 +00:00
|
|
|
data = [x]
|
|
|
|
else:
|
|
|
|
raise Exception("unexpected task type")
|
|
|
|
for y in data:
|
2012-07-14 23:18:33 +00:00
|
|
|
mv = task_vars.copy()
|
|
|
|
results.append(Task(self,y,module_vars=mv))
|
2012-07-12 04:58:48 +00:00
|
|
|
|
|
|
|
for x in results:
|
|
|
|
if self.tags is not None:
|
|
|
|
x.tags.extend(self.tags)
|
|
|
|
|
2012-05-26 04:37:34 +00:00
|
|
|
return results
|
|
|
|
|
|
|
|
# *************************************************
|
|
|
|
|
|
|
|
def tasks(self):
|
2012-05-26 05:20:53 +00:00
|
|
|
''' return task objects for this play '''
|
2012-08-07 00:07:02 +00:00
|
|
|
return self._tasks
|
2012-05-26 04:37:34 +00:00
|
|
|
|
2012-05-26 05:20:53 +00:00
|
|
|
def handlers(self):
|
|
|
|
''' return handler objects for this play '''
|
|
|
|
return self._handlers
|
|
|
|
|
2012-05-26 04:37:34 +00:00
|
|
|
# *************************************************
|
|
|
|
|
|
|
|
def _get_vars(self, dirname):
|
2012-05-26 05:20:53 +00:00
|
|
|
''' load the vars section from a play, accounting for all sorts of variable features
|
|
|
|
including loading from yaml files, prompting, and conditional includes of the first
|
|
|
|
file found in a list. '''
|
2012-05-26 04:37:34 +00:00
|
|
|
|
|
|
|
if self.vars is None:
|
|
|
|
self.vars = {}
|
|
|
|
|
|
|
|
if type(self.vars) not in [dict, list]:
|
|
|
|
raise errors.AnsibleError("'vars' section must contain only key/value pairs")
|
|
|
|
|
|
|
|
vars = self.playbook.global_vars
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-05-26 04:37:34 +00:00
|
|
|
# translate a list of vars into a dict
|
|
|
|
if type(self.vars) == list:
|
|
|
|
for item in self.vars:
|
|
|
|
k, v = item.items()[0]
|
|
|
|
vars[k] = v
|
|
|
|
else:
|
|
|
|
vars.update(self.vars)
|
|
|
|
|
2012-07-24 14:43:29 +00:00
|
|
|
if type(self.vars_prompt) == list:
|
|
|
|
for var in self.vars_prompt:
|
2012-07-27 23:00:29 +00:00
|
|
|
if not 'name' in var:
|
|
|
|
raise errors.AnsibleError("'vars_prompt' item is missing 'name:'")
|
|
|
|
vname = var['name']
|
|
|
|
prompt = "%s: " % var.get("prompt", vname)
|
2012-07-24 14:43:29 +00:00
|
|
|
private = var.get("private", True)
|
|
|
|
vars[vname] = self.playbook.callbacks.on_vars_prompt(vname, private, prompt)
|
|
|
|
elif type(self.vars_prompt) == dict:
|
2012-07-27 23:00:29 +00:00
|
|
|
for (vname, prompt) in self.vars_prompt.iteritems():
|
|
|
|
prompt = "%s: " % prompt
|
|
|
|
vars[vname] = self.playbook.callbacks.on_vars_prompt(vname, False, prompt)
|
2012-07-24 14:43:29 +00:00
|
|
|
else:
|
2012-07-27 23:00:29 +00:00
|
|
|
raise errors.AnsibleError("'vars_prompt' section is malformed, see docs")
|
2012-05-26 04:37:34 +00:00
|
|
|
|
|
|
|
results = self.playbook.extra_vars.copy()
|
|
|
|
results.update(vars)
|
|
|
|
return results
|
|
|
|
|
|
|
|
# *************************************************
|
|
|
|
|
|
|
|
def update_vars_files(self, hosts):
|
|
|
|
''' calculate vars_files, which requires that setup runs first so ansible facts can be mixed in '''
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-14 17:12:19 +00:00
|
|
|
# now loop through all the hosts...
|
2012-05-26 04:37:34 +00:00
|
|
|
for h in hosts:
|
2012-05-26 05:20:53 +00:00
|
|
|
self._update_vars_files_for_host(h)
|
2012-05-26 04:37:34 +00:00
|
|
|
|
|
|
|
# *************************************************
|
|
|
|
|
2012-07-12 00:30:30 +00:00
|
|
|
def should_run(self, tags):
|
|
|
|
''' does the play match any of the tags? '''
|
2012-07-12 04:17:51 +00:00
|
|
|
|
2012-07-26 00:45:51 +00:00
|
|
|
tags_counted = 0
|
2012-07-12 00:30:30 +00:00
|
|
|
for task in self._tasks:
|
|
|
|
for task_tag in task.tags:
|
2012-07-26 00:45:51 +00:00
|
|
|
tags_counted = tags_counted + 1
|
2012-07-12 00:30:30 +00:00
|
|
|
if task_tag in tags:
|
|
|
|
return True
|
2012-07-26 00:45:51 +00:00
|
|
|
|
|
|
|
if tags_counted > 0:
|
2012-08-07 00:07:02 +00:00
|
|
|
return False
|
|
|
|
|
2012-07-26 00:45:51 +00:00
|
|
|
# didn't tag the play, and the play contains no steps
|
|
|
|
# so assume we just want to gather facts
|
2012-08-07 00:07:02 +00:00
|
|
|
return True
|
2012-07-12 00:30:30 +00:00
|
|
|
|
|
|
|
# *************************************************
|
|
|
|
|
2012-07-14 17:12:19 +00:00
|
|
|
def _has_vars_in(self, msg):
|
|
|
|
return ((msg.find("$") != -1) or (msg.find("{{") != -1))
|
|
|
|
|
|
|
|
# *************************************************
|
|
|
|
|
2012-05-26 05:20:53 +00:00
|
|
|
def _update_vars_files_for_host(self, host):
|
2012-05-26 04:37:34 +00:00
|
|
|
|
2012-07-14 17:12:19 +00:00
|
|
|
if type(self.vars_files) != list:
|
|
|
|
self.vars_files = [ self.vars_files ]
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-20 13:43:45 +00:00
|
|
|
if (host is not None):
|
|
|
|
inventory = self.playbook.inventory
|
|
|
|
hostrec = inventory.get_host(host)
|
|
|
|
groups = [ g.name for g in hostrec.groups ]
|
|
|
|
basedir = inventory.basedir()
|
|
|
|
if basedir is not None:
|
|
|
|
for x in groups:
|
|
|
|
path = os.path.join(basedir, "group_vars/%s" % x)
|
|
|
|
if os.path.exists(path):
|
2012-07-20 14:00:51 +00:00
|
|
|
data = utils.parse_yaml_from_file(path)
|
2012-08-01 23:46:33 +00:00
|
|
|
if type(data) != dict:
|
|
|
|
raise errors.AnsibleError("%s must be stored as a dictionary/hash" % path)
|
2012-07-20 14:00:51 +00:00
|
|
|
self.playbook.SETUP_CACHE[host].update(data)
|
2012-07-20 13:43:45 +00:00
|
|
|
path = os.path.join(basedir, "host_vars/%s" % hostrec.name)
|
|
|
|
if os.path.exists(path):
|
2012-07-20 14:00:51 +00:00
|
|
|
data = utils.parse_yaml_from_file(path)
|
2012-08-01 23:46:33 +00:00
|
|
|
if type(data) != dict:
|
|
|
|
raise errors.AnsibleError("%s must be stored as a dictionary/hash" % path)
|
2012-07-20 14:00:51 +00:00
|
|
|
self.playbook.SETUP_CACHE[host].update(data)
|
2012-07-20 13:43:45 +00:00
|
|
|
|
2012-05-26 04:37:34 +00:00
|
|
|
for filename in self.vars_files:
|
|
|
|
|
|
|
|
if type(filename) == list:
|
|
|
|
|
|
|
|
# loop over all filenames, loading the first one, and failing if # none found
|
|
|
|
found = False
|
|
|
|
sequence = []
|
|
|
|
for real_filename in filename:
|
2012-07-14 17:12:19 +00:00
|
|
|
filename2 = utils.template(real_filename, self.vars)
|
|
|
|
filename3 = filename2
|
|
|
|
if host is not None:
|
|
|
|
filename3 = utils.template(filename2, self.playbook.SETUP_CACHE[host])
|
|
|
|
filename4 = utils.path_dwim(self.playbook.basedir, filename3)
|
|
|
|
sequence.append(filename4)
|
|
|
|
if os.path.exists(filename4):
|
2012-05-26 04:37:34 +00:00
|
|
|
found = True
|
2012-07-14 17:12:19 +00:00
|
|
|
data = utils.parse_yaml_from_file(filename4)
|
2012-08-01 23:51:52 +00:00
|
|
|
if type(data) != dict:
|
|
|
|
raise errors.AnsibleError("%s must be stored as a dictionary/hash" % filename4)
|
2012-07-14 17:12:19 +00:00
|
|
|
if host is not None:
|
|
|
|
if self._has_vars_in(filename2) and not self._has_vars_in(filename3):
|
|
|
|
# this filename has variables in it that were fact specific
|
|
|
|
# so it needs to be loaded into the per host SETUP_CACHE
|
|
|
|
self.playbook.SETUP_CACHE[host].update(data)
|
|
|
|
self.playbook.callbacks.on_import_for_host(host, filename4)
|
|
|
|
elif not self._has_vars_in(filename4):
|
|
|
|
# found a non-host specific variable, load into vars and NOT
|
|
|
|
# the setup cache
|
|
|
|
self.vars.update(data)
|
|
|
|
elif host is not None:
|
|
|
|
self.playbook.callbacks.on_not_import_for_host(host, filename4)
|
2012-07-19 00:48:13 +00:00
|
|
|
if found:
|
2012-07-18 22:31:04 +00:00
|
|
|
break
|
2012-05-26 04:37:34 +00:00
|
|
|
if not found:
|
|
|
|
raise errors.AnsibleError(
|
|
|
|
"%s: FATAL, no files matched for vars_files import sequence: %s" % (host, sequence)
|
|
|
|
)
|
|
|
|
|
|
|
|
else:
|
2012-07-14 17:12:19 +00:00
|
|
|
# just one filename supplied, load it!
|
|
|
|
|
|
|
|
filename2 = utils.template(filename, self.vars)
|
|
|
|
filename3 = filename2
|
|
|
|
if host is not None:
|
|
|
|
filename3 = utils.template(filename2, self.playbook.SETUP_CACHE[host])
|
|
|
|
filename4 = utils.path_dwim(self.playbook.basedir, filename3)
|
2012-07-14 23:18:33 +00:00
|
|
|
if self._has_vars_in(filename4):
|
|
|
|
return
|
2012-07-14 17:12:19 +00:00
|
|
|
new_vars = utils.parse_yaml_from_file(filename4)
|
2012-05-29 14:23:09 +00:00
|
|
|
if new_vars:
|
2012-07-14 16:39:06 +00:00
|
|
|
if type(new_vars) != dict:
|
2012-08-01 23:51:52 +00:00
|
|
|
raise errors.AnsibleError("%s must be stored as dictonary/hash: %s" % filename4)
|
2012-07-14 17:12:19 +00:00
|
|
|
if host is not None and self._has_vars_in(filename2) and not self._has_vars_in(filename3):
|
|
|
|
# running a host specific pass and has host specific variables
|
2012-08-07 00:07:02 +00:00
|
|
|
# load into setup cache
|
2012-07-14 17:12:19 +00:00
|
|
|
self.playbook.SETUP_CACHE[host].update(new_vars)
|
|
|
|
elif host is None:
|
2012-08-07 00:07:02 +00:00
|
|
|
# running a non-host specific pass and we can update the global vars instead
|
2012-07-14 17:12:19 +00:00
|
|
|
self.vars.update(new_vars)
|