Skip to content

Commit

Permalink
Fix bug when using 'key' or 'value' variables (#346)
Browse files Browse the repository at this point in the history
Enable any name for key/value in apply_for loop
  • Loading branch information
lucagubler authored Dec 17, 2024
1 parent e945904 commit 738076b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- Allow for usage of loop variables from :code:`apply_for` within object - Thanks @lucagubler (#344)
6 changes: 5 additions & 1 deletion plugins/action/icinga2_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ def run(self, tmp=None, task_vars=None):
#
# parser
#
object_content += Icinga2Parser().parse(obj['args'], list(task_vars['icinga2_combined_constants'].keys())+task_vars['icinga2_reserved']+varlist+list(obj['args'].keys()), 2) + '}\n'
object_content += Icinga2Parser().parse(
obj['args'],
list(task_vars['icinga2_combined_constants'].keys()) + task_vars['icinga2_reserved'] + varlist + list(obj['args'].keys()),
2
) + '}\n'
copy_action = self._task.copy()
copy_action.args = dict()
copy_action.args['dest'] = file_fragment
Expand Down
23 changes: 12 additions & 11 deletions plugins/module_utils/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Icinga2Parser(object):

def parse(self, attrs, constants, indent=0):
def attribute_types(attr):
if re.search(r'^[a-zA-Z0-9_]+$', attr):
if re.search(r'^[a-zA-Z_][a-zA-Z0-9_]*$', attr):
result = attr
else:
result = '"' + attr + '"'
Expand All @@ -16,7 +16,8 @@ def value_types(value, indent=2):
# Values without quotes
if ((re.search(r'^-?\d+\.?\d*[dhms]?$', value)) or
(re.search(r'^(true|false|null)$', value)) or
(re.search(r'^!?(host|service|user)\.', value))):
(re.search(r'^!?(host|service|user)\.', value)) or
any(value.startswith(constant + '.') for constant in constants)):
result = value
elif (re.search(r'^(True|False)$', value)):
result = value.lower()
Expand Down Expand Up @@ -90,9 +91,9 @@ def parser(row):
def process_array(items, indent=2):
result = ''
for item in items:
if type(item) is dict:
if isinstance(item, dict):
result += "\n%s{\n%s%s}, " % (' ' * indent, process_hash(attrs=item, indent=indent+2), ' ' * indent)
elif type(item) is list:
elif isinstance(item, list):
result += "[ %s], " % (process_array(item.split(','), indent=indent+2))
else:
result += "%s, " % (parser(str(item)))
Expand All @@ -105,7 +106,7 @@ def process_hash(attrs, level=3, indent=2, prefix=' '):
op = ''

for attr, value in attrs.items():
if type(value) is dict:
if isinstance(value, dict):
if "+" in value:
del value['+']
op = "+"
Expand Down Expand Up @@ -133,7 +134,7 @@ def process_hash(attrs, level=3, indent=2, prefix=' '):
else:
result += "%s%s %s= {\n%s%s}\n" % (
prefix, attribute_types(attr), op, process_hash(attrs=value, indent=indent+2), ' '*indent)
elif type(value) is list and value:
elif isinstance(value, list) and value:
if value[0] == "+":
op = "+"
value.pop(0)
Expand Down Expand Up @@ -181,13 +182,13 @@ def divide_chunks(l, n):
for x in value:
config += "%s%s %s\n" % (' '*indent, attr+' where', parser(x))
elif attr == 'vars':
if type(value) is dict:
if isinstance(value, dict):
if "+" in value:
del value['+']
config += process_hash(attrs=value, indent=indent+2, level=1, prefix=("%s%s." % (' '*indent, attr)))
elif type(value) is list:
elif isinstance(value, list):
for item in value:
if type(item) is str:
if isinstance(item, str):
config += "%s%s += %s\n" % (indent*' ', attr, re.sub(r'^[\+,-]\s+/', '', item))
else:
if "+" in item:
Expand All @@ -202,15 +203,15 @@ def divide_chunks(l, n):
value = re.sub(r'^\+\s+', '', str(value))
config += "%s%s %s= %s\n" % (' ' * indent, attr, op, parser(value))
else:
if type(value) is dict:
if isinstance(value, dict):
if "+" in value:
op = '+'
del value['+']
if bool(value):
config += "%s%s %s= {\n%s%s}\n" % (' '*indent, attr, op, process_hash(attrs=value, indent=indent+2), ' '*indent)
else:
config += "%s%s %s= {}\n" % (' '*indent, op, attr)
elif type(value) is list:
elif isinstance(value, list):
if value:
if value[0] == "+":
op = "+"
Expand Down

0 comments on commit 738076b

Please sign in to comment.