So now that we have recapped the basics of compression and archives,
let's now take a look at an Ansible module that can help us perform these tasks
.
So if you happen to search for Ansible Archive module and you click on this top
link right here,
so the name of the module that we're going to be using is community.general.
archive and if we scroll
on down we can see here this is part of the community.general collection. So if
you have not
already installed this collection, which by this point you probably already
should have,
but nevertheless if you're not, then you can get this collection by just simply WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
but nevertheless if you're not, then you can get this collection by just simply
typing in the
command Ansible Galaxy Collection Install community.general. Now let's go down
here to the synopsis here.
Now it says here this is going to allow us to create or extend an archive and
now crucially
here it says the source and the archive are going to be on the remote host and
the archive is not
copied to the local host. So pretty much here as we know we have our Ansible
control nodes and
let's say we have our managed device such as the blue box that we're going to
be managing with
Ansible. If we want to perhaps create an archive on this particular device here WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Ansible. If we want to perhaps create an archive on this particular device here
,
the actual files involved such as let's maybe say file 1.txt and file 2.txt,
the actual are going
to live on this particular device when the archiving happens. The archive is
not generated from files
on the actual control node and then copied over it's pretty much on the
existing box. Now just to
be clear here if you do happen to have particular files on this device here
that you want to be
compressed and present on the target device then you can still do this. All you WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
compressed and present on the target device then you can still do this. All you
would have to do
would be to copy the files over to the target first using the Ansible copy
module once they now
exist on this device here you can now use this particular module to archive and
compress them.
So just keep that distinction in your mind. So let's look at some parameters
here if we look at the
desk parameter as we can see here what we have to provide is a path pretty much
where on the file
system to be want the archive to reside. Now notice here this is something very
very important.
It says here that the parent directory must exist on the remote host pretty WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
It says here that the parent directory must exist on the remote host pretty
much on the target.
If that does not exist then you will have to create this directory structure
before you happen to
launch this particular module and by the way notice is that if the destination
archive already
exists it's going to be truncated and effectively overwritten we'll just write
over that completely.
Now this module also gives us the option to specify compression as well and as
we can see here this
is done via the format parameter and all we do here is specify the type of
compression we want to
use so we can use busy too. We can just create a regular tarball which is not WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
use so we can use busy too. We can just create a regular tarball which is not
actually going to
be compressed. We can specify xz compression and we can also specify zip
compression which is also
very useful for Windows based devices that you happen to be managing. Now the
resultant
fail system object that we're going to create the actual archive we can also
attach permissions
to this okay and we do this via the mode parameter. Originally this would be
specified via
octonotations that would be on numeric values such as 755 or if you want to
also include special WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
also include special
permissions that could be 1 6 4 4 whatever it may be using the four digits but
in later versions
of Ansible at either version you should have we can also have support for
symbolic notation so we
can just specify read write and execute as we so wish. We also have the option
here to specify the
owner pretty much when we create that object who should actually own it. This
parameter acts as
an abstraction layer for the tone command basically and now we move down to our
require parameter so
we're going to absolutely have to specify this if we want the module to operate WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
we're going to absolutely have to specify this if we want the module to operate
. Now don't get this
confused with the desk parameter okay. In the case here the path parameter is
going to allow to
supply a list object and the information that we're going to supply here is
going to be the remote
absolute path for the fail that you want to compress or archive or the globe
for the multiple
files you want to compress or archive or if we so wish a list of multiple
different paths whereby
we can specify fails to compress or archive. So say for example you've got file WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
we can specify fails to compress or archive. So say for example you've got file
1 dot txt and
file 2 dot txt if you want to compress and archive these particular files you
specify that via the
path parameter right here. If you want to use these files and create an archive
called well I don't
know that just maybe say my data dot tar dot busy this particular option would
be specified via the
desk parameter you see big big distinction. Now again scrolling on down you can
go through
many different options we have options for security enhanced Linux so if you WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
many different options we have options for security enhanced Linux so if you
want to also configure
particular se Linux security context then you can do so via a variety of
different ways via the
se level parameter se role se type that type of thing okay. So let's go back to
our terminal
right now and we'll begin using this particular module within a playbook so let
me go to my
blue box device right here I'll open up a terminal and inside of this directory
right here all that
we'll do is I'll create a little file called my file 1 dot txt I'll just say
this is my file 1
and I'll just write and quit and let's also create one called my file 2 dot txt WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and I'll just write and quit and let's also create one called my file 2 dot txt
once again super
debircrative I'll just say this is my file 2 and save you of course we have
these two devices or
rather these two files on the remote device that we want to manage with Ansible
file 1 and file 2
okay let's now go back to our control nodes and now we can begin writing our
playbooks let's just
call this play 1 dot yml so let's go into insert mode right here have our free
hyphens at the top
give this playbook a name and we'll just call this play 1 testing the archive WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
give this playbook a name and we'll just call this play 1 testing the archive
module now who are
the hosts that I want to target if I so wish I could target all devices in my
topology in my
case here I'm just going to target the blue box okay now I don't want to gather
facts that is not
going to be useful so I'm just going to configure that to be false in order to
save time for execution
and now to specify the tasks that I want to create here so the first task I'll
just call create archive
of some text files now what I'm actually going to use here well we know the
fully qualified
collection name of this module is community dot general dot archive remember to WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
collection name of this module is community dot general dot archive remember to
install this with
Ansible Galaxy if you don't have this module and now I'm going to specify the
path now the path
is required after all and this is going to be all of the files that I want to
be targeting right here
now I can supply this as a list object if I so wish so I can just say my file
in fact let me give
the full path I'll say home nuggets user my file 1 dot txt and the second one
will be home nugget user
my file 2 dot txt now specify the destination pretty much what is the file that WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
my file 2 dot txt now specify the destination pretty much what is the file that
I want to create so
again I'll put it in the same directory and I'll just call this my archive dot
tar dot gz okay so
basically I want to be combining both of these files into a singular file and I
also want to
compress it with gzip compression now remember the way we can specify the
compression type is via
the format parameter now as per the option right here notice the valid values
if you want to specify
bzip to compression we actually just specify bz2 and if you want gzip WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
bzip to compression we actually just specify bz2 and if you want gzip
compression you just say gz
not gzip now as we can see here by default the compression is going to be gzip
so we actually
don't have to be explicit in this case here but if you so wish then you can do
so so just in the
interest of making the playbook more readable I will just explicitly specify
that I'm using gz
compression okay so let's just press colon wq to write and quit I will say
answer will play book
play 1 dot yml and to answer open it appears I actually don't happen to have
the community dot
general archive on my particular playbook because I've used an old virtual WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
general archive on my particular playbook because I've used an old virtual
machine right here so
let me just type in the answer will galaxy collection install community dot the
general apologies about
that just give it a moment now I have the collection installed let's try to
read on the playbook again
so the playbook kicks off and as we can see as per the recap we have a change
on the device
and if we now go back to our blue box if I do an LS notice we happen to have a
compressed archive
of the fails right here so let's just do it we did in the previous video let's
just remove
these particular files fail 1 and my fail 2 dot txt so all that is left right WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
these particular files fail 1 and my fail 2 dot txt so all that is left right
now is the actual
archive if I want to undo that what I can do is say tar dot x to extract the
tarball I will say
z because that is how we extract the tarball using gzip compression via tar and
then say
f for the fail and target the fail my archive dot tar dot gz if I hit enter if
I do an LS now we
have these fails back and again if I just cat them and to enter there is fail 1
the content is still
the same and I can't fail to the content has not been modified now we notice
that from the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
that from the
documentation we had reference to the concept of globbing pretty much all glob
bing is going to allow
us to do it's going to allow us to specify particular fail paths by utilizing
very particular
patterns so as opposed to having to feed in every single individual file that
you want to be a
part of this particular archive you can use a fail globe to effectively match
on a particular
pattern now the most common fail globbing technique is to use the asterisk so
let's maybe say you
wanted to archive all files that happen to end in dot txt you could say aster WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
wanted to archive all files that happen to end in dot txt you could say aster
isk dot txt which
pretty much means anything so long as it ends in dot txt that would match and
now you could
create an archive of all those dot txt files or you could specify say for
example the question
mark sign or question mark character should I say which is going to allow you
to match any
particular character so say for example I did fail question mark dot txt this
here would match
on any single character right here so if I happen to have a fail called let's
maybe say fail one dot
txt that would match if I happen to have fail five dot txt that would also WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
txt that would match if I happen to have fail five dot txt that would also
match because it is one
single character or matching here however if I had a fail called fail 10 dot t
xt then that
actually would not match the pattern because what we're actually wanting here
is the word fail
followed by any single character immediately followed by dot txt this does not
follow this
particular format because we have two new characters so that is all we're
really talking about with
respect to globbing we're talking about using patterns to match particular file WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
respect to globbing we're talking about using patterns to match particular file
paths if you
want to see more paths that you can specify then by all means I would encourage
you to research
more advanced lennox globbing techniques but for now we're not going to spend
too much time on it
let me instead just demonstrate that this technique can be used directly within
the
Ansible playbook so let's go back again so let's remove the my archive dot txt
and what I will do
now is I'll create one called my file 10 dot txt to use example that I just
said there so let's
just say this is fail 10 just write and quit so here we have our three text WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
just say this is fail 10 just write and quit so here we have our three text
files let's go in to
our playbook once again and all I'm going to do here is as opposed to
specifying multiple individual
paths all I want to do is I want to use this globbing technique so in this
particular example
here let me just change this to a question mark and let's also change the
compression to be bz to
this time for a little bit of difference and again we'll change the name of the
extension to be bz2 as
well so let's see how this actually shakes out let's just press colon wq to
rank it and now see
Ansible playbook on play one and to answer now we create a new archive let's go WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Ansible playbook on play one and to answer now we create a new archive let's go
back and see the
results case if I do an LS we can see we have created the new archive file
right here if say for
example I move my file 1 dot txt and my file 2 as well as my file 10 dot txt
and I'll put it into
another directory just so it's out the road let's maybe go into the documents
directory okay so now
they're not here and what I will do now is extract the archive and we'll
actually see which files are
inside of that archive so if I say tar dot x to extract lower case j because it
is bz2 compression WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
is bz2 compression
and specify the file with the f switch and then say my archive dot tar dot bz2
and to answer
now we extract into an LS check this out right here notice the contents are
only my file 1 dot txt
and my file 2 dot txt the my file 10 dot txt file was not included whereas if I
just happened to
remove my archive as well as the files and I go into my documents right here
here are the files
right here and I'll just say move everything back a directory let's go back out
to an LS there are WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to an LS there are
the three files I now go back into my playbook and I change the globbing
technique to specify that
it's going to be an asterisk which will match any characters or any amount of
characters if I
now rerun it and I hit enter we now create the archive once again but this time
let me just remove
all of my files and then dot txt so all we have now is the actual archive let's
just say tar dot x
to extract lower case j then if I now specify my archive dot tar dot bz2 and to
enter do an LS
now we can see here all three files are present because effectively all three WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
now we can see here all three files are present because effectively all three
files were matched
on the globbing pattern and thus a part of the my archive dot tar dot bz2
archive file