Ansible: Roles and Modules from Content Collections

Ansible_Logo

The Content Collection is a set of related roles, supplementary modules, and other content. This can be obtained from an Ansible Content Collection and can be used in playbook.

The default path where collections are usually downloaded by defaults is: ~/.ansible/collections:/usr/share/ansible/collections

A collection available in Ansible Galaxy can be downloaded to a specific folder, in this case the collections folder in root folder by the below command:

[root@centos9vm ~]# mkdir pre-exam/collections
[root@centos9vm ~]# ansible-galaxy collection install community.crypto -p pre-exam/collections/

We can install collections from tar files too

[root@centos9vm ~]#  ansible-galaxy collection install /mnt/community-dns-1.2.0.tar.gz -p collections

As for installing roles, we can install collections too using requirements.yml file

[root@centos9vm ~]#  cat requirements.yml

===== ==
– – –
collections:
      – name: community.crypto
      – name: ansible.posix
            version: 1.2.0
      – name: /tmp/community-dns-1.1.0.tar.gz
      – name: http://www.example.com/redhat-insights-1.0.1.tar.gz
===== ==

[root@centos9vm ~]#  ansible-galaxy collection install -r requirements.yml -p collections

At times, it may be good to use your own private repository too to download collections before searching the specific collection in Ansible Galaxy. This can be achieved by editing the ansible.cfg configuration file and adding the private repositories too under galaxy. An example of the configuration file is:

===== ===
[galaxy]
server_list = my_automation_hub, galaxy

[galaxy_server.my_automation_hub]
url=https://console.redhat.com/api/automation-hub/
auth_url=https://sso.redhat.com/auth/realms/redhat-external/protocol/openidconnect/
token
token=eyJh…Jf0o

[galaxy_server.galaxy]
url=https://galaxy.ansible.com/
===== ===

We can list all collections deployed and browse through the roles using the following command.

[root@centos9vm ~]# ansible-navigator collections

 

Installing role from external source
=======******======

Let us install a role installable downloaded from external source and stored as ansible-role-java-2.5.0.tar.gz

[root@centos9vm pre-ex]# ansible-galaxy role install ansible-role-java-2.5.0.tar.gz -p roles
Starting galaxy role install process
– extracting ansible-role-java-2.5.0.tar.gz to /root/pre-exam/roles/ansible-role-java-2.5.0.tar.gz
– ansible-role-java-2.5.0.tar.gz was installed successfully

[root@centos9vm pre-exam]# ls roles/ansible-role-java-2.5.0.tar.gz/
defaults LICENSE meta molecule README.md tasks templates vars

==== ====

Let us remove the installed role:

[root@centos9vm pre-exam]# ansible-galaxy role remove ansible-role-java-2.5.0.tar.gz
– successfully removed ansible-role-java-2.5.0.tar.gz
=======

We can install the same role in tar format using requirement.yml file too

[root@centos9vm pre-exam]# cat requirements.yml

===== ===
– – –
roles:
– ansible-role-java-2.5.0.tar.gz
===== ===
[root@centos9vm pre-exam]# ansible-galaxy role install -r requirements.yml -p roles
Starting galaxy role install process
– extracting ansible-role-java-2.5.0.tar.gz to /root/pre-exam/roles/ansible-role-java-2.5.0.tar.gz
– ansible-role-java-2.5.0.tar.gz was installed successfully

[root@centos9vm pre-exam]# ls roles/ansible-role-java-2.5.0.tar.gz/
defaults LICENSE meta molecule README.md tasks templates vars

Let us remove the installed role

[root@centos9vm pre-exam]# ansible-galaxy role list
# /root/pre-exam/roles
– linux-system-roles.selinux, 1.7.6
– ansible-role-java-2.5.0.tar.gz, (unknown version)
[root@centos9vm pre-exam]# ansible-galaxy role remove ansible-role-java-2.5.0.tar.gz
– successfully removed ansible-role-java-2.5.0.tar.gz
[root@centos9vm pre-exam]# ls roles/
linux-system-roles.selinux .. requirements.yml
===== ====

Let us play around a bit with multiple role files and see how they work

[root@centos9vm pre-exam]# cp ansible-role-java-2.5.0.tar.gz ansible-SHIJU-java-2.5.0.tar.gz
[root@centos9vm pre-exam]# cat requirements.yml

====== ==
– – –
roles:
– ansible-role-java-2.5.0.tar.gz
– ansible-SHIJU-java-2.5.0.tar.gz
====== ==

[root@centos9vm pre-exam]# ansible-galaxy role install -r requirements.yml -p roles
Starting galaxy role install process
– extracting ansible-role-java-2.5.0.tar.gz to /root/pre-exam/roles/ansible-role-java-2.5.0.tar.gz
– ansible-role-java-2.5.0.tar.gz was installed successfully
– extracting ansible-SHIJU-java-2.5.0.tar.gz to /root/pre-exam/roles/ansible-SHIJU-java-2.5.0.tar.gz
– ansible-SHIJU-java-2.5.0.tar.gz was installed successfully

[root@centos9vm pre-exam]# ls -l roles/
total 16
drwxr-xr-x. 9 root root 4096 Jul 3 05:26 ansible-role-java-2.5.0.tar.gz
drwxr-xr-x. 9 root root 4096 Jul 3 05:26 ansible-SHIJU-java-2.5.0.tar.gz
drwxr-xr-x. 13 root root 4096 Jun 24 12:36 linux-system-roles.selinux
-rw-r–r–. 1 root root 38 Jun 23 17:23 requirements.yml
=======

[root@centos9vm pre-exam]# ansible-galaxy role remove ansible-role-java-2.5.0.tar.gz
– successfully removed ansible-role-java-2.5.0.tar.gz
[root@centos9vm pre-exam]# ansible-galaxy role remove ansible-SHIJU-java-2.5.0.tar.gz
– successfully removed ansible-SHIJU-java-2.5.0.tar.gz

===== ====

Let is see if we can change the name of one of the role

[root@centos9vm pre-exam]# cat requirements.yml

===== ==
– – –
roles:
– ansible-role-java-2.5.0.tar.gz
– src: ansible-SHIJU-java-2.5.0.tar.gz
name: SHIJU_ROLE
====== ==

[root@centos9vm pre-exam]# ansible-galaxy role install -r requirements.yml -p roles
Starting galaxy role install process
– extracting ansible-role-java-2.5.0.tar.gz to /root/pre-exam/roles/ansible-role-java-2.5.0.tar.gz
– ansible-role-java-2.5.0.tar.gz was installed successfully
– extracting SHIJU_ROLE to /root/pre-exam/roles/SHIJU_ROLE
– SHIJU_ROLE was installed successfully

[root@centos9vm pre-exam]# ls -l roles/
total 16
drwxr-xr-x. 9 root root 4096 Jul 3 05:39 ansible-role-java-2.5.0.tar.gz
drwxr-xr-x. 13 root root 4096 Jun 24 12:36 linux-system-roles.selinux
-rw-r–r–. 1 root root 38 Jun 23 17:23 requirements.yml
drwxr-xr-x. 9 root root 4096 Jul 3 05:39 SHIJU_ROLE
[root@centos9vm pre-exam]# ls -l roles/SHIJU_ROLE/tasks/
total 20
-rw-r–r–. 1 root root 1710 Apr 25 14:01 main.yml
-rw-r–r–. 1 root root 100 Apr 25 14:01 setup-Amazon.yml
-rw-r–r–. 1 root root 451 Apr 25 14:01 setup-Debian.yml
-rw-r–r–. 1 root root 347 Apr 25 14:01 setup-FreeBSD.yml
-rw-r–r–. 1 root root 100 Apr 25 14:01 setup-RedHat.yml
=========