o nnhn@sddlmZdZdZdZddlZddlZddlZddlm Z ddl m Z ddl m Z dd lmZmZdd lmZmZdd lmZd d ZGdddeZdS)) annotationsa name: ini author: Yannig Perre (!UNKNOWN) version_added: "2.0" short_description: read data from an ini file description: - "The ini lookup reads the contents of a file in INI format C(key1=value1). This plugin retrieves the value on the right side after the equal sign C('=') of a given section C([section])." - "You can also read a property file which - in this case - does not contain section." options: _terms: description: The key(s) to look up. required: True type: description: Type of the file. 'properties' refers to the Java properties files. default: 'ini' choices: ['ini', 'properties'] file: description: Name of the file to load. default: 'ansible.ini' section: default: global description: Section where to lookup the key. re: default: False type: boolean description: Flag to indicate if the key supplied is a regexp. encoding: default: utf-8 description: Text encoding to use. default: description: Return value if the key is not in the ini file. default: '' case_sensitive: description: Whether key names read from O(file) should be case sensitive. This prevents duplicate key errors if keys only differ in case. default: False version_added: '2.12' allow_no_value: description: - Read an ini file which contains key without value and without '=' symbol. type: bool default: False aliases: ['allow_none'] version_added: '2.12' seealso: - ref: playbook_task_paths description: Search paths used for relative files. a - ansible.builtin.debug: msg="User in integration is {{ lookup('ansible.builtin.ini', 'user', section='integration', file='users.ini') }}" - ansible.builtin.debug: msg="User in production is {{ lookup('ansible.builtin.ini', 'user', section='production', file='users.ini') }}" - ansible.builtin.debug: msg="user.name is {{ lookup('ansible.builtin.ini', 'user.name', type='properties', file='user.properties') }}" - ansible.builtin.debug: msg: "{{ item }}" loop: "{{ q('ansible.builtin.ini', '.*', section='section1', file='test.ini', re=True) }}" - name: Read an ini file with allow_no_value ansible.builtin.debug: msg: "{{ lookup('ansible.builtin.ini', 'user', file='mysql.ini', section='mysqld', allow_no_value=True) }}" z` _raw: description: - value(s) of the key(s) in the ini file type: list elements: str N)StringIO) defaultdict)MutableSequence)AnsibleLookupErrorAnsibleOptionsError)to_text to_native) LookupBasecs|}tddg}d}t|D]1\}}d|vr)|D] }d||vr(|}q|dks1|s;||<||q|d|7<qfdd |DS) z.Safely split parameter term to preserve spacescSsdS)Nr r r E/usr/local/lib/python3.10/dist-packages/ansible/plugins/lookup/ini.pycsz_parse_params..key=z%s=r csg|]}|qSr r ).0xparamsr r ysz!_parse_params..)keysr enumeratesplitappend)term paramvals valid_keysrthiskeyidpphrasekr rr _parse_params^s   r"c@seZdZddZdddZdS) LookupModulecsP|rfdd|j|DSd}z |j|}W|Stjy'|YSw)Ncs g|] \}}t|r|qSr )rematch)rr!vrr r rs z*LookupModule.get_value..)cpitemsget configparser NoOptionError)selfrsectiondflt is_regexpvaluer r'r get_value~szLookupModule.get_valueNc Ksn|j||d|}tj|d|dd|_|dr!t|j_g}|D]}|}d|vs4d|vr| t ||}z7d} d} |D](} d| vrd| d\} } | |vr[t d | | | | d } qD||krl| }d } qD| rs|}Wnty}z t d | |t|f|d d}~ww| std |||d|d}t}|ddkr|dd|d<|j|\}}t|d|dd}|||dtjz|j|Wntjy}z t dj|dt|dd}~wwz|||d|d|d}Wntjyt dj|d|ddw|dur4t|tr/|D]}| |q%q%| |q%|S)N) var_optionsdirectallow_no_value allow_none)r5case_sensitiverrFz%s is not a valid option.Tz Could not use '%s' from '%s': %s)orig_exczMNo key to lookup was provided as first term with in string inline options: %sfilesfiletype propertiesz[java_properties] java_propertiesr.surrogate_or_strictencoding)errorsr?rz%Duplicate option in '{file}': {error})r:errordefaultr$z No section '{section}' in {file})r.r:)! set_options get_optionsr+ ConfigParserr*r(r optionxformstrip_deprecate_inline_kvr"rr set_option ValueErrorrfind_file_in_search_pathrwrite_loader_get_file_contentsrseekosSEEK_SET read_fileDuplicateOptionErrorformatr2NoSectionError isinstancerr)r-terms variableskwargsrretrrr updated_keyupdated_optionsparamnamer1epathconfigcontents show_datadoevarr&r r r runs|            zLookupModule.run)N)__name__ __module__ __qualname__r2rfr r r r r#|s r#) __future__r DOCUMENTATIONEXAMPLESRETURNr+rPr$ior collectionsrcollections.abcransible.errorsrr+ansible.module_utils.common.text.convertersrr ansible.plugins.lookupr r"r#r r r r s 3