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

查看:27
本文介绍了使用 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.

推荐答案

似乎是 facts.d.

  1. 编写一个 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. Despite the name "local facts" this should be done on remote machine.

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

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