Bootstrap FreeKB - Ansible - Create local collection
Ansible - Create local collection

Updated:   |  Ansible articles

Let's say you have one or more identical tasks that need to be used by various different isolated Ansible playbooks. You can create homegrown collections that have the tasks that need to be used by your isolated Ansible playbooks. In other words, a collection is basically like a shared library.

As an arbitrary example, let's say your various different isolated Ansible playbooks must always start with some sort of disclaimer. 

- debug:
    msg: Unauthorized access is prohibited and may result in civil or criminal prosecution or disciplinary action

 

Let's create a temporary directory that will be used to create the collection.

mkdir /tmp/ansible_collection

 

Move into the directory.

cd /tmp/ansible_collection

 

Use the ansible-galaxy collection init <namespace>.<collection name> command to create the files and directories for the collection. 

~]$ ansible-galaxy collection init homegrown.collections
- Collection homegrown.collections was created successfully

 

In this example, the /tmp/ansible_collection/homegrown/collections directory will contain the files and directories for the collection. Let's create a directory for a role named my_role.

mkdir --parents /tmp/ansible_collection/homegrown/collections/roles/my_role/tasks

 

Let's create the main.yml file for my_role.

touch /tmp/ansible_collection/homegrown/collections/roles/my_role/tasks/main.yml

 

Let's add the following to my_role main.yml file.

- debug:
    msg: Unauthorized access is prohibited and may result in civil or criminal prosecution or disciplinary action

 

Let's move into the /tmp/ansible_collection/homegrown/collections directory since the galaxy.yml file is located at /tmp/ansible_collection/homegrown/collections/galaxy.yml.

cd /tmp/ansible_collection/homegrown/collections

 

The ansible-galaxy collection build command can be used to create a gzip compressed tar archive of the collection.

~]$ ansible-galaxy collection build --output-path /tmp/ansible_collection/homegrown/collections
Created collection for homegrown.collections at /tmp/ansible_collection/homegrown/collections/homegrown-collections-1.0.0.tar.gz

 

For local testing, let's install the collection using ansible-galaxy collection install. By default, this will install the collection to /home/username/.ansible/collections/ansible_collections.

~]# ansible-galaxy collection install /tmp/ansible_collection/homegrown/collections/homegrown-collections-1.0.0.tar.gz --collections-path /opt/ansible/collections
Starting galaxy collection install process
Process install dependency map
Starting collection install process
Installing 'homegrown.collections:1.0.0' to '/opt/ansible/collections/ansible_collections/homegrown/collections'
homegrown.collections:1.0.0 was installed successfully

 

And the ansible-galaxy collection list should list the local collection.

~]$ ansible-galaxy collection list

# /opt/ansible/collections
Collection          Version
------------------- -------
homegrown.collections 1.0.0

 

Let's create a testing playbook that uses homegrown.collections.my_role.

---
- hosts: localhost
  tasks:
  - ansible.builtin.import_role:
      name: homegrown.collections.my_role
...

 

Running the testing playbook should run the tasks in homegrown.collections.my_role, which in this arbitrary example, just prints the disclaimer.

~]$ ansible-playbook testing.yml
TASK [homegrown.collections.my_role : debug] 
ok: [localhost] => {
    "msg": "Unauthorized access is prohibited and may result in civil or criminal prosecution or disciplinary action"
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter a63f2a in the box below so that we can be sure you are a human.