mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 04:40:22 -07:00
parent
2f33c1a1a1
commit
5553b20828
206 changed files with 1853 additions and 1870 deletions
|
@ -85,8 +85,7 @@ class BaseInventoryPlugin(object):
|
|||
def _compose(self, template, variables):
|
||||
''' helper method for pluigns to compose variables for Ansible based on jinja2 expression and inventory vars'''
|
||||
t = Templar(loader=self.loader, variables=variables)
|
||||
return t.do_template('%s%s%s' % (t.environment.variable_start_string,template,t.environment.variable_end_string), disable_lookups=True)
|
||||
|
||||
return t.do_template('%s%s%s' % (t.environment.variable_start_string, template, t.environment.variable_end_string), disable_lookups=True)
|
||||
|
||||
|
||||
class BaseFileInventoryPlugin(BaseInventoryPlugin):
|
||||
|
@ -100,8 +99,8 @@ class BaseFileInventoryPlugin(BaseInventoryPlugin):
|
|||
super(BaseFileInventoryPlugin, self).__init__(cache=None)
|
||||
|
||||
|
||||
#### Helper methods ####
|
||||
def detect_range(line = None):
|
||||
# Helper methods
|
||||
def detect_range(line=None):
|
||||
'''
|
||||
A helper function that checks a given host line to see if it contains
|
||||
a range pattern described in the docstring above.
|
||||
|
@ -110,7 +109,8 @@ def detect_range(line = None):
|
|||
'''
|
||||
return '[' in line
|
||||
|
||||
def expand_hostname_range(line = None):
|
||||
|
||||
def expand_hostname_range(line=None):
|
||||
'''
|
||||
A helper function that expands a given line that contains a pattern
|
||||
specified in top docstring, and returns a list that consists of the
|
||||
|
@ -137,7 +137,7 @@ def expand_hostname_range(line = None):
|
|||
# - also add an optional third parameter which contains the step. (Default: 1)
|
||||
# so range can be [01:10:2] -> 01 03 05 07 09
|
||||
|
||||
(head, nrange, tail) = line.replace('[','|',1).replace(']','|',1).split('|')
|
||||
(head, nrange, tail) = line.replace('[', '|', 1).replace(']', '|', 1).split('|')
|
||||
bounds = nrange.split(":")
|
||||
if len(bounds) != 2 and len(bounds) != 3:
|
||||
raise AnsibleError("host range must be begin:end or begin:end:step")
|
||||
|
@ -152,10 +152,13 @@ def expand_hostname_range(line = None):
|
|||
if not end:
|
||||
raise AnsibleError("host range must specify end value")
|
||||
if beg[0] == '0' and len(beg) > 1:
|
||||
rlen = len(beg) # range length formatting hint
|
||||
rlen = len(beg) # range length formatting hint
|
||||
if rlen != len(end):
|
||||
raise AnsibleError("host range must specify equal-length begin and end formats")
|
||||
fill = lambda _: str(_).zfill(rlen) # range sequence
|
||||
|
||||
def fill(x):
|
||||
return str(x).zfill(rlen) # range sequence
|
||||
|
||||
else:
|
||||
fill = str
|
||||
|
||||
|
@ -164,17 +167,16 @@ def expand_hostname_range(line = None):
|
|||
i_end = string.ascii_letters.index(end)
|
||||
if i_beg > i_end:
|
||||
raise AnsibleError("host range must have begin <= end")
|
||||
seq = list(string.ascii_letters[i_beg:i_end+1:int(step)])
|
||||
seq = list(string.ascii_letters[i_beg:i_end + 1:int(step)])
|
||||
except ValueError: # not an alpha range
|
||||
seq = range(int(beg), int(end)+1, int(step))
|
||||
seq = range(int(beg), int(end) + 1, int(step))
|
||||
|
||||
for rseq in seq:
|
||||
hname = ''.join((head, fill(rseq), tail))
|
||||
|
||||
if detect_range(hname):
|
||||
all_hosts.extend( expand_hostname_range( hname ) )
|
||||
all_hosts.extend(expand_hostname_range(hname))
|
||||
else:
|
||||
all_hosts.append(hname)
|
||||
|
||||
return all_hosts
|
||||
|
||||
|
|
|
@ -130,7 +130,7 @@ class InventoryModule(BaseFileInventoryPlugin):
|
|||
for line in b_data.splitlines():
|
||||
if line and line[0] in self.b_COMMENT_MARKERS:
|
||||
# Replace is okay for comment lines
|
||||
#data.append(to_text(line, errors='surrogate_then_replace'))
|
||||
# data.append(to_text(line, errors='surrogate_then_replace'))
|
||||
# Currently we only need these lines for accurate lineno in errors
|
||||
data.append(u'')
|
||||
else:
|
||||
|
@ -141,7 +141,6 @@ class InventoryModule(BaseFileInventoryPlugin):
|
|||
except Exception as e:
|
||||
raise AnsibleParserError(e)
|
||||
|
||||
|
||||
def _raise_error(self, message):
|
||||
raise AnsibleError("%s:%d: " % (self._filename, self.lineno) + message)
|
||||
|
||||
|
@ -281,7 +280,7 @@ class InventoryModule(BaseFileInventoryPlugin):
|
|||
|
||||
self._raise_error("Expected key=value, got: %s" % (line))
|
||||
|
||||
def _parse_host_definition(self, line ):
|
||||
def _parse_host_definition(self, line):
|
||||
'''
|
||||
Takes a single line and tries to parse it as a host definition. Returns
|
||||
a list of Hosts if successful, or raises an error.
|
||||
|
|
|
@ -80,7 +80,7 @@ class InventoryModule(BaseInventoryPlugin):
|
|||
# Support inventory scripts that are not prefixed with some
|
||||
# path information but happen to be in the current working
|
||||
# directory when '.' is not in PATH.
|
||||
cmd = [ path, "--list" ]
|
||||
cmd = [path, "--list"]
|
||||
|
||||
try:
|
||||
cache_key = self.get_cache_prefix(path)
|
||||
|
@ -95,7 +95,7 @@ class InventoryModule(BaseInventoryPlugin):
|
|||
|
||||
path = to_native(path)
|
||||
if stderr:
|
||||
err = to_native(stderr) + "\n"
|
||||
err = to_native(stderr) + "\n"
|
||||
|
||||
if sp.returncode != 0:
|
||||
raise AnsibleError("Inventory script (%s) had an execution error: %s " % (path, err))
|
||||
|
@ -140,14 +140,13 @@ class InventoryModule(BaseInventoryPlugin):
|
|||
try:
|
||||
got = processed.get(host, {})
|
||||
except AttributeError as e:
|
||||
raise AnsibleError("Improperly formatted host information for %s: %s" % (host,to_native(e)))
|
||||
raise AnsibleError("Improperly formatted host information for %s: %s" % (host, to_native(e)))
|
||||
|
||||
self.populate_host_vars(host, got, group)
|
||||
|
||||
except Exception as e:
|
||||
raise AnsibleParserError(to_native(e))
|
||||
|
||||
|
||||
def _parse_group(self, group, data):
|
||||
|
||||
self.inventory.add_group(group)
|
||||
|
@ -155,7 +154,7 @@ class InventoryModule(BaseInventoryPlugin):
|
|||
if not isinstance(data, dict):
|
||||
data = {'hosts': data}
|
||||
# is not those subkeys, then simplified syntax, host with vars
|
||||
elif not any(k in data for k in ('hosts','vars','children')):
|
||||
elif not any(k in data for k in ('hosts', 'vars', 'children')):
|
||||
data = {'hosts': [group], 'vars': data}
|
||||
|
||||
if 'hosts' in data:
|
||||
|
|
|
@ -71,6 +71,7 @@ from ansible.module_utils._text import to_bytes, to_text
|
|||
from ansible.parsing.utils.addresses import parse_address
|
||||
from ansible.plugins.inventory import BaseFileInventoryPlugin, detect_range, expand_hostname_range
|
||||
|
||||
|
||||
class InventoryModule(BaseFileInventoryPlugin):
|
||||
|
||||
NAME = 'yaml'
|
||||
|
@ -120,7 +121,7 @@ class InventoryModule(BaseFileInventoryPlugin):
|
|||
self.inventory.add_group(group)
|
||||
|
||||
if isinstance(group_data, dict):
|
||||
#make sure they are dicts
|
||||
# make sure they are dicts
|
||||
for section in ['vars', 'children', 'hosts']:
|
||||
if section in group_data and isinstance(group_data[section], string_types):
|
||||
group_data[section] = {group_data[section]: None}
|
||||
|
@ -178,4 +179,4 @@ class InventoryModule(BaseFileInventoryPlugin):
|
|||
'''
|
||||
Compiles the regular expressions required to parse the inventory and stores them in self.patterns.
|
||||
'''
|
||||
self.patterns['groupname'] = re.compile( r'''^[A-Za-z_][A-Za-z0-9_]*$''')
|
||||
self.patterns['groupname'] = re.compile(r'''^[A-Za-z_][A-Za-z0-9_]*$''')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue