diff --git a/changelogs/fragments/fix_issue_344_allow_loop_variable_usage.yml b/changelogs/fragments/fix_issue_344_allow_loop_variable_usage.yml new file mode 100644 index 00000000..3afa7af6 --- /dev/null +++ b/changelogs/fragments/fix_issue_344_allow_loop_variable_usage.yml @@ -0,0 +1,2 @@ +minor_changes: + - Allow for usage of loop variables from :code:`apply_for` within object - Thanks @lucagubler (#344) diff --git a/plugins/action/icinga2_object.py b/plugins/action/icinga2_object.py index b09e80b2..0ed97bad 100644 --- a/plugins/action/icinga2_object.py +++ b/plugins/action/icinga2_object.py @@ -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 diff --git a/plugins/module_utils/parse.py b/plugins/module_utils/parse.py index 1e592e0e..29514a22 100644 --- a/plugins/module_utils/parse.py +++ b/plugins/module_utils/parse.py @@ -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 + '"' @@ -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() @@ -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))) @@ -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 = "+" @@ -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) @@ -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: @@ -202,7 +203,7 @@ 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['+'] @@ -210,7 +211,7 @@ def divide_chunks(l, n): 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 = "+"