在 ansible playbook 中包含另一个角色的任务 [英] include tasks from another role in ansible playbook
问题描述
我正在设计一种具有单独任务的剧本库
所以在通常的角色仓库中,我有类似的东西:
角色├── 普通│ └── 任务│ ├── A.yml│ ├── B.yml│ ├── C.yml│ ├── D.yml│ ├── login.yml│ ├── logout.yml│ └── save.yml├── custom_stuff_workflow│ └── 任务│ └── main.yml└── other_stuff_workflow└── 任务└── main.yml
我在 custom_stuff_workflow 中的 main.yml 然后包含如下内容:
---- 包括:login.yml- 包括:A.yml- 包括:C.yml- 包括:save.yml- 包括:logout.yml
另一个工作流中的这个:
---- 包括:login.yml- 包括:B.yml- 包括:A.yml- 包括:D.yml- 包括:save.yml- 包括:logout.yml
我找不到以自然的方式做到这一点的方法:一种有效的方法是将所有任务置于一个角色中,并在包含 custom_stuff_workflow 的同时标记相关任务
我遇到的问题是无法在调用剧本中设置标签:只能在命令行中设置由于我正在与公司中的许多人分发这个 ansible 存储库,因此我不能依赖命令行调用(在 yml 中有一个 #!
标头由 ansible-playbook
命令)
我也可以复制每个工作流中的相关任务(上面树中的常见任务),但我不想重复它们
有人可以看到一个解决方案来实现我想要的,而无需重复不同角色的任务吗?
我想我的问题的基石是我将任务定义为个人,并且在 ansible 中看起来不自然......
非常感谢
PS:请注意,工作流中的任务必须按特定顺序完成,抽象的唯一自然步骤是登录和保存/注销
PPS:我看过这个问题 如何从 Ansible 中的另一个角色中调用角色? 但它并没有解决我的问题,因为它调用的是完整角色而不是角色中任务的子集
以防万一其他人碰到这个问题,Ansible 2.2 版现在有 include_role
.你现在可以做这样的事情.
---- 名称:做某事include_role:名称:普通tasks_from:登录
在此处查看文档.>
I'm designing a kind of playbook lib with individual tasks
so in the usual roles repo, I have something like:
roles
├── common
│ └── tasks
│ ├── A.yml
│ ├── B.yml
│ ├── C.yml
│ ├── D.yml
│ ├── login.yml
│ ├── logout.yml
│ └── save.yml
├── custom_stuff_workflow
│ └── tasks
│ └── main.yml
└── other_stuff_workflow
└── tasks
└── main.yml
my main.yml in custom_stuff_workflow then contain something like:
---
- include: login.yml
- include: A.yml
- include: C.yml
- include: save.yml
- include: logout.yml
and this one in the other workflow:
---
- include: login.yml
- include: B.yml
- include: A.yml
- include: D.yml
- include: save.yml
- include: logout.yml
I can't find a way to do it in a natural way: one way that worked was having all tasks in a single role and tagging the relevant tasks while including a custom_stuff_workflow
The problem I have with that is that tags cannot be set in the calling playbook: it's only to be set at command line
as I'm distributing this ansible repo with many people in the company, I can't rely on command line invocations (it would be nice to have a #!
header in yml to be processed by ansible-playbook
command)
I could also copy the relevant tasks (inside common in the above tree) in each workflow, but I don't want to repeat them around
Can someone see a solution to achieve what I'd like without repeating the tasks over different roles?
I guess the corner stone of my problem is that I define tasks as individual and it looks not natural in ansible...
Thanks a lot
PS: note that the tasks in the workflow have to be done in specific order and the only natural steps to abstract would be the login and save/logout
PPS: I've seen this question How do I call a role from within another role in Ansible? but it does not solve my problem as it's invoking a full role and not a subset of the tasks in a role
Just incase someone else bumps into this, version 2.2 of Ansible now has include_role
.You can now do something like this.
---
- name: do something
include_role:
name: common
tasks_from: login
Check out the documentation here.
这篇关于在 ansible playbook 中包含另一个角色的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!