- In this video, we are going to take a deep dive
into collections and further understand.
So, we have talked about collections before
and you know about the basic usage of the collections.
Now let's look a little bit further.
So in the play header, you can use the collections keyword
and the collection keyword
takes a list of collections as its argument.
And we have already discussed that
that makes working with collections a bit easier.
And after using the collections keyword,
the collection itself can be addressed the old way.
So you use selinux instead of ansible.posix.selinux.
We have already seen how this is happening
in enforce-selinux-simplified,
and we have also seen how the old notation was used
in the other example.
Now let's try to understand
the differences in the usage between collections
and the things that you can do with them,
for Ansible 2.9 as well as 2.10.
So in Ansible 2.9,
collections are not a default part of Ansible.
You can use them,
but if you want to use them,
they must be installed.
If you don't install them, they're not there.
So, they're not working.
And if you're installing multiple collections,
you should consider using a requirements file.
A requirements file
allows you to list all of the collections
so that you can install them
before actually starting to use them.
In order to do so,
use ansible-galaxy collection install -r requirements.yml.
I have an example.
Let's check out the example on an Ansible 2.9 system.
In this video,
it's really important that with everything we do,
you are fully conscient
on the version of Ansible on which we are doing it.
So Ansible 2.9, the requirements file.
In Ansible 2.10 and later,
collections are installed by default.
So that makes it a lot easier.
Now, the next thing you wanna do
if you're working with collections,
you want to be able to get collection information.
Ansible 2.10 and later
contain better collection integration
and you can easily find collection names using ansible-doc;
ansible-doc-l | grep aws.
And Ansible 2.9 only shows collections
that have been installed.
So collections themselves are not listed,
just do your ansible-doc-l.
And collection documentation is available
while addressing collection components directly.
And finding collection names
only goes through the installed files.
That's not very convenient.
Now, how can we list collection files?
Well, you use ansible collection list
on Ansible 2.10 and later,
which will show a complete list
of all the collections that are available.
And to verify the contents of the collection,
you can access its physical location
or investigate the directory structure.
Now let's go check out how this works in real life.
Alright, let me demonstrate how to work with collections.
I'm on 2.9 for your information,
and I'm using ansible-galaxy collection
install
ansible.posix
Oh, that's not nice.
This is probably a bug.
Well, if it's a bug, then we are not going to fix it.
But fortunately, there's the Ansible Galaxy website.
Look, here is the posix collection.
On the Ansible Galaxy website,
there is an option to download the tarball.
So let's go behind the scenes
and let me just download the tarball.
Saving the file.
And next, I can get back
to my downloads directory
There we can see the ansible-posix.
So, I am going to do a tar -xvf.
You know what, let me avoid doing the Linux visitry
and let me do an ls
on
.ansible.
So we have collections.
Just verifying the contents in collections.
We have ansible_collections
and in there, there is nothing.
So I'm going to use this path that we just discovered.
But this time I'm copying the ansible-posix
till the .ansible/collections/ansible_collections.
And I am going to make that my current directory as well.
So, .ansible/collections/ansible_collections.
There we go,
and there I'm running tar xvf.
So, what do we see?
We see that it's creating
the entire sub directory structure in here.
That's not exactly what I wanted.
I wanted this sub directory structure to be somewhere else.
So, what does that mean?
That means that I should have created, mkdir ansible.
A directory with the name, ansible
and in this ansible, mkdir ansible/posix.
And then I can move everything to ansible/posix,
and then I have my directory structure.
So, in ansible/posix,
we see the structure of the posix collection,
which is everything that you can see right here.
Wow, is that an easy way to see what is in the collection?
No, of course not.
But it's probably the only way in 2.9.
2.9 was never organized to make it really accessible.
Look, this is the best way to get in there.
In plugins,
you will find all the different plugins,
and modules happen to be plugins as well.
Amongst these plugins,
we have acl and at and firewalld and whatever
This is how you can figure out what it is.
It brings it directly
to the python scripts.
Yes, I know.
But it's probably the best way that you can get around.
Based on this information,
based on the name of the collection,
you can next use your ansible-doc command.
So, if I'm using ansible-doc
on ansible
.posix
.selinux,
then, I am getting access
to the actual collection information.
And that is how it works on 2.9.
Is that the best way around?
No,
but I really think that in Ansible 2.9
you should consider collections to be in preview,
not in final.
If you wanna use collections,
make sure that you are on Ansible 2.10 or later.