如何将配置变量传递给集合中的插件?

huangapple go评论78阅读模式
英文:

How do I pass configuration variable to plugins in a collection?

问题

我有一个用于管理TrueNAS设备的Ansible集合。核心部分是一个实用程序类,用于与中间件守护程序通信,以检查配置并进行更改。有一个命令行实用程序用于与守护程序通信,还有一个Python模块。我想允许Ansible用户选择要使用的方法,就像他们可以选择如何通过SSH连接到远程机器或如何成为root一样。换句话说,在我的插件代码中的某个地方,我将有:

if method_to_use == 'First':
    method = FirstMethod()
else:
    method = SecondMethod()

但是如何在playbook中设置method_to_use,然后在已在远程机器上调用的插件中检索它呢?

我理解的是,清单变量在插件内部不可用。但由于我的所有模块都是集合的一部分,也许有类似于集合级别的变量或事实可以设置?

显然,我可以为每个模块添加一个method: {first|second}选项,但我宁愿不让用户在他们的playbook的每个play中都添加它。

我见过一种方法,即任何客户端上的第一个play是类似于以下内容的东西:

tasks:
  - set_config:
      method: first

其中set_config是一个插件,它接受其method选项并将其存储在客户端的文件中。然后稍后的插件读取该文件以获取配置。但这似乎有点像一个hack。我想知道是否有更优雅的解决方案。

英文:

I have an Ansible collection for managing TrueNAS boxes. The core is a utility class that talks to the middleware daemon to check the configuration and make changes. There's a command-line utility to talk to the daemon, but there's also a Python module. I'd like to allow the Ansible user to choose which approach to use, just as they can choose how to ssh to a remote machine, or how to become root. In other words, somewhere in my plugin code I'll have

if method_to_use == 'First':
    method = FirstMethod()
else:
    method = SecondMethod()

But how do I set method_to_use in a playbook, then retrieve it in a plugin that's been called on the remote machine?

My understanding is that inventory variables aren't available inside plugins. But since all of my modules are part of a collection, maybe there's something like a collection-level variable or fact that could be set?

Obviously, I could just have a method: {first|second} option for each module, but I'd prefer not to make the user add that to every single play in their playbook.

I've seen one approach where the first play on any client is something like:

tasks:
  - set_config:
      method: first

where set_config is a plugin that takes its method option and stashes it in a file on the client. Then later plugins read that file to get the configuration. But that seems like a hack. I'm wondering whether there might be a more elegant solution.

答案1

得分: 3

没有像"collection level"设置那样可以在您的playbooks中指定的设置。

为了避免在每个任务中都提供它,您可以(a) 在ansible.cfg中进行设置,或者(b) 从环境变量获取您的值。

community.hashi_vault模块正是这样做的,因此请查看它们的源代码以获取示例。

例如,hashi_vault查找模块将从ansible.cfg[hashi_vault_collection]部分读取配置设置,或者它将从特定的ANSIBLE_HASHI_VAULT_*环境变量中读取,或者它将读取显式参数。


如果您编写插件以从环境变量获取默认值,您可以利用Ansible允许您在play级别设置这些值的特性:

- hosts: all
  environment:
    MY_MODULE_DEFAULT_COLOR: blue
    MY_MODULE_FAILURE_ACTION: explode
  tasks:
    - my_module:
        use_method: first

    - my_module:
        use_method: second
英文:

There is nothing like a "collection level" setting that can be specified in your playbooks.

To avoid having to provide it on every task, you can (a) set things in ansible.cfg or (b) get your value from an environment variable.

The community.hashi_vault modules do exactly this, so see their source code for an example.

For example, the hashi_vault lookup module will read config settings from the [hashi_vault_collection] section of ansible.cfg, or it will read from specific ANSIBLE_HASHI_VAULT_* environment variables, or it will read explicit parameters.


If you write your plugin to get defaults from environment variables, you can take advantage of the fact that Ansible allows you to set these at the play level:

- hosts: all
  environment:
    MY_MODULE_DEFAULT_COLOR: blue
    MY_MODULE_FAILURE_ACTION: explode
  tasks:
    - my_module:
        use_method: first

    - my_module:
        use_method: second

huangapple
  • 本文由 发表于 2023年6月29日 05:02:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576684.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定