Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 440
montiainen Avatar asked Oct 18 '22 11:10

montiainen


1 Answers

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.

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

like image 65
Konstantin Suvorov Avatar answered Oct 21 '22 03:10

Konstantin Suvorov