Ansible: Collection

Manage collections

# Install collection
ansible-galaxy collection install ansible.posix
ansible-galaxy collection install git@git.example.com:foo/ansible-collections/bar
ansible-galaxy collection install git+file:///home/user/path/to/repo_name
 
# List collections
ansible-galaxy collection list
# default user Ansible collection directory
~/.ansible/collections/ansible_collections/
 
# env vars
ANSIBLE_COLLECTIONS_PATHS
 
# ~/.ansible.cfg 
[defaults]
collections_paths = /path/to/collection
 
# get current path
ansible-config dump | grep -i collection

Include collection in playbook

- hosts: all
  collections:
    - my_namespace.my_collection
 
 
- hosts: all
  tasks:
    - import_role:
        name: my_namespace.my_collection.my_role

Defile collection dependency in role

# ./meta/main.yml
---
dependencies:
  - role: my_namespace.my_collection.my_role
    vars:
      foo: "bar"
- include_role:
    name: "{{ item }}"
  collections:
    - my_namespace.my_collection
  vars:
    foo: bar
   loop:
    - my_role

Links
https://docs.ansible.com/ansible/latest/galaxy/user_guide.html
https://docs.ansible.com/ansible/latest/user_guide/collections_using.html
https://goetzrieger.github.io/ansible-collections/2-using-collections-from-playbooks/