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 a local collection that has 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 local.my_collection

 

In this example, the /tmp/ansible_collection/local/my_collection 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/local/my_collection/roles/my_role/tasks

 

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

touch /tmp/ansible_collection/local/my_collection/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/local/my_collection since the galaxy.yml file is located at /tmp/ansible_collection/local/my_collection/galaxy.yml.

cd /tmp/ansible_collection/local/my_collection

 

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
Created collection for local.my_collection at /tmp/local-my_collection-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/local-my_collection-1.0.0.tar.gz
Starting galaxy collection install process
Process install dependency map
Starting collection install process
Installing 'local.my_collection:1.0.0' to '/home/john.doe/.ansible/collections/ansible_collections/local/my_collection'
local.my_collection:1.0.0 was installed successfully

 

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

~]$ ansible-galaxy collection list

# /home/john.doe/.ansible/collections/ansible_collections
Collection          Version
------------------- -------
local.my_collection 1.0.0

 

Let's create a testing playbook that uses my_role in my_collection.

---
- hosts: localhost
  tasks:
  - import_role:
      name: local.my_collection.my_role
...

 

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

~]$ ansible-playbook testing.yml
TASK [local.my_collection.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 6934a9 in the box below so that we can be sure you are a human.