ini lookup - add case sensitive option (#74630)
* Add tests for case-sensitive option * Run all test playbooks from a single file
This commit is contained in:
parent
2f87b8760d
commit
d8e6f21d9f
7 changed files with 63 additions and 6 deletions
|
@ -0,0 +1,2 @@
|
|||
minor_changes:
|
||||
- ini lookup - add case sensitive option (https://github.com/ansible/ansible/issues/74601)
|
|
@ -37,6 +37,12 @@ DOCUMENTATION = """
|
|||
default:
|
||||
description: Return value if the key is not in the ini file.
|
||||
default: ''
|
||||
case_sensitive:
|
||||
description:
|
||||
Whether key names read from C(file) should be case sensitive. This prevents
|
||||
duplicate key errors if keys only differ in case.
|
||||
default: False
|
||||
version_added: '2.12'
|
||||
"""
|
||||
|
||||
EXAMPLES = """
|
||||
|
@ -122,6 +128,8 @@ class LookupModule(LookupBase):
|
|||
paramvals = self.get_options()
|
||||
|
||||
self.cp = configparser.ConfigParser()
|
||||
if paramvals['case_sensitive']:
|
||||
self.cp.optionxform = to_native
|
||||
|
||||
ret = []
|
||||
for term in terms:
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
name = captain
|
||||
NAME = fantastic
|
|
@ -2,5 +2,4 @@
|
|||
|
||||
set -eux
|
||||
|
||||
ansible-playbook test_lookup_properties.yml -i inventory -v "$@"
|
||||
ansible-playbook test_errors.yml -i inventory -v "$@"
|
||||
ansible-playbook test_ini.yml -i inventory -v "$@"
|
||||
|
|
31
test/integration/targets/lookup_ini/test_case_sensitive.yml
Normal file
31
test/integration/targets/lookup_ini/test_case_sensitive.yml
Normal file
|
@ -0,0 +1,31 @@
|
|||
- name: Test case sensitive option
|
||||
hosts: all
|
||||
|
||||
tasks:
|
||||
- name: Lookup a file with keys that differ only in case with case sensitivity enabled
|
||||
debug:
|
||||
msg: "{{ lookup('ini', 'name', file='duplicate_case_check.ini', section='reggae', case_sensitive=True) }}"
|
||||
register: duplicate_case_sensitive_name
|
||||
|
||||
- name: Lookup a file with keys that differ only in case with case sensitivity enabled
|
||||
debug:
|
||||
msg: "{{ lookup('ini', 'NAME', file='duplicate_case_check.ini', section='reggae', case_sensitive=True) }}"
|
||||
register: duplicate_case_sensitive_NAME
|
||||
|
||||
- name: Lookup a properties file with keys that differ only in case with case sensitivity enabled
|
||||
debug:
|
||||
msg: "{{ lookup('ini', 'name', file='lookup_case_check.properties', type='properties', case_sensitive=True) }}"
|
||||
register: duplicate_case_sensitive_properties_name
|
||||
|
||||
- name: Lookup a properties file with keys that differ only in case with case sensitivity enabled
|
||||
debug:
|
||||
msg: "{{ lookup('ini', 'NAME', file='lookup_case_check.properties', type='properties', case_sensitive=True) }}"
|
||||
register: duplicate_case_sensitive_properties_NAME
|
||||
|
||||
- name: Ensure the correct case-sensitive values were retieved
|
||||
assert:
|
||||
that:
|
||||
- duplicate_case_sensitive_name.msg == 'bob'
|
||||
- duplicate_case_sensitive_NAME.msg == 'marley'
|
||||
- duplicate_case_sensitive_properties_name.msg == 'captain'
|
||||
- duplicate_case_sensitive_properties_NAME.msg == 'fantastic'
|
|
@ -7,17 +7,17 @@
|
|||
block:
|
||||
- name: Lookup a file with duplicate keys
|
||||
debug:
|
||||
msg: "{{ lookup('ini', 'reggae', file='duplicate.ini', section='reggae') }}"
|
||||
msg: "{{ lookup('ini', 'name', file='duplicate.ini', section='reggae') }}"
|
||||
ignore_errors: yes
|
||||
register: duplicate
|
||||
|
||||
- name: Lookup a file with keys that differ only in case
|
||||
debug:
|
||||
msg: "{{ lookup('ini', 'reggae', file='duplicate_case_check.ini', section='reggae') }}"
|
||||
msg: "{{ lookup('ini', 'name', file='duplicate_case_check.ini', section='reggae') }}"
|
||||
ignore_errors: yes
|
||||
register: duplicate_case_sensitive
|
||||
|
||||
- name: Ensure duplicate key errers were handled properly
|
||||
- name: Ensure duplicate key errors were handled properly
|
||||
assert:
|
||||
that:
|
||||
- duplicate is failed
|
||||
|
@ -27,7 +27,7 @@
|
|||
|
||||
- name: Lookup a file with a missing section
|
||||
debug:
|
||||
msg: "{{ lookup('ini', 'reggae', file='lookup.ini', section='missing') }}"
|
||||
msg: "{{ lookup('ini', 'name', file='lookup.ini', section='missing') }}"
|
||||
ignore_errors: yes
|
||||
register: missing_section
|
||||
|
||||
|
@ -48,3 +48,15 @@
|
|||
that:
|
||||
- bad_mojo is failed
|
||||
- '"No key to lookup was provided as first term with in string inline option" in bad_mojo.msg'
|
||||
|
||||
- name: Test invalid option
|
||||
debug:
|
||||
msg: "{{ lookup('ini', 'invalid=option') }}"
|
||||
ignore_errors: yes
|
||||
register: invalid_option
|
||||
|
||||
- name: Ensure invalid option failed
|
||||
assert:
|
||||
that:
|
||||
- invalid_option is failed
|
||||
- "'is not a valid option' in invalid_option.msg"
|
||||
|
|
3
test/integration/targets/lookup_ini/test_ini.yml
Normal file
3
test/integration/targets/lookup_ini/test_ini.yml
Normal file
|
@ -0,0 +1,3 @@
|
|||
- import_playbook: test_lookup_properties.yml
|
||||
- import_playbook: test_errors.yml
|
||||
- import_playbook: test_case_sensitive.yml
|
Loading…
Reference in a new issue