如何仅为此Playbook添加自定义Ansible连接插件?

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

How to add custom Ansible connection plugin for this playbook only?

问题

我想为 Ansible 编写一个自定义连接插件。我只想在一个特定的 playbook 中使用它。我希望将它与该 playbook 一起分发。(在同一个 git 仓库中。)

文档 中提到:

> 要仅在选定的 playbook 或 playbooks 中使用独立的插件,请将插件存储在包含 playbooks 的目录中,该目录中有正确的 plugin_type 子目录(例如 callback_pluginsinventory_plugins)。这些目录必须使用 _plugins 后缀。有关插件类型的完整列表,请参阅 使用插件

那个 "使用插件" 页面说:

> 你可以通过将自定义插件放入 connection_plugins 目录来扩展 Ansible 以支持其他传输方式(如 SNMP 或消息总线)。

假设我的 playbook 位于 ./playbook.yaml

这些文档听起来好像只需要将我的文件保存为 ./connection_plugins/my_connector.py 就可以了:

mkdir connection_plugins
curl https://raw.githubusercontent.com/ansible-collections/community.aws/e80bf933412ea5c7ab2a94af945170cb2ebd900f/plugins/connection/aws_ssm.py | sed -e 's/aws_ssm/my_connector/g' > connection_plugins/my_connector.py

所以现在我的目录看起来是这样的:

  • ./playbook.yaml
  • ./connection_plugins/my_connector.py

根据 连接插件文档 的说法,我应该能够列出所有的连接插件:

ansible-doc -t connection -l

这显示了几个插件,但其中没有 my_connector。我认为这意味着我没有将我的自定义连接插件放到正确的位置。但我相信我遵循了文档。所以我不知道为什么它不起作用。

一个仅供附近 playbook 使用的自定义连接插件的正确位置是什么?
ansible-doc -t connection -l 应该显示本地安装的插件吗?

英文:

I want to write a custom connection plugin for Ansible. I only want to use it for one particular playbook. I want to distribute it with that playbook. (Same git repo.)

The docs say:

> To use a standalone plugin only in a selected playbook or playbooks, store the plugin in a subdirectory for the correct plugin_type (for example, callback_plugins or inventory_plugins) in the directory that contains the playbooks. These directories must use the _plugins suffix. For a full list of plugin types, see Working with plugins.

That "working with plugins" page says:

> You can extend Ansible to support other transports (such as SNMP or message bus) by dropping a custom plugin into the connection_plugins directory.

Let's assume my playbook is at ./playbook.yaml.
These docs sound like it's as simple as saving my file as ./connection_plugins/my_connector.py:

mkdir connection_plugins
curl https://raw.githubusercontent.com/ansible-collections/community.aws/e80bf933412ea5c7ab2a94af945170cb2ebd900f/plugins/connection/aws_ssm.py | sed -e 's/aws_ssm/my_connector/g' > connection_plugins/my_connector.py

So now my directory looks like

  • ./playbook.yaml
  • ./connection_plugins/my_connector.py

As per the connection plugins docs, I should be able to list all connection plugins with:

ansible-doc -t connection -l

This shows several plugins, but none of them are my_connector.
I assume this means I did not put my custom connector plugin into the correct location. But I believe I followed the documentation. So I don't know why it didn't work.

What is the correct location for a custom connector plugin for be used only for a nearby playbook?
Should ansible-doc -t connection -l show locally installed plugins?

答案1

得分: 2

ansible-doc 不调用 playbook,因此默认情况下它没有加载 playbook 相邻插件的位置。与大多数不使用 playbook 的 ansible 命令一样,它支持一个 --playbook-dir 参数,您可以使用它来指定加载这些插件的路径。

然而,最好的测试方法是尝试在您的 playbook 中使用插件。如果它起作用,那就是正确位置;如果不起作用,ansible-doc 是否列出它其实并不重要。

英文:

ansible-doc does not call a playbook, so by default it doesn't have a place to load playbook-adjacent plugins from. As with most ansible commands that do not work with playbooks, it supports a --playbook-dir argument that you can use to specify the path for loading these plugins.

Ultimately, though, the best way to test if you've put the plugin in the correct location is to attempt to use it in your playbook. If it works, you have; if it doesn't, it doesn't really matter whether ansible-doc listed it.

huangapple
  • 本文由 发表于 2023年5月29日 13:49:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76354936.html
匿名

发表评论

匿名网友

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

确定