1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

misc code cleanup

This commit is contained in:
Michael DeHaan 2012-07-15 11:20:59 -04:00
parent 031ba43d22
commit 6341e75e33

View file

@ -29,17 +29,14 @@ from play import Play
############################################# #############################################
class PlayBook(object): class PlayBook(object):
''' '''
runs an ansible playbook, given as a datastructure runs an ansible playbook, given as a datastructure or YAML filename.
or YAML filename. a playbook is a deployment, config A playbook is a deployment, config management, or automation based
management, or automation based set of commands to set of commands to run in series.
run in series.
multiple plays/tasks do not execute simultaneously, multiple plays/tasks do not execute simultaneously, but tasks in each
but tasks in each pattern do execute in parallel pattern do execute in parallel (according to the number of forks
(according to the number of forks requested) among requested) among the hosts they address
the hosts they address
''' '''
# ***************************************************** # *****************************************************
@ -89,7 +86,6 @@ class PlayBook(object):
if extra_vars is None: if extra_vars is None:
extra_vars = {} extra_vars = {}
if only_tags is None: if only_tags is None:
only_tags = [ 'all' ] only_tags = [ 'all' ]
@ -112,22 +108,20 @@ class PlayBook(object):
self.private_key_file = private_key_file self.private_key_file = private_key_file
self.only_tags = only_tags self.only_tags = only_tags
self.inventory = ansible.inventory.Inventory(host_list) self.inventory = ansible.inventory.Inventory(host_list)
if not self.inventory._is_script: if not self.inventory._is_script:
self.global_vars.update(self.inventory.get_group_variables('all')) self.global_vars.update(self.inventory.get_group_variables('all'))
self.basedir = os.path.dirname(playbook) self.basedir = os.path.dirname(playbook)
self.playbook = self._load_playbook_from_file(playbook) self.playbook = self._load_playbook_from_file(playbook)
self.module_path = self.module_path + os.pathsep + os.path.join(self.basedir, "library") self.module_path = self.module_path + os.pathsep + os.path.join(self.basedir, "library")
# ***************************************************** # *****************************************************
def _load_playbook_from_file(self, path): def _load_playbook_from_file(self, path):
''' '''
do some top level error checking on playbooks and allow them to include other run top level error checking on playbooks and allow them to include other playbooks.
playbooks.
''' '''
playbook_data = utils.parse_yaml_from_file(path) playbook_data = utils.parse_yaml_from_file(path)
@ -225,18 +219,16 @@ class PlayBook(object):
# load up an appropriate ansible runner to run the task in parallel # load up an appropriate ansible runner to run the task in parallel
results = self._run_task_internal(task) results = self._run_task_internal(task)
# add facts to the global setup cache
for host, result in results['contacted'].iteritems():
if "ansible_facts" in result:
for k,v in result['ansible_facts'].iteritems():
self.SETUP_CACHE[host][k]=v
self.stats.compute(results)
# if no hosts are matched, carry on # if no hosts are matched, carry on
if results is None: if results is None:
results = {} results = {}
# add facts to the global setup cache
for host, result in results['contacted'].iteritems():
facts = results.get('ansible_facts', {})
self.SETUP_CACHE[host].update(facts)
self.stats.compute(results)
# flag which notify handlers need to be run # flag which notify handlers need to be run
if len(task.notify) > 0: if len(task.notify) > 0:
@ -266,7 +258,6 @@ class PlayBook(object):
# ***************************************************** # *****************************************************
def _do_setup_step(self, play): def _do_setup_step(self, play):
''' get facts from the remote system ''' ''' get facts from the remote system '''
host_list = [ h for h in self.inventory.list_hosts(play.hosts) host_list = [ h for h in self.inventory.list_hosts(play.hosts)
@ -275,15 +266,12 @@ class PlayBook(object):
if not play.gather_facts: if not play.gather_facts:
return {} return {}
setup_args = {}
self.callbacks.on_setup() self.callbacks.on_setup()
self.inventory.restrict_to(host_list) self.inventory.restrict_to(host_list)
# push any variables down to the system # push any variables down to the system
setup_results = ansible.runner.Runner( setup_results = ansible.runner.Runner(
pattern=play.hosts, module_name='setup', module_args=setup_args, inventory=self.inventory, pattern=play.hosts, module_name='setup', module_args={}, inventory=self.inventory,
forks=self.forks, module_path=self.module_path, timeout=self.timeout, remote_user=play.remote_user, forks=self.forks, module_path=self.module_path, timeout=self.timeout, remote_user=play.remote_user,
remote_pass=self.remote_pass, remote_port=play.remote_port, private_key_file=self.private_key_file, remote_pass=self.remote_pass, remote_port=play.remote_port, private_key_file=self.private_key_file,
setup_cache=self.SETUP_CACHE, callbacks=self.runner_callbacks, sudo=play.sudo, sudo_user=play.sudo_user, setup_cache=self.SETUP_CACHE, callbacks=self.runner_callbacks, sudo=play.sudo, sudo_user=play.sudo_user,
@ -297,8 +285,7 @@ class PlayBook(object):
# let runner template out future commands # let runner template out future commands
setup_ok = setup_results.get('contacted', {}) setup_ok = setup_results.get('contacted', {})
for (host, result) in setup_ok.iteritems(): for (host, result) in setup_ok.iteritems():
if 'ansible_facts' in result: self.SETUP_CACHE[host] = result.get('ansible_facts', {})
self.SETUP_CACHE[host] = result['ansible_facts']
return setup_results return setup_results
# ***************************************************** # *****************************************************