将其他角色的任务包括在可玩剧本中 [英] include tasks from another role in ansible playbook

查看:128
本文介绍了将其他角色的任务包括在可玩剧本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一种具有单独任务的剧本库

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

custom_stuff_workflow中的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

我找不到一种自然的方法: 一种有效的方法是将所有任务都放在一个角色中,并在包含custom_stuff_workflow的同时标记相关任务

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

我的问题是无法在调用剧本中设置标签:只能在命令行中设置 由于我正在与公司中的许多人一起分发此仓库,因此我不能依赖命令行调用(在yml中有一个#!标头由ansible-playbook命令处理会很好)

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...

非常感谢

PS:请注意,工作流中的任务必须按特定顺序完成,抽象的唯一自然步骤就是登录和保存/注销

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:我已经看到了这个问题

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

推荐答案

只是为了防止其他人碰到这个问题,Ansible的 2.2 版本现在有了include_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

此处中查看文档.

这篇关于将其他角色的任务包括在可玩剧本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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