Skip to content

Instantly share code, notes, and snippets.

@dzintars
Last active January 14, 2024 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dzintars/4583a06cdbf5c76291a96bad55b07fde to your computer and use it in GitHub Desktop.
Save dzintars/4583a06cdbf5c76291a96bad55b07fde to your computer and use it in GitHub Desktop.
How to setup Ansible Collection

How to setup Ansible Collection

I hope to collect bits and pieces on how to setup Ansible Collection project for local development and testing with Molecule.

Structure

  • ~/code/github.com/user/namespace/
    • .skeleton/
    • collection-a/
    • collection-b/
      • collections/
      • environments/
        • development/
        • production/
          • .ssh/
          • group_vars/
          • host_vars/
          • hosts
      • molecule/
        • role-a/
        • role-b/
      • playbooks/
      • plugins/
      • roles/
        • role-a/
        • role-b/
      • scripts/

Molecule

Molecule and role meta dependency discovery

There are 3 ways how to handle role meta dependencies:

  • Publish collection to Ansible Galaxy
  • Push collection to the remote Git repository
  • Use local collections

Local collections

This means that all dependencies are available locally and Molecule does not need to ping the Web. To make this working there are few requirements:

  • All dependencies should be cloned into ~/code/github.com/user/ansible/collection-b/collections/ansible_collections/collection-name This should be done even for the collection or roles which are part of the collection-a or collection-b if there are roles which deppends on each other. But inter-role dependencies is considered as a bad practice and the playbooks should be used to glue several roles together. You can use ln -s $PWD/roles $PWD/collections/ansible_collections/namespace/collection-b/roles to create the symlinks to the actual roles. Or you can just copy them, but that will lead to the confussion when you will forget to update those.

  • molecule.yaml#provisioner:env:ANSIBLE_COLLECTION_PATH: "./collections" should be set.

    I believe you could also create symlink from ~/code/github.com/user/namespace/ to ~/.ansible/collections/ansible_collections/namespace and in this case I think you can omit the ANSIBLE_COLLECTION_PATH variable. I should check that. https://docs.ansible.com/ansible/devel/dev_guide/developing_collections_contributing.html#hacking-collections

@dzintars
Copy link
Author

Since I wrote this, I do things bit differently. All my collections are just part of single "ansible" Git repository under ./collections/ansible_collections directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment