使用Ansible在远程节点上的INI文件中读取选项的正确方法是什么? [英] What is the proper way to read an option in INI-file on remote node with Ansible?

查看:227
本文介绍了使用Ansible在远程节点上的INI文件中读取选项的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Ansible角色,用于安装和更新某些特定的企业软件.由于各种原因,我想将已安装的版本(如果已安装)与我要安装的版本进行比较,但主要是为了能够在实际执行安装程序之前验证是否有必要进行安装.安装程序包和安装程序都包含一个INI文件,该文件包含组件版本作为选项(component_name=version).

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).

Ansible中从远程节点上的某些INI文件读取某些选项的正确方法是什么?据我了解:

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用于修改目标文件,这不是我想要的.
  • ini查找适用于控制器上的文件,而不是远程节点上的文件.
  • 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. 使用fetch -module将文件从远程节点获取到控制器计算机,然后使用ini查找.
  2. 使用commandshell -module,使用grep/sed/awk解析INI文件并注册输出.
  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.

第一种选择似乎不必要地笨拙(尽管我确实意识到我可能会以错误的方式考虑它).从另一个角度来看,第二个似乎有点笨拙(但是还有另一个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. Right now I am leaning on the latter, but I can't help thinking that there must be an easier and more elegant way.

推荐答案

似乎像

  • 编写一个shell或Python脚本来检查这些ini文件,并将必需的字段作为JSON对象转储到stdout.

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

    将此脚本放入/etc/ansible/facts.d/custom_soft.fact并使其可执行.

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

    然后您可以使用以下事实:

    Then you can use these facts as follows:

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

  • 如果您的ini文件非常简单,即使没有脚本,您也可以执行此操作,只需创建如下链接:

    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
    

    和所有config.ini键将通过ansible_local.custom_soft变量提供给Ansible.

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

    P.S.尽管名称为"本地事实" ",但应在远程计算机上完成.

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

    这篇关于使用Ansible在远程节点上的INI文件中读取选项的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆