Explaining of Ansible Collections

Explaining of  Ansible Collections
Techiio-author
Written by Nilima PaulFebruary 2, 2022
11 min read
Ansible
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Nilima Paul

Technology Security Analyst

We will know in this article, what's the Ansible Collections.

Introduction to Ansible Collections

Ansible Collections are the organization wherein Ansible substances like Modules, Roles, Playbooks and Plugin, and so forth can be appropriated to the Ansible clients across Ansible people group and Ansible clients. An Ansible Collection can incorporate a job, modules, playbook, modules. This circulation is performed by Ansible Galaxy, which is a spot from where you can introduce or get other designers' common substance. Likewise, you can transfer your code/content in Ansible Galaxy to additional offer it. There is a worldwide area for the Ansible Galaxy server yet on the off chance that you have other Galaxy Server like RedHat Automation Hub, you can arrange something very similar in your Ansible arrangement.

Explaining Ansible Collections

Ansible Collections are gathered from Ansible Galaxy the same way we import any jobs. Just some punctuation changes should be noted while running the order for itself and involving any assortment in playbooks.

Thus, beneath are a few focuses that you should keep in mind while chipping away at Collections.

To introduce an assortment, you can utilize the underneath order. However, note that we utilized - p here to determine the area of the Ansible assortment, this is set by COLLECTIONS_PATHSin Ansible design document and naturally ~/.ansible/assortments:/usr/share/ansible/assortments.

ansible-galaxy collection install<collection_name> -p <collection_local_location>

Along with default locations for collection, you can place your collection in the directory named as collections/ansible_collections/ adjacent to your playbook. This will follow the below directory structure.

my_playbook.YAML
├── collections/
│   └── ansible_collections/
│               └── our_Ansible_namespace/
│                   └── our_Ansible_collection/< a playbook relevant collection's structure>

System server of course will be utilized as https://galaxy.ansible.com, to transform it then you really want to change your working cfg document and rundown your Galaxy Servers under GALAXY_SERVER_LIST. Likewise, you can utilize - server in the order line to restrict your assortment's inquiry. In situations where you need to utilize your Ansible servers of course rather than worldwide Ansible Galaxy area. You can adjust ansible.cfg record and utilize a part named [galaxy]. Then, at that point, place a rundown of assortments after server_list.

Below is an example of such a section, where we have listed two Collections, one is RedHat Automation Hub and other is a sample, which can be prepared by us manually: –

[galaxy] server_list = automation_hub, sample_galaxy_collection
[galaxy_server.automation_hub] url=https://cloud.redhat.com/api/automation-hub/
auth_url=https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token
token=automation_hub_token
[galaxy_server.sample_galaxy_collection] url=https://galaxy-dev.ansible.com/
token=sample_token

As you saw above there are certain parameters accepted by Ansible for an Ansible Collection.

Below is the list for same: –

  • url: For the location of an Ansible Collection.
  • token: This is mutually exclusive with the username. This is used to authenticate against a Galaxy Server.
  • username: This is mutually exclusive with token and used for basic authentication with Galaxy Server.
  • password: This is the password for the username above.
  • auth_url: This is mutually exclusive with the username. auth_url requires a token. So this provides the location of the token endpoint

When installing any collection from Ansible Galaxy, by default the latest version will be installed. But if you need a specific version then you can use below operators to specify a version.

  • :* For any version, this is default and will look for the latest.
  • :!= For any version by not equal to specified ones.
  • :== For a specified version.
  • :>= For any version greater than equal to the one specified here.
  • :> For any version greater than specified.
  • :<= For any version lower than equal to the one specified here.
  • :< For any version lower than specified.

Instead of installing on the command line, you can also set up a file like yaml, where you can give the list of collections that need to be installed under keyword collections. This can also work when you have defined your roles in the same file. A sample file will be like below: –

---
collections:
# Installing collection from Ansible_Galaxy.
- name: <namespace_name.collection_name>
source: https://galaxy.ansible.com
roles:
# Installing role from Ansible_Galaxy.
- name: <role_name>

If these cases where you have Ansible Roles and Collections defined in the same file for sake

of simplicity and consolidation. You can simply use the below command to install Collections only from this file.

ansible-galaxy collection install -r requirements.yml -p ./<file_location>

After installation, to use an Ansible collection inside a playbook, you shall refer it by using it FQCN (fully qualified collection name). This kind of reference works for modules, plugins as well as roles. Below is a code sample.

---
- hosts: all
collections:
- customized_namespace.customized_collection
tasks:
- import_role:
name: <role_name>
- my_module:
key: value

Similarly, there are few other alternatives.

How Ansible Collections Works?

In this part, we will perceive the way we can utilize an Ansible Collection. We will download and introduce an assortment from Ansible Galaxy and afterward utilize a module inside it to accomplish something on the localhost. We will introduce an example Ansible Collection named newswangerd.collection_demo, which I just found on the Ansible system site ( https://galaxy.ansible.com/), via looking.

We will introduce this Ansible assortment utilizing the underneath order, here we are not giving any way to the world server, so as a matter of course, it will divert to the worldwide Ansible Galaxy server for source.

ansible-galaxy collection installs newswangerd.collection_demo

In the result, we can see beneath, that the establishment interaction began and made a catalog structure under my work area.

blogpost

If we explore the directory structure created by this then we have below: –

tree .ansible/collections/ansible_collections/newswangerd/
blogpost

Now using a module from this Ansible Collection can be done as below, we can see in the above picture that we have a module named real_facts, which basically will print some lines on display: –

ansible localhost -m newswangerd.collection_demo.real_facts

Now, by running it, we will get an output like below: –

blogpost

Please note that the above is just an example to show you how it works. You can browse Ansible Galaxy for a required Collection or you can develop one by yourself using Python Scripting.

Conclusion

As we found in this article, the assortment is a great method for circulating substance to your clients or from Developers to Administrators. This assists with merging and smoothing out a comparative or same sort of playbook, jobs, modules, and modules. Adding this capacity to your range of abilities makes you powerful in Ansible. Along these lines, learn it first and afterward use it.

Ansible
Ansible Collections
Linux
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Nilima Paul
Technology Security Analyst
Techiio-followerTechiio-followerTechiio-follower
201 Blog Posts
0 Discussion Threads
Trending Technologies
15
Software40
DevOps48
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance