Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support import of playbooks in other directories #1030

Merged
merged 1 commit into from
Sep 11, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 13 additions & 8 deletions lib/ansible/playbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def __init__(self,
self.global_vars.update(self.inventory.get_group_variables('all'))

self.basedir = os.path.dirname(playbook)
self.playbook = self._load_playbook_from_file(playbook)
(self.playbook, self.play_basedirs) = self._load_playbook_from_file(playbook)
self.module_path = self.module_path + os.pathsep + os.path.join(self.basedir, "library")

# *****************************************************
Expand All @@ -124,6 +124,7 @@ def _load_playbook_from_file(self, path):

playbook_data = utils.parse_yaml_from_file(path)
accumulated_plays = []
play_basedirs = []

if type(playbook_data) != list:
raise errors.AnsibleError("parse error: playbooks must be formatted as a YAML list")
Expand All @@ -134,13 +135,17 @@ def _load_playbook_from_file(self, path):
if 'include' in play:
if len(play.keys()) == 1:
included_path = utils.path_dwim(self.basedir, play['include'])
accumulated_plays.extend(self._load_playbook_from_file(included_path))
(plays, basedirs) = self._load_playbook_from_file(included_path)
accumulated_plays.extend(plays)
play_basedirs.extend(basedirs)

else:
raise errors.AnsibleError("parse error: top level includes cannot be used with other directives: %s" % play)
else:
accumulated_plays.append(play)
play_basedirs.append(os.path.dirname(path))

return accumulated_plays
return (accumulated_plays, play_basedirs)

# *****************************************************

Expand All @@ -152,8 +157,8 @@ def run(self):

# loop through all patterns and run them
self.callbacks.on_start()
for play_ds in self.playbook:
play = Play(self,play_ds)
for (play_ds, play_basedir) in zip(self.playbook, self.play_basedirs):
play = Play(self, play_ds, play_basedir)
matched_tags, unmatched_tags = play.compare_tags(self.only_tags)
matched_tags_all = matched_tags_all | matched_tags
unmatched_tags_all = unmatched_tags_all | unmatched_tags
Expand All @@ -166,8 +171,8 @@ def run(self):
# if the playbook is invoked with --tags that don't exist at all in the playbooks
# then we need to raise an error so that the user can correct the arguments.
unknown_tags = set(self.only_tags) - (matched_tags_all | unmatched_tags_all)
unknown_tags.discard('all')
unknown_tags.discard('all')

if len(unknown_tags) > 0:
unmatched_tags_all.discard('all')
msg = 'tag(s) not found in playbook: %s. possible values: %s'
Expand Down Expand Up @@ -215,7 +220,7 @@ def _run_task_internal(self, task):
timeout=self.timeout, remote_user=task.play.remote_user,
remote_port=task.play.remote_port, module_vars=task.module_vars,
private_key_file=self.private_key_file,
setup_cache=self.SETUP_CACHE, basedir=self.basedir,
setup_cache=self.SETUP_CACHE, basedir=task.play.basedir,
conditional=task.only_if, callbacks=self.runner_callbacks,
sudo=task.play.sudo, sudo_user=task.play.sudo_user,
transport=task.play.transport, sudo_pass=self.sudo_pass, is_playbook=True
Expand Down
17 changes: 9 additions & 8 deletions lib/ansible/playbook/play.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class Play(object):
'hosts', 'name', 'vars', 'vars_prompt', 'vars_files',
'handlers', 'remote_user', 'remote_port',
'sudo', 'sudo_user', 'transport', 'playbook',
'tags', 'gather_facts', 'serial', '_ds', '_handlers', '_tasks'
'tags', 'gather_facts', 'serial', '_ds', '_handlers', '_tasks',
'basedir'
]

# to catch typos and so forth -- these are userland names
Expand All @@ -42,7 +43,7 @@ class Play(object):

# *************************************************

def __init__(self, playbook, ds):
def __init__(self, playbook, ds, basedir):
''' constructor loads from a play datastructure '''

for x in ds.keys():
Expand All @@ -57,15 +58,15 @@ def __init__(self, playbook, ds):
elif isinstance(hosts, list):
hosts = ';'.join(hosts)
hosts = utils.template(hosts, playbook.extra_vars)

self._ds = ds
self.playbook = playbook
self.basedir = basedir
self.hosts = hosts
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.vars = self._get_vars()
self._tasks = ds.get('tasks', [])
self._handlers = ds.get('handlers', [])
self.remote_user = utils.template(ds.get('user', self.playbook.remote_user), playbook.extra_vars)
Expand Down Expand Up @@ -107,7 +108,7 @@ def _load_tasks(self, ds, keyname):
(k,v) = t.split("=", 1)
task_vars[k] = utils.template(v, task_vars)
include_file = utils.template(tokens[0], task_vars)
data = utils.parse_yaml_from_file(utils.path_dwim(self.playbook.basedir, include_file))
data = utils.parse_yaml_from_file(utils.path_dwim(self.basedir, include_file))
elif type(x) == dict:
data = [x]
else:
Expand Down Expand Up @@ -135,7 +136,7 @@ def handlers(self):

# *************************************************

def _get_vars(self, dirname):
def _get_vars(self):
''' 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. '''
Expand Down Expand Up @@ -264,7 +265,7 @@ def _update_vars_files_for_host(self, host):
filename3 = filename2
if host is not None:
filename3 = utils.template(filename2, self.playbook.SETUP_CACHE[host])
filename4 = utils.path_dwim(self.playbook.basedir, filename3)
filename4 = utils.path_dwim(self.basedir, filename3)
sequence.append(filename4)
if os.path.exists(filename4):
found = True
Expand Down Expand Up @@ -297,7 +298,7 @@ def _update_vars_files_for_host(self, host):
filename3 = filename2
if host is not None:
filename3 = utils.template(filename2, self.playbook.SETUP_CACHE[host])
filename4 = utils.path_dwim(self.playbook.basedir, filename3)
filename4 = utils.path_dwim(self.basedir, filename3)
if self._has_vars_in(filename4):
return
new_vars = utils.parse_yaml_from_file(filename4)
Expand Down