a )gc@sddlmZmZmZeZdZdZdZddl Z ddl Z ddl Z ddl m Z ddlmZddlmZdd lmZmZdd lmZmZdd lmZd d ZGdddeZdS))absolute_importdivisionprint_functiona 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 C(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' 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]b\}}d|vrR|D]}d||vr<|}q<|dksb|sv||<||q(|d|7<q(fdd |DS) z.Safely split parameter term to preserve spacescSsdS)Nrrr>/usr/lib/python3.9/site-packages/ansible/plugins/lookup/ini.pyaz_parse_params..key=z%s=r csg|] }|qSrr).0xparamsrr wrz!_parse_params..)keysr enumeratesplitappend)term paramvalsZ valid_keysrZthiskeyZidpphrasekrrr _parse_params\s   r"c@seZdZddZdddZdS) LookupModulecsR|rfdd|j|DSd}z|j|}WntjyL|YS0|S)Ncs g|]\}}t|r|qSr)rematch)rr!vrrrrrz*LookupModule.get_value..)cpitemsget configparserZ NoOptionError)selfrsectionZdfltZ is_regexpvaluerr'r get_value|s zLookupModule.get_valueNc Ksz|j||d|}tj|d|dd|_|drBt|j_g}|D](}|}d|vsjd|vr&| t ||}zVd} |D]H} d| vr| d\} } | |vrt d | | || <q||kr| }d } qWn@t y} z&t d | |t| f| d WYd} ~ n d} ~ 00| s&td |||d|d}t}|ddkr^|dd|d<|j|\}}t|d|dd}|||dtjz|j|WnBtjy}z&t dj|dt|dWYd}~n d}~00z |||d|d|d}Wn0tjy<t dj|d|ddYn0|durJt|trj|D]}||qVqJ||qJ|S)N)Z var_optionsZdirectallow_no_valueZ allow_none)r0Zcase_sensitiverrFz%s is not a valid option.Tz Could not use '%s' from '%s': %s)Zorig_exczMNo key to lookup was provided as first term with in string inline options: %sfilesfiletypeZ propertiesz[java_properties] Zjava_propertiesr-Zsurrogate_or_strictencoding)errorsr4rz%Duplicate option in '{file}': {error})r2errordefaultr$z No section '{section}' in {file})r-r2) Z set_optionsZ get_optionsr+Z ConfigParserr*r(r Z optionxformstripZ_deprecate_inline_kvr"rr ValueErrorr Zfind_file_in_search_pathrwriteZ_loaderZ_get_file_contentsr seekosSEEK_SETZreadfpZDuplicateOptionErrorformatr/ZNoSectionError isinstancerr)r,ZtermsZ variableskwargsrretrrrZ updated_keyZparamnamer.epathZconfigcontentsZ show_dataZdoevarr&rrrrunsb     0   0    zLookupModule.run)N)__name__ __module__ __qualname__r/rGrrrrr#zs r#)Z __future__rrrr3Z __metaclass__Z DOCUMENTATIONZEXAMPLESZRETURNr+r<r$ior collectionsrcollections.abcrZansible.errorsrr Zansible.module_utils._textr r Zansible.plugins.lookupr r"r#rrrrs0