简体   繁体   中英

What is the proper way to read an option in INI-file on remote node with Ansible?

I am writing an Ansible role that installs and updates some specific enterprise software. I would like to compare the installed version (if it is installed) to the one I am trying to install, for various reasons, but mainly to be able to verify that installation is necessary and allowed before actually executing the installer. Both installer package and installation contain an INI-file which contains component versions as options ( component_name=version ).

What is the proper way in Ansible to read some option(s) from some INI-file on remote node? As far as I understand:

  • ini_file -module is meant for modifying target file, which is not what I want to do.
  • ini lookup is meant for files on controller, not on remote nodes.

I can see two possibilities here:

  1. Use fetch -module to get file from remote node to controller machine, then use ini lookup.
  2. Use command or shell -module, parse INI file using grep/sed/awk and register output.

The first option seems unnecessarily clumsy (although I do realize I may think about it in the wrong way). Second one seems a bit clumsy from another point of view (yet another INI-file parsing method), but I may be wrong here too. Right now I am leaning on the latter, but I can't help thinking that there must be an easier and more elegant way.

Seems like a use case for facts.d .

  1. Write a shell or Python script that inspects those ini files and dumps required fields as JSON object to stdout.

  2. Place this script into /etc/ansible/facts.d/custom_soft.fact and make it executable.

  3. Then you can use these facts as follows:

     - shell: install_custom_soft.sh when: ansible_local.custom_soft.component_ver | int > 4 

If your ini files are very simple, you may do the job even without script, just make a link like this:

ln -s /etc/custom_soft/config.ini /etc/ansible/facts.d/custom_soft.fact

and all config.ini keys will be available to Ansible via ansible_local.custom_soft variable.

PS Despite the name "local facts" this should be done on remote machine .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM