Split conditional imports in playbook into subfunction, fix small bug in event reporting on playbook

actions.
This commit is contained in:
Michael DeHaan 2012-03-23 21:03:25 -04:00
parent b43019f3a1
commit 44d4dede92
2 changed files with 49 additions and 52 deletions

View file

@ -220,9 +220,9 @@ class PlayBook(object):
elif 'skipped' in host_result: elif 'skipped' in host_result:
self.skipped[host] = self.skipped.get(host, 0) + 1 self.skipped[host] = self.skipped.get(host, 0) + 1
self.callbacks.on_skipped(host) self.callbacks.on_skipped(host)
if poll: elif poll:
continue continue
if not setup and ('changed' in host_result): elif not setup and ('changed' in host_result):
self.invocations[host] = self.invocations.get(host, 0) + 1 self.invocations[host] = self.invocations.get(host, 0) + 1
self.changed[host] = self.changed.get(host, 0) + 1 self.changed[host] = self.changed.get(host, 0) + 1
self.callbacks.on_ok(host, host_result) self.callbacks.on_ok(host, host_result)
@ -402,55 +402,61 @@ class PlayBook(object):
# ***************************************************** # *****************************************************
def _do_setup_step(self, pattern, vars, user, host_list, vars_files=None): def _do_conditional_imports(self, vars_files, host_list):
''' push variables down to the systems and get variables+facts back up ''' ''' handle the vars_files section, which can contain variables '''
# this enables conditional includes like $facter_os.yml and is only done
# after the original pass when we have that data.
#
# FIXME: refactor into subfunction
# FIXME: save parsed variable results in memory to avoid excessive re-reading/parsing # FIXME: save parsed variable results in memory to avoid excessive re-reading/parsing
# FIXME: currently parses imports for hosts not in the pattern, that is not wrong, but it's # FIXME: currently parses imports for hosts not in the pattern, that is not wrong, but it's
# not super optimized yet either, because we wouldn't have hit them, ergo # not super optimized yet either, because we wouldn't have hit them, ergo
# it will raise false errors if there is no defaults variable file without any $vars # it will raise false errors if there is no defaults variable file without any $vars
# in it, which could happen on uncontacted hosts. # in it, which could happen on uncontacted hosts.
if vars_files is not None: if type(vars_files) != list:
if type(vars_files) != list: raise errors.AnsibleError("vars_files must be a list")
raise errors.AnsibleError("vars_files must be a list") for host in host_list:
self.callbacks.on_setup_secondary() cache_vars = SETUP_CACHE.get(host,{})
for host in host_list: SETUP_CACHE[host] = {}
cache_vars = SETUP_CACHE.get(host,{}) for filename in vars_files:
SETUP_CACHE[host] = {} if type(filename) == list:
for filename in vars_files: # loop over all filenames, loading the first one, and failing if # none found
if type(filename) == list: found = False
# loop over all filenames, loading the first one, and failing if sequence = []
# none found for real_filename in filename:
found = False filename2 = utils.path_dwim(self.basedir, utils.template(real_filename, cache_vars))
sequence = [] sequence.append(filename2)
for real_filename in filename: if os.path.exists(filename2):
filename2 = utils.path_dwim(self.basedir, utils.template(real_filename, cache_vars)) found = True
sequence.append(filename2) data = utils.parse_yaml_from_file(filename2)
if os.path.exists(filename2): SETUP_CACHE[host].update(data)
found = True self.callbacks.on_import_for_host(host, filename2)
data = utils.parse_yaml_from_file(filename2) break
SETUP_CACHE[host].update(data) else:
self.callbacks.on_import_for_host(host, filename2) self.callbacks.on_not_import_for_host(host, filename2)
break if not found:
else: raise errors.AnsibleError(
self.callbacks.on_not_import_for_host(host, filename2) "%s: FATAL, no files matched for vars_files import sequence: %s" % (host, sequence)
if not found: )
raise errors.AnsibleError(
"%s: FATAL, no files matched for vars_files import sequence: %s" % (host, sequence)
)
else: else:
filename2 = utils.path_dwim(self.basedir, utils.template(filename, cache_vars)) filename2 = utils.path_dwim(self.basedir, utils.template(filename, cache_vars))
if not os.path.exists(filename2): if not os.path.exists(filename2):
raise errors.AnsibleError("no file matched for vars_file import: %s" % filename2) raise errors.AnsibleError("no file matched for vars_file import: %s" % filename2)
data = utils.parse_yaml_from_file(filename2) data = utils.parse_yaml_from_file(filename2)
SETUP_CACHE[host].update(data) SETUP_CACHE[host].update(data)
self.callbacks.on_import_for_host(host, filename2) self.callbacks.on_import_for_host(host, filename2)
# *****************************************************
def _do_setup_step(self, pattern, vars, user, host_list, vars_files=None):
''' push variables down to the systems and get variables+facts back up '''
# this enables conditional includes like $facter_os.yml and is only done
# after the original pass when we have that data.
#
if vars_files is not None:
self.callbacks.on_setup_secondary()
self._do_conditional_imports(vars_files, host_list)
else: else:
self.callbacks.on_setup_primary() self.callbacks.on_setup_primary()

View file

@ -223,15 +223,6 @@
"127.0.0.1" "127.0.0.1"
] ]
], ],
[
"ok",
[
"127.0.0.1",
{
"skipped": true
}
]
],
[ [
"task start", "task start",
[ [
@ -276,7 +267,7 @@
"changed": 2, "changed": 2,
"dark": 0, "dark": 0,
"failed": 0, "failed": 0,
"resources": 12, "resources": 11,
"skipped": 1 "skipped": 1
} }
} }