如何使用 Ansible 标签调用角色? [英] How to call a role using Ansible tags ??

查看:19
本文介绍了如何使用 Ansible 标签调用角色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个像roles/base/tasks/main.yml&这样的结构.在roles/目录正上方的另一个文件名为base.yml,它基本上调用了基本角色.我的问题是这个,ansible 文档建议

角色:- { 角色:网络服务器,端口:5000,标签:[ 'web', 'foo' ] }

如果我有一个定义为web"的标签,则调用 main.yml.我试过了,但没有用&是的,我也在 reddit 上看到了一个帖子 ---> https://www.reddit.com/r/ansible/comments/3628s8/question_about_using_tags_in_roles/

有没有办法使用 .yml 而不是 adhoc 命令来调用角色中定义的标签,因为我的部署策略不允许我运行 adhoc 命令

解决方案

不,没有办法指定活动标签,只能通过 --tags 从命令行调用它.

您的示例:

角色:- { 角色:网络服务器,端口:5000,标签:[ 'web', 'foo' ] }

实际上会用标签webfoo来标记角色webserver的每一个任务.

我认为你想要的是只触发 webserver 中标记为 web 和/或 foo 的任务.目前没有内置方式.

可能有一个解决方案,通过创建一个自定义操作插件,将标签注入 Ansible 的 runner 对象.动作插件根本没有记录,您需要查看一些已实现动作的来源.我曾经创建了一个 动作插件,它与 runner 对象交互,注入变量.标签可能以类似的方式工作.如果您遵循这条路径,请注意 Ansible 2 将包含大量重构,我几乎可以肯定像这样的插件会损坏.

So i am having a structure like roles/base/tasks/main.yml & another file right above the roles/ directory with name base.yml which basically calls the base role. My problem is this, ansible docs suggest

roles:
  - { role: webserver, port: 5000, tags: [ 'web', 'foo' ] }

to call the main.yml if i have a tag defined as 'web'. I tried it but it didn't work & yes i saw a post on reddit too ---> https://www.reddit.com/r/ansible/comments/3628s8/question_about_using_tags_in_roles/

Soo is there a WAY to call tags defined in a role using .yml instead of the adhoc command , coz my deployment strategies don't allow me to run a adhoc command

解决方案

No, there is no way to specify the active tags other than invoking it from the command line with --tags.

Your example here:

roles:
  - { role: webserver, port: 5000, tags: [ 'web', 'foo' ] }

will actually tag every single task of the role webserver with the tags web and foo.

I think what you want instead is to only trigger the task within webserver which are tagged as web and/or foo. There's currently no built-in way.

There might be a solution, by creating a custom action plugin which injects tags into the runner object of Ansible. Action plugins are not documented at all and you will need to look at the source of some implemented actions. I once created an action plugin which interacts with the runner object, injecting variables. Tags might work in a similar way. If you follow this path, be aware that Ansible 2 will include big refactoring and I'm nearly sure plugins like this will break.

这篇关于如何使用 Ansible 标签调用角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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