include tasks from another role in ansible playbook

試著忘記壹切 提交于 2019-12-02 17:24:10

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.

tehK

Yes, Ansible doesn't really like tasks as individual components. I think it wants you to use roles, but I can see why you wouldn't want to use roles for simple, reusable tasks.

I currently see two possible solutions:

1. Make those task-files into roles and use dependencies

Then you could do something like this in e.g. custom_stuff_workflow

dependencies:
  - { role: login }

See: https://docs.ansible.com/playbooks_roles.html#role-dependencies

2. Use include with "hardcoded" paths to the task files

- include: ../../common/tasks/login.yml

That worked pretty well in a short test playbook I just did. Keep in mind, you can also use parameters etc. in those includes.

See: http://docs.ansible.com/ansible/latest/playbooks_reuse.html

I hope I understood that question correctly and this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!