mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-08-22 14:01:42 -07:00
Revert "Merge pull request #6287 from risaacson/fix_hash_in_var"
This reverts commita808287451
, reversing changes made to6129ea7566
.
This commit is contained in:
parent
a808287451
commit
b41d8106ff
5 changed files with 92 additions and 152 deletions
|
@ -23,12 +23,10 @@ from ansible.inventory.group import Group
|
|||
from ansible.inventory.expand_hosts import detect_range
|
||||
from ansible.inventory.expand_hosts import expand_hostname_range
|
||||
from ansible import errors
|
||||
import ansible.utils as utils
|
||||
import shlex
|
||||
import re
|
||||
import ast
|
||||
|
||||
|
||||
class InventoryParser(object):
|
||||
"""
|
||||
Host inventory for ansible.
|
||||
|
@ -49,6 +47,7 @@ class InventoryParser(object):
|
|||
self._parse_group_variables()
|
||||
return self.groups
|
||||
|
||||
|
||||
# [webservers]
|
||||
# alpha
|
||||
# beta:2345
|
||||
|
@ -66,16 +65,9 @@ class InventoryParser(object):
|
|||
active_group_name = 'ungrouped'
|
||||
|
||||
for line in self.lines:
|
||||
|
||||
# Split off any comments that are not contained in a variable.
|
||||
if "#" in line:
|
||||
line = utils.split_unquoted_hash(line)
|
||||
|
||||
# Clean up the end of the line.
|
||||
line = line.strip()
|
||||
|
||||
line = line.split("#")[0].strip()
|
||||
if line.startswith("[") and line.endswith("]"):
|
||||
active_group_name = line.replace("[", "").replace("]", "")
|
||||
active_group_name = line.replace("[","").replace("]","")
|
||||
if line.find(":vars") != -1 or line.find(":children") != -1:
|
||||
active_group_name = active_group_name.rsplit(":", 1)[0]
|
||||
if active_group_name not in self.groups:
|
||||
|
@ -103,18 +95,20 @@ class InventoryParser(object):
|
|||
if hostname.count(".") == 1:
|
||||
(hostname, port) = hostname.rsplit(".", 1)
|
||||
elif (hostname.find("[") != -1 and
|
||||
hostname.find("]") != -1 and
|
||||
hostname.find(":") != -1 and
|
||||
(hostname.rindex("]") < hostname.rindex(":")) or
|
||||
(hostname.find("]") == -1 and hostname.find(":") != -1)):
|
||||
(hostname, port) = hostname.rsplit(":", 1)
|
||||
hostname.find("]") != -1 and
|
||||
hostname.find(":") != -1 and
|
||||
(hostname.rindex("]") < hostname.rindex(":")) or
|
||||
(hostname.find("]") == -1 and hostname.find(":") != -1)):
|
||||
(hostname, port) = hostname.rsplit(":", 1)
|
||||
|
||||
hostnames = []
|
||||
if detect_range(hostname):
|
||||
hostnames = expand_hostname_range(hostname)
|
||||
else:
|
||||
hostnames = [hostname]
|
||||
|
||||
for hn in hostnames:
|
||||
host = None
|
||||
if hn in self.hosts:
|
||||
host = self.hosts[hn]
|
||||
else:
|
||||
|
@ -125,24 +119,15 @@ class InventoryParser(object):
|
|||
if t.startswith('#'):
|
||||
break
|
||||
try:
|
||||
(k, v) = t.split("=", 1)
|
||||
(k,v) = t.split("=", 1)
|
||||
except ValueError, e:
|
||||
raise errors.AnsibleError("Invalid ini entry: %s - %s" % (t, str(e)))
|
||||
# I am not sure where a variable with a hash needs to be evaluated via ast.
|
||||
# If an instance comes up this is the condition we need to modify.
|
||||
if "#" in v:
|
||||
host.set_variable(k, v)
|
||||
else:
|
||||
try:
|
||||
host.set_variable(k, ast.literal_eval(v))
|
||||
# Using explicit exceptions.
|
||||
# Likely a string that literal_eval does not like. We wil then just set it.
|
||||
except ValueError:
|
||||
# For some reason this was thought to be malformed.
|
||||
host.set_variable(k, v)
|
||||
except SyntaxError:
|
||||
# Is this a hash with an equals at the end?
|
||||
host.set_variable(k, v)
|
||||
try:
|
||||
host.set_variable(k,ast.literal_eval(v))
|
||||
except:
|
||||
# most likely a string that literal_eval
|
||||
# doesn't like, so just set it
|
||||
host.set_variable(k,v)
|
||||
self.groups[active_group_name].add_host(host)
|
||||
|
||||
# [southeast:children]
|
||||
|
@ -157,7 +142,7 @@ class InventoryParser(object):
|
|||
if line is None or line == '':
|
||||
continue
|
||||
if line.startswith("[") and line.find(":children]") != -1:
|
||||
line = line.replace("[", "").replace(":children]", "")
|
||||
line = line.replace("[","").replace(":children]","")
|
||||
group = self.groups.get(line, None)
|
||||
if group is None:
|
||||
group = self.groups[line] = Group(name=line)
|
||||
|
@ -172,6 +157,7 @@ class InventoryParser(object):
|
|||
else:
|
||||
group.add_child_group(kid_group)
|
||||
|
||||
|
||||
# [webservers:vars]
|
||||
# http_port=1234
|
||||
# maxRequestsPerChild=200
|
||||
|
@ -181,7 +167,7 @@ class InventoryParser(object):
|
|||
for line in self.lines:
|
||||
line = line.strip()
|
||||
if line.startswith("[") and line.find(":vars]") != -1:
|
||||
line = line.replace("[", "").replace(":vars]", "")
|
||||
line = line.replace("[","").replace(":vars]","")
|
||||
group = self.groups.get(line, None)
|
||||
if group is None:
|
||||
raise errors.AnsibleError("can't add vars to undefined group: %s" % line)
|
||||
|
|
|
@ -1071,37 +1071,3 @@ def random_password(length=20, chars=C.DEFAULT_PASSWORD_CHARS):
|
|||
password.append(new_char)
|
||||
|
||||
return ''.join(password)
|
||||
|
||||
|
||||
def split_unquoted_hash(line):
|
||||
'''
|
||||
Carve off comments from a line which are not contained in quotes and a part of an assignment.
|
||||
'''
|
||||
|
||||
# We would really like to have this using a regex to make it less code. For instance:
|
||||
# line = re.split('(?<!=["|\'].*)#(?!.*?["|\']).*', line)[0]
|
||||
# this has the problem that it comes back with a "sre_constants.error: look-behind requires fixed-width pattern"
|
||||
|
||||
if "#" in line:
|
||||
split_line = line.split("#")
|
||||
instances = len(split_line) - 1
|
||||
if instances > 0:
|
||||
marker = 0
|
||||
while marker < instances:
|
||||
if ("=\"" in split_line[marker] and "\"" in split_line[marker + 1]) or (
|
||||
"='" in split_line[marker] and "'" in split_line[marker + 1]):
|
||||
marker += 1
|
||||
else:
|
||||
if marker == 0:
|
||||
line = split_line[marker]
|
||||
else:
|
||||
# We have multiple fragments that we need to combine back together.
|
||||
# rekram is us reversing that work we did with marker.
|
||||
rekram = 0
|
||||
new_line = split_line[rekram]
|
||||
while marker > rekram:
|
||||
rekram += 1
|
||||
new_line = new_line + "#" + split_line[rekram]
|
||||
line = new_line
|
||||
break
|
||||
return line
|
Loading…
Add table
Add a link
Reference in a new issue