简体   繁体   English

使用Ansible在远程节点上读取INI文件中的选项的正确方法是什么?

[英]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. 我正在编写一个安装和更新某些特定企业软件的Ansible角色。 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 ). 安装程序包和安装都包含一个INI文件,其中包含组件版本作为选项( component_name=version )。

What is the proper way in Ansible to read some option(s) from some INI-file on remote node? Ansible从远程节点上的某些INI文件中读取某些选项的正确方法是什么? As far as I understand: 据我所理解:

  • ini_file -module is meant for modifying target file, which is not what I want to do. ini_file -module用于修改目标文件,这不是我想要做的。
  • ini lookup is meant for files on controller, not on remote nodes. ini查找是指控制器上的文件,而不是远程节点上的文件。

I can see two possibilities here: 我可以在这看到两种可能性:

  1. Use fetch -module to get file from remote node to controller machine, then use ini lookup. 使用fetch -module从远程节点获取文件到控制器机器,然后使用ini查找。
  2. Use command or shell -module, parse INI file using grep/sed/awk and register output. 使用commandshell -module,使用grep / sed / awk解析INI文件并注册输出。

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. 从另一个角度来看,第二个看起来有点笨拙(另一个INI文件解析方法),但我也可能在这里错了。 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 . 看起来像facts.d的用例。

  1. Write a shell or Python script that inspects those ini files and dumps required fields as JSON object to stdout. 编写一个shell或Python脚本来检查这些ini文件,并将所需字段作为JSON对象转储到stdout。

  2. Place this script into /etc/ansible/facts.d/custom_soft.fact and make it executable. 将此脚本放入/etc/ansible/facts.d/custom_soft.fact并使其可执行。

  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: 如果您的ini文件非常简单,即使没有脚本也可以完成工作,只需创建如下链接:

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. 并且所有config.ini密钥都可以通过ansible_local.custom_soft变量用于Ansible。

PS Despite the name "local facts" this should be done on remote machine . PS尽管名称为“本地事实”,但这应该在远程机器上完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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