{"id":1486,"date":"2024-06-23T21:52:22","date_gmt":"2024-06-23T21:52:22","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=1486"},"modified":"2025-05-23T18:45:28","modified_gmt":"2025-05-23T18:45:28","slug":"ansible-roles-and-modules-from-content-collections","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=1486","title":{"rendered":"Ansible: Roles and Modules from Content Collections"},"content":{"rendered":"<p>The Content Collection is a <strong>set<\/strong> of related roles, supplementary modules, and other content. This can be obtained from an Ansible\u00a0Content Collection and can be used in playbook.<\/p>\n<p>The default path where collections are <em>usually\u00a0<\/em>downloaded by defaults is: ~\/.ansible\/collections:\/usr\/share\/ansible\/collections<\/p>\n<p>A collection available in Ansible Galaxy can be downloaded to a specific folder, in this case the\u00a0<em>collections<\/em> folder in root folder by the below command:<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong>\u00a0mkdir pre-exam\/collections<br \/>\n<strong>[root@centos9vm ~]#<\/strong>\u00a0ansible-galaxy collection install community.crypto -p pre-exam\/collections\/<\/p>\n<p>We can install collections from tar files too<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong>\u00a0 ansible-galaxy collection install \/mnt\/community-dns-1.2.0.tar.gz -p collections<\/p>\n<p>As for installing\u00a0<em>roles<\/em>, we can install collections too using requirements.yml file<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong>\u00a0 cat requirements.yml<\/p>\n<p><em>===== ==<\/em><br \/>\n<em>&#8211; &#8211; &#8211;<\/em><br \/>\n<em>collections:<\/em><br \/>\n<em>\u00a0\u00a0 \u00a0 \u00a0&#8211; name: community.crypto<\/em><br \/>\n<em>\u00a0 \u00a0<\/em><em>\u00a0 \u00a0<\/em><em>&#8211; name: ansible.posix<\/em><br \/>\n<em>\u00a0 \u00a0<\/em><em>\u00a0 \u00a0<\/em><em>\u00a0 \u00a0<\/em><em>\u00a0 \u00a0<\/em><em>version: 1.2.0<\/em><br \/>\n<em>\u00a0 \u00a0<\/em><em>\u00a0 \u00a0<\/em><em>&#8211; name: \/tmp\/community-dns-1.1.0.tar.gz<\/em><br \/>\n<em>\u00a0 \u00a0<\/em><em>\u00a0 \u00a0<\/em><em>&#8211; name: http:\/\/www.example.com\/redhat-insights-1.0.1.tar.gz<\/em><br \/>\n<em>===== ==<\/em><\/p>\n<p><strong>[root@centos9vm ~]#<\/strong>\u00a0\u00a0ansible-galaxy collection install -r requirements.yml -p collections<\/p>\n<p>At times, it may be good to use your own private repository too to download\u00a0<em>collections<\/em> before searching the specific collection in Ansible Galaxy. This can be achieved by editing the\u00a0<em><strong>ansible.cfg<\/strong><\/em> configuration file and adding the private repositories too under\u00a0<em><strong>galaxy<\/strong><\/em>. An example of the configuration file is:<\/p>\n<p><em>===== ===<\/em><br \/>\n<em>[galaxy]<\/em><br \/>\n<em>server_list = my_automation_hub, galaxy<\/em><\/p>\n<p><em>[galaxy_server.my_automation_hub]<\/em><br \/>\n<em>url=https:\/\/console.redhat.com\/api\/automation-hub\/<\/em><br \/>\n<em>auth_url=https:\/\/sso.redhat.com\/auth\/realms\/redhat-external\/protocol\/openidconnect\/<\/em><br \/>\n<em>token<\/em><br \/>\n<em>token=eyJh&#8230;Jf0o<\/em><\/p>\n<p><em>[galaxy_server.galaxy]<\/em><br \/>\n<em>url=https:\/\/galaxy.ansible.com\/<\/em><br \/>\n<em>===== ===<\/em><\/p>\n<p>We can list all <em>collections<\/em> deployed and browse through the roles using the following command.<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator collections<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><span style=\"color: #993300;\"><strong>Installing role from external source<\/strong><\/span><br \/>\n<span style=\"color: #993300;\"> <strong>=======******======<\/strong><\/span><\/p>\n<p style=\"text-align: left;\">Let us install a role installable downloaded from external source and stored as\u00a0<em>ansible-role-java-2.5.0.tar.gz<\/em><\/p>\n<p style=\"text-align: left;\"><strong>[root@centos9vm pre-ex]#<\/strong> ansible-galaxy role install ansible-role-java-2.5.0.tar.gz -p roles<br \/>\nStarting galaxy role install process<br \/>\n&#8211; extracting ansible-role-java-2.5.0.tar.gz to \/root\/pre-exam\/roles\/ansible-role-java-2.5.0.tar.gz<br \/>\n&#8211; ansible-role-java-2.5.0.tar.gz was installed successfully<\/p>\n<p><strong>[root@centos9vm pre-exam]#<\/strong> ls roles\/ansible-role-java-2.5.0.tar.gz\/<br \/>\ndefaults LICENSE meta molecule README.md tasks templates vars<\/p>\n<p style=\"text-align: left;\">==== ====<\/p>\n<p style=\"text-align: left;\">Let us remove the installed role:<\/p>\n<p style=\"text-align: left;\"><strong>[root@centos9vm pre-exam]#<\/strong> ansible-galaxy role <em>remove<\/em> ansible-role-java-2.5.0.tar.gz<br \/>\n<em>&#8211; successfully removed ansible-role-java-2.5.0.tar.gz<\/em><br \/>\n<em>=======<\/em><\/p>\n<p style=\"text-align: left;\">We can install the same role in tar format using\u00a0<em>requirement.yml<\/em> file too<\/p>\n<p style=\"text-align: left;\"><strong>[root@centos9vm pre-exam]#<\/strong> cat requirements.yml<\/p>\n<p style=\"text-align: left;\"><em>===== ===<\/em><br \/>\n<em>&#8211; &#8211; &#8211;<\/em><br \/>\n<em>roles:<\/em><br \/>\n<em> &#8211; ansible-role-java-2.5.0.tar.gz<\/em><br \/>\n<em>===== ===<\/em><br \/>\n<strong>[root@centos9vm pre-exam]#<\/strong> ansible-galaxy role install -r requirements.yml -p roles<br \/>\nStarting galaxy role install process<br \/>\n&#8211; extracting ansible-role-java-2.5.0.tar.gz to \/root\/pre-exam\/roles\/ansible-role-java-2.5.0.tar.gz<br \/>\n&#8211; ansible-role-java-2.5.0.tar.gz was installed successfully<\/p>\n<p style=\"text-align: left;\"><strong>[root@centos9vm pre-exam]#<\/strong> ls roles\/ansible-role-java-2.5.0.tar.gz\/<br \/>\ndefaults LICENSE meta molecule README.md tasks templates vars<\/p>\n<p style=\"text-align: left;\">Let us remove the installed role<\/p>\n<p style=\"text-align: left;\"><strong>[root@centos9vm pre-exam]#<\/strong> ansible-galaxy role list<br \/>\n# \/root\/pre-exam\/roles<br \/>\n&#8211; linux-system-roles.selinux, 1.7.6<br \/>\n&#8211; ansible-role-java-2.5.0.tar.gz, (unknown version)<br \/>\n<strong>[root@centos9vm pre-exam]#<\/strong> ansible-galaxy role remove ansible-role-java-2.5.0.tar.gz<br \/>\n&#8211; successfully removed ansible-role-java-2.5.0.tar.gz<br \/>\n<strong>[root@centos9vm pre-exam]#<\/strong> ls roles\/<br \/>\n<em>linux-system-roles.selinux<span style=\"color: #ffffff;\"> ..<\/span> requirements.yml<\/em><br \/>\n<em>===== ====<\/em><\/p>\n<p style=\"text-align: left;\">Let us play around a bit with multiple role files and see how they work<\/p>\n<p style=\"text-align: left;\"><strong>[root@centos9vm pre-exam]#<\/strong> cp <em>ansible-role-java-2.5.0.tar.gz<\/em> ansible-SHIJU-java-2.5.0.tar.gz<br \/>\n<strong>[root@centos9vm pre-exam]#<\/strong>\u00a0cat requirements.yml<\/p>\n<p style=\"text-align: left;\"><em>====== ==<\/em><br \/>\n<em>&#8211; &#8211; &#8211;<\/em><br \/>\n<em>roles:<\/em><br \/>\n<em> &#8211; ansible-role-java-2.5.0.tar.gz<\/em><br \/>\n<em> &#8211; ansible-SHIJU-java-2.5.0.tar.gz<\/em><br \/>\n<em>====== ==<\/em><\/p>\n<p><strong>[root@centos9vm pre-exam]#<\/strong> ansible-galaxy role install -r requirements.yml -p roles<br \/>\n<em>Starting galaxy role install process<\/em><br \/>\n<em>&#8211; extracting ansible-role-java-2.5.0.tar.gz to \/root\/pre-exam\/roles\/ansible-role-java-2.5.0.tar.gz<\/em><br \/>\n<em>&#8211; ansible-role-java-2.5.0.tar.gz was installed successfully<\/em><br \/>\n<em>&#8211; extracting ansible-SHIJU-java-2.5.0.tar.gz to \/root\/pre-exam\/roles\/ansible-SHIJU-java-2.5.0.tar.gz<\/em><br \/>\n<em>&#8211; ansible-SHIJU-java-2.5.0.tar.gz was installed successfully<\/em><\/p>\n<p style=\"text-align: left;\"><strong>[root@centos9vm pre-exam]#<\/strong> ls -l roles\/<br \/>\n<em>total 16<\/em><br \/>\n<em>drwxr-xr-x. 9 root root 4096 Jul 3 05:26 ansible-role-java-2.5.0.tar.gz<\/em><br \/>\n<em>drwxr-xr-x. 9 root root 4096 Jul 3 05:26 ansible-SHIJU-java-2.5.0.tar.gz<\/em><br \/>\n<em>drwxr-xr-x. 13 root root 4096 Jun 24 12:36 linux-system-roles.selinux<\/em><br \/>\n<em>-rw-r&#8211;r&#8211;. 1 root root 38 Jun 23 17:23 requirements.yml<\/em><br \/>\n<em>=======<\/em><\/p>\n<p style=\"text-align: left;\"><strong>[root@centos9vm pre-exam]#<\/strong> ansible-galaxy role remove ansible-role-java-2.5.0.tar.gz<br \/>\n&#8211; successfully removed ansible-role-java-2.5.0.tar.gz<br \/>\n<strong>[root@centos9vm pre-exam]#<\/strong> ansible-galaxy role remove ansible-SHIJU-java-2.5.0.tar.gz<br \/>\n&#8211; successfully removed ansible-SHIJU-java-2.5.0.tar.gz<\/p>\n<p style=\"text-align: left;\">===== ====<\/p>\n<p style=\"text-align: left;\">Let is see if we can change the name of one of the role<\/p>\n<p><strong>[root@centos9vm pre-exam]#<\/strong> cat requirements.yml<\/p>\n<p><em>===== ==<\/em><br \/>\n<em>&#8211; &#8211; &#8211;<\/em><br \/>\n<em>roles:<\/em><br \/>\n<em> &#8211; ansible-role-java-2.5.0.tar.gz<\/em><br \/>\n<em> &#8211; src: ansible-SHIJU-java-2.5.0.tar.gz<\/em><br \/>\n<em> name: SHIJU_ROLE<\/em><br \/>\n<em>====== ==<\/em><\/p>\n<p><strong>[root@centos9vm pre-exam]#<\/strong> ansible-galaxy role install -r requirements.yml -p roles<br \/>\nStarting galaxy role install process<br \/>\n&#8211; extracting ansible-role-java-2.5.0.tar.gz to \/root\/pre-exam\/roles\/ansible-role-java-2.5.0.tar.gz<br \/>\n&#8211; ansible-role-java-2.5.0.tar.gz was installed successfully<br \/>\n&#8211; extracting SHIJU_ROLE to \/root\/pre-exam\/roles\/SHIJU_ROLE<br \/>\n&#8211; SHIJU_ROLE was installed successfully<\/p>\n<p><strong>[root@centos9vm pre-exam]#<\/strong> ls -l roles\/<br \/>\nt<em>otal 16<\/em><br \/>\n<em> drwxr-xr-x. 9 root root 4096 Jul 3 05:39 <strong>ansible-role-java-2.5.0.tar.gz<\/strong><\/em><br \/>\n<em> drwxr-xr-x. 13 root root 4096 Jun 24 12:36 linux-system-roles.selinux<\/em><br \/>\n<em> -rw-r&#8211;r&#8211;. 1 root root 38 Jun 23 17:23 requirements.yml<\/em><br \/>\n<em> drwxr-xr-x. 9 root root 4096 Jul 3 05:39 <strong>SHIJU_ROLE<\/strong><\/em><br \/>\n<strong>[root@centos9vm pre-exam]#<\/strong> ls -l roles\/SHIJU_ROLE\/tasks\/<br \/>\ntotal 20<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 1710 Apr 25 14:01 main.yml<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 100 Apr 25 14:01 setup-Amazon.yml<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 451 Apr 25 14:01 setup-Debian.yml<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 347 Apr 25 14:01 setup-FreeBSD.yml<br \/>\n-rw-r&#8211;r&#8211;. 1 root root 100 Apr 25 14:01 setup-RedHat.yml<br \/>\n=========<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>The Content Collection is a set of related roles, supplementary modules, and other content. This can be obtained from an Ansible\u00a0Content Collection and can be <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=1486\" title=\"Ansible: Roles and Modules from Content Collections\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":1792,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,21,3],"tags":[],"class_list":["post-1486","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ansible","category-devops","category-linux"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1486"}],"version-history":[{"count":17,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1486\/revisions"}],"predecessor-version":[{"id":1559,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1486\/revisions\/1559"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/media\/1792"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1486"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}