【问题标题】:How do I call a role from within another role in Ansible?如何从 Ansible 的另一个角色中调用角色?
【发布时间】:2014-02-27 19:22:04
【问题描述】:

我的剧本运行一个角色列表:

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

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

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

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

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

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

【问题讨论】:

标签: ansible


【解决方案1】:

我认为ansible depenencies 在这里会有所帮助。只需创建一个 /meta/main.yml 在您的角色中使用以下内容:

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

【讨论】:

  • 听起来 OP 希望 deploy_app 运行 before celery
【解决方案2】:

我建议不要从剧本中调用,而是在你的条目中使用条件

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

或者干脆将其转换为文件中的一组任务并进行条件导入

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2014-12-20
    • 2017-08-14
    • 2021-03-27
    相关资源
    最近更新 更多