How do I call a role from within another role in A

2019-04-07 10:58发布

问题:

My playbook runs a list of roles:

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

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 }

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?

回答1:

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'" }


回答2:

I would suggest to not call from within a playbook and either use a condition in your entry books

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

or simply convert it into a group of tasks in a file and do a conditional import

Hope that helps.



标签: ansible