Reuse Ansible playbook logic between repositories

951 views Asked by At

I'm developing Ansible playbooks for reuse across our applications and wondering the best way to share core logic playbooks in one repository to be used in other repositories.

Example:

  • Repository A: contains playbooks to build/compile/deploy web applications.
  • Repository B: web application 1 source code.
  • Repository C: web application 2 source code.
  • etc for all other application repositories.

Repo A's playbooks could contain logic for cloning a remote repository before performing the logic, but I'm not sure if it's the best approach. Additionally the permissions might be different for the "build" repository versus "application" repositories.

Naturally, I don't want to copy the build playbooks in repo A to all other repos which need to to perform builds. But I'm not sure of the best way to use playbook logic from one repo when executing builds from the context of other repositories. In this case I'm not sure if what I'm looking for is to create a collection, module, role, or other (a completely different and better way)?

Is there a way to package all the logic from repo A's playbooks so they can be used from simple playbooks in the application repositories? I'm not sure how to accomplish this for utilization in both command-line usage via ansible-playbook and execution using AWX/Tower.

Example playbook which would reside in repo B which uses logic from repo A:

- name: "Deploy application A"
  perform_app_deploy: # <--- the common/shared part from repo A
    app_name: "Application 1"
    deploy_uri: "/application-a"

The logic in repo A's playbooks are using commonly available Ansible modules available in Ansible core (e.g. uri, azure.azcollection) and NOT python code.

1

There are 1 answers

0
ikora On

As far as I know, the best approach you can do is to use roles, if you set your roles properly they will be working fine on all the executions and the role1 will do his logic then role 2 will do his logic separately. If you need them to be dependant one from another you can use meta: Roles Dependencies (Meta)

There is an official guide and best practices on reusing roles and making them reusable

Re-using Ansible artifacts