Make the ini-based validations more robust

They were failing when some of the config files they were checking
didn't exist even though that shouldn't be a problem in these particular
cases.

So this updates the ini module to handle missing files, ini sections and
values. In addition, the `failed_when` check is simplified.

Change-Id: I6c0fb3ba8b16c7a442f764479b7e3c1703e5939e
Closes-Bug: 1620549
This commit is contained in:
Tomas Sedovic 2016-09-06 12:04:38 +02:00
parent 3c031c68e6
commit 8502c3ad51
3 changed files with 29 additions and 13 deletions

View File

@ -15,9 +15,9 @@
tasks:
- name: Check ceilomter.conf for a -1 TTL setting
become: true
ini: path=/etc/ceilometer/ceilometer.conf section=database key={{ item }}
ini: path=/etc/ceilometer/ceilometer.conf section=database key={{ item }} ignore_missing_file=True
register: config_result
with_items:
- "{{ metering_ttl_check }}"
- "{{ event_ttl_check }}"
failed_when: "'{{ ttl_value }}' in config_result.value"
failed_when: "ttl_value == config_result.value"

View File

@ -36,24 +36,40 @@ def main():
path=dict(required=True, type='str'),
section=dict(required=True, type='str'),
key=dict(required=True, type='str'),
ignore_missing_file=dict(required=False, type='bool'),
))
ini_file_path = module.params.get('path')
if path.exists(ini_file_path) and path.isfile(ini_file_path):
config = ConfigParser.SafeConfigParser()
config.read(ini_file_path)
try:
value = config.get(module.params.get('section'),
module.params.get('key'))
except ConfigParser.Error as e:
module.fail_json(msg=e.message)
config.read(ini_file_path)
except Exception:
module.fail_json(msg="The file '{}' is not in a valid INI format."
.format(ini_file_path))
section = module.params.get('section')
key = module.params.get('key')
try:
value = config.get(section, key)
msg = ("The key '{}' under the section '{}' in file {} "
"has the value: '{}'"
.format(key, section, ini_file_path, value))
module.exit_json(msg=msg, changed=False, value=value)
except ConfigParser.Error:
msg = ("There is no key '{}' under the section '{}' in file {}."
.format(key, section, ini_file_path))
module.exit_json(msg=msg, changed=False, value=None)
module.exit_json(changed=False, value=value)
else:
module.fail_json(msg="Could not open the ini file: '{}'"
.format(ini_file_path))
missing_file_message = "Could not open the ini file: '{}'".format(
ini_file_path)
if module.params.get('ignore_missing_file'):
module.exit_json(msg=missing_file_message, changed=False,
value=None)
else:
module.fail_json(msg=missing_file_message)
if __name__ == '__main__':

View File

@ -14,7 +14,7 @@
tasks:
- name: Check the services for debug flag
become: true
ini: path={{ item }} section=DEFAULT key=debug
ini: path={{ item }} section=DEFAULT key=debug ignore_missing_file=True
register: config_result
with_items:
- /etc/nova/nova.conf
@ -22,4 +22,4 @@
- /etc/ceilometer/ceilometer.conf
- /etc/heat/heat.conf
- /etc/ironic/ironic.conf
failed_when: "'{{ debug_check }}' in config_result.value"
failed_when: "debug_check|bool == config_result.value|bool"