mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-06-27 18:50:21 -07:00
Merge branch 'devel' of https://github.com/skvidal/ansible into skvidal-devel
This commit is contained in:
commit
02efcdced6
2 changed files with 37 additions and 11 deletions
|
@ -32,10 +32,14 @@ class Inventory(object):
|
||||||
systems, or a script that will be called with --list or --host.
|
systems, or a script that will be called with --list or --host.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, host_list=C.DEFAULT_HOST_LIST):
|
def __init__(self, host_list=C.DEFAULT_HOST_LIST, global_host_vars={}):
|
||||||
|
|
||||||
self._restriction = None
|
self._restriction = None
|
||||||
self._variables = {}
|
self._variables = {}
|
||||||
|
self._global_host_vars = global_host_vars # lets us pass in a global var list
|
||||||
|
# that each host gets
|
||||||
|
# after we have set up the inventory
|
||||||
|
# to get all group variables
|
||||||
|
|
||||||
if type(host_list) == list:
|
if type(host_list) == list:
|
||||||
self.host_list = host_list
|
self.host_list = host_list
|
||||||
|
@ -98,6 +102,9 @@ class Inventory(object):
|
||||||
|
|
||||||
return variables
|
return variables
|
||||||
|
|
||||||
|
def get_global_vars(self):
|
||||||
|
return self._global_host_vars
|
||||||
|
|
||||||
# *****************************************************
|
# *****************************************************
|
||||||
|
|
||||||
def _parse_from_file(self):
|
def _parse_from_file(self):
|
||||||
|
@ -177,9 +184,24 @@ class Inventory(object):
|
||||||
|
|
||||||
hosts = []
|
hosts = []
|
||||||
groups = {}
|
groups = {}
|
||||||
|
|
||||||
ungrouped = []
|
ungrouped = []
|
||||||
|
|
||||||
|
|
||||||
|
# go through once and grab up the global_host_vars from the 'all' group
|
||||||
|
for item in data:
|
||||||
|
if type(item) == dict:
|
||||||
|
if "group" in item and 'all' == item["group"]:
|
||||||
|
if "vars" in item:
|
||||||
|
variables = item['vars']
|
||||||
|
if type(variables) == list:
|
||||||
|
for variable in variables:
|
||||||
|
if len(variable) != 1:
|
||||||
|
raise errors.AnsibleError("Only one item expected in %s"%(variable))
|
||||||
|
k, v = variable.items()[0]
|
||||||
|
self._global_host_vars[k] = utils.template(v, self._global_host_vars, {})
|
||||||
|
elif type(variables) == dict:
|
||||||
|
self._global_host_vars.update(variables)
|
||||||
|
|
||||||
for item in data:
|
for item in data:
|
||||||
if type(item) == dict:
|
if type(item) == dict:
|
||||||
if "group" in item:
|
if "group" in item:
|
||||||
|
@ -187,7 +209,7 @@ class Inventory(object):
|
||||||
|
|
||||||
group_vars = []
|
group_vars = []
|
||||||
if "vars" in item:
|
if "vars" in item:
|
||||||
group_vars = item["vars"]
|
group_vars.extend(item["vars"])
|
||||||
|
|
||||||
group_hosts = []
|
group_hosts = []
|
||||||
if "hosts" in item:
|
if "hosts" in item:
|
||||||
|
|
|
@ -103,16 +103,20 @@ class PlayBook(object):
|
||||||
self.sudo = sudo
|
self.sudo = sudo
|
||||||
self.sudo_pass = sudo_pass
|
self.sudo_pass = sudo_pass
|
||||||
self.extra_vars = extra_vars
|
self.extra_vars = extra_vars
|
||||||
|
self.global_vars = {}
|
||||||
self.basedir = os.path.dirname(playbook)
|
|
||||||
self.playbook = self._parse_playbook(playbook)
|
|
||||||
|
|
||||||
if override_hosts is not None:
|
if override_hosts is not None:
|
||||||
if type(override_hosts) != list:
|
if type(override_hosts) != list:
|
||||||
raise errors.AnsibleError("override hosts must be a list")
|
raise errors.AnsibleError("override hosts must be a list")
|
||||||
|
self.global_vars.update(ansible.inventory.Inventory(host_list).get_global_vars())
|
||||||
self.inventory = ansible.inventory.Inventory(override_hosts)
|
self.inventory = ansible.inventory.Inventory(override_hosts)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self.inventory = ansible.inventory.Inventory(host_list)
|
self.inventory = ansible.inventory.Inventory(host_list)
|
||||||
|
self.global_vars.update(ansible.inventory.Inventory(host_list).get_global_vars())
|
||||||
|
|
||||||
|
self.basedir = os.path.dirname(playbook)
|
||||||
|
self.playbook = self._parse_playbook(playbook)
|
||||||
|
|
||||||
# *****************************************************
|
# *****************************************************
|
||||||
|
|
||||||
|
@ -123,7 +127,7 @@ class PlayBook(object):
|
||||||
play['vars'] = {}
|
play['vars'] = {}
|
||||||
if type(play['vars']) not in [dict, list]:
|
if type(play['vars']) not in [dict, list]:
|
||||||
raise errors.AnsibleError("'vars' section must contain only key/value pairs")
|
raise errors.AnsibleError("'vars' section must contain only key/value pairs")
|
||||||
vars = {}
|
vars = self.global_vars
|
||||||
|
|
||||||
# translate a list of vars into a dict
|
# translate a list of vars into a dict
|
||||||
if type(play['vars']) == list:
|
if type(play['vars']) == list:
|
||||||
|
@ -131,7 +135,7 @@ class PlayBook(object):
|
||||||
k, v = item.items()[0]
|
k, v = item.items()[0]
|
||||||
vars[k] = v
|
vars[k] = v
|
||||||
else:
|
else:
|
||||||
vars = play['vars']
|
vars.update(play['vars'])
|
||||||
|
|
||||||
vars_prompt = play.get('vars_prompt', {})
|
vars_prompt = play.get('vars_prompt', {})
|
||||||
if type(vars_prompt) != dict:
|
if type(vars_prompt) != dict:
|
||||||
|
@ -151,9 +155,9 @@ class PlayBook(object):
|
||||||
''' load tasks included from external files. '''
|
''' load tasks included from external files. '''
|
||||||
|
|
||||||
# include: some.yml a=2 b=3 c=4
|
# include: some.yml a=2 b=3 c=4
|
||||||
include_tokens = task['include'].split()
|
|
||||||
path = utils.path_dwim(dirname, include_tokens[0])
|
|
||||||
play_vars = self._get_vars(play, dirname)
|
play_vars = self._get_vars(play, dirname)
|
||||||
|
include_tokens = utils.template(task['include'], play_vars, SETUP_CACHE).split()
|
||||||
|
path = utils.path_dwim(dirname, include_tokens[0])
|
||||||
include_vars = {}
|
include_vars = {}
|
||||||
for i,x in enumerate(include_tokens):
|
for i,x in enumerate(include_tokens):
|
||||||
if x.find("=") != -1:
|
if x.find("=") != -1:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue