diff --git a/library/ini.py b/library/ini.py index debed94cc..660e57cb7 100644 --- a/library/ini.py +++ b/library/ini.py @@ -52,7 +52,7 @@ def check_file(path, ignore_missing): return '' -def get_result(path, section, key): +def get_result(path, section, key, default=None): '''Get value based on section and key''' msg = '' @@ -73,10 +73,16 @@ def get_result(path, section, key): ret = ReturnValue.OK return (ret, msg, value) except ConfigParser.Error: - value = None - msg = "There is no key '{}' under the section '{}' in file {}.".format( - key, section, path) - ret = ReturnValue.KEY_NOT_FOUND + if default: + msg = ("There is no key '{}' under section '{}' in file {}. Using" + " default value '{}'".format(key, section, path, default)) + ret = ReturnValue.OK + value = default + else: + value = None + msg = "There is no key '{}' under the section '{}' in file {}.".format( + key, section, path) + ret = ReturnValue.KEY_NOT_FOUND return (ret, msg, value) @@ -102,6 +108,11 @@ options: description: - Section key to look up type: str + default: + required: false + description: + - Default value if key isn't found + type: str ignore_missing_file: required: false description: @@ -124,6 +135,7 @@ def main(): section=dict(required=True, type='str'), key=dict(required=True, type='str'), ignore_missing_file=dict(required=False, type='bool'), + default=dict(required=False, type='str'), )) ini_file_path = module.params.get('path') @@ -142,8 +154,9 @@ def main(): # Try to parse the result from ini file section = module.params.get('section') key = module.params.get('key') + default = module.params.get('default') - ret, msg, value = get_result(ini_file_path, section, key) + ret, msg, value = get_result(ini_file_path, section, key, default) if ret == ReturnValue.INVALID_FORMAT: module.fail_json(msg=msg) diff --git a/roles/ctlplane-ip-range/tasks/main.yml b/roles/ctlplane-ip-range/tasks/main.yml index 170e91705..963acd7d9 100644 --- a/roles/ctlplane-ip-range/tasks/main.yml +++ b/roles/ctlplane-ip-range/tasks/main.yml @@ -11,6 +11,7 @@ section: ctlplane-subnet key: dhcp_start ignore_missing_file: True + default: "192.0.2.5" register: dhcp_start - name: Get dhcp_end value from the undercloud.conf file @@ -20,10 +21,11 @@ section: ctlplane-subnet key: dhcp_end ignore_missing_file: True + default: "192.0.2.24" register: dhcp_end - name: Check the size of the DHCP range for overcloud nodes ip_range: - start: "{{ dhcp_start.value|default('192.0.2.5', true) }}" - end: "{{ dhcp_end.value|default('192.0.2.24', true) }}" + start: "{{ dhcp_start.value }}" + end: "{{ dhcp_end.value }}" min_size: "{{ ctlplane_iprange_min_size }}" diff --git a/tripleo_validations/tests/library/test_ini.py b/tripleo_validations/tests/library/test_ini.py index 5d7ea989a..d4d240612 100644 --- a/tripleo_validations/tests/library/test_ini.py +++ b/tripleo_validations/tests/library/test_ini.py @@ -101,6 +101,23 @@ class TestIni(base.TestCase): "in file {}.").format(tmp_name), msg) self.assertIsNone(value) + def test_get_result_key_not_found_with_default(self): + '''Test ini when key is not found but has a default''' + + tmpfile = self.create_tmp_ini() + tmp_name = os.path.relpath(tmpfile.name) + tmpfile.write(valid_content.encode('utf-8')) + tmpfile.seek(0) + ret, msg, value = validation.get_result(tmp_name, 'section', 'key', + 'foo') + tmpfile.close() + + self.assertEqual(validation.ReturnValue.OK, ret) + self.assertEqual(("There is no key 'key' under section 'section' " + "in file {}. Using default value '{}'" + ).format(tmp_name, 'foo'), msg) + self.assertEqual(value, 'foo') + def test_get_result_ok(self): '''Test ini when key is not found'''