如何在Ansible中从另一个角色中调用一个角色? [英] How do I call a role from within another role in Ansible?

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

问题描述

我的剧本有一系列角色:

My playbook runs a list of roles:

roles:
  - common
  - postgres
  - nginx
  - supervisord
  - { role: deploy_app,  app_name: myapp }
  - { role: deploy_app,  app_name: otherapp }

我还有另一个角色,celery,我只想在使用deploy_app创建名为myapp的应用程序时运行.我当时想我应该将参数传递给这样的角色:

I have another role, celery, that I only want to run when the app named myapp is created using deploy_app. I was thinking I should pass a parameter into the role like this:

- { role: deploy_app,  app_name: myapp, celery: yes }

然后在我的deploy_app角色中,我将使用when条件:

Then within my deploy_app role, I would use a when conditional:

- name: create celery worker for application
  <RUN ROLE HERE>
  when: '{{ celery }}' == 'yes'

如何从任务列表中有条件地运行角色?

How can I conditionally run a role from within a task list?

推荐答案

我认为可行的依赖关系在这里会有所帮助.只需创建一个 /meta/main.yml在您的角色内,具有以下内容:

I think ansible depenencies would help here. Just create a /meta/main.yml inside your role with the following:

---
dependencies:
  - { role: celery, tags: ["sometag"], when: "celery == 'yes'" }

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

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