So friends in the previous video we discussed the general structure needed in
order to create an
Ansible role and this particular video we're going to take that information and
actually do it for
real. So with that said let's just get to it. So here is the plan and this
particular video
we're going to create a new directory structure for the role that we're going
to be generating.
Once again we're going to take advantage of the Ansible Galaxy and net command
to auto generate
this for us. Then once we have done that we're going to actually populate the
content needed within WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
content needed within
the correct files. With that in play a role should be operational and then we
're going to call a role
from the thin Apple Book itself. Now I'm not being fully transparent here we're
not just going to
quickly go in and populate the files. Instead we're going to actually follow a
very common format
or rather should I say a very common process that generally comes about upon
the creation of an
Ansible role. Because quite honestly these roles are often best just from
regular playbooks. So WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
regular playbooks. So
someone writes an Ansible playbook it happens to be getting used quite a lot
and as such the
decision is made to convert this into a role whereby it's much more portable
and reusable.
So as per that convention we're going to actually copy that by first actually
creating an Ansible
playbook and we will walk through the process of transforming that regular Ans
ible playbook
into a brand new shiny Ansible role. Now one thing to state just off the bat to
be very aware of WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
is that these Ansible roles very often not necessarily by the way but very
often
are designed to be shared. That is why they are published on publicly
accessible websites such
as Ansible Galaxy. So because of this if they are going to be shared and they
can be freely downloaded
you have to be very very careful that you don't actually package up any
sensitive data inside of
these files within the role itself. So things like passwords, things like
sensitive configurations
within your network that type of thing always be sure what it is you happen to WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
within your network that type of thing always be sure what it is you happen to
be publishing.
So with that warming out the road let's go and begin creating an Ansible
playbook.
So let's just say touch let's just call this play1.yml the name itself doesn't
actually matter and
people go into this particular file. So I'm going to insert mode have my three
hyphens at the top
and I'll just say the name is playbook for multiple tasks. Okay let's just
pretend that this playbook
is doing it much more than I actually will configure it to do. Okay so let's
have our host be the
all group so that's going to be the blue box and the yellow box targeted and WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
all group so that's going to be the blue box and the yellow box targeted and
now what I will do is
I'll go down and define my tasks. Okay so this is very much similar to what we
saw before I'll
have the name key and I'll just say maybe a task one or just a configuring DNS
servers and what I
will do is I'll use the Ansible built-in template module and the source is
going to be which is
called the file DNS dot j2 and the destination where it's going to be copied to
the actual render
configuration is going to be in the etsy resolve dot conf configuration of all
of the target devices.
So like I say this is just one simple task let me add in another one and I'll WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So like I say this is just one simple task let me add in another one and I'll
just say task two
I'm just taking figuring ntp servers and in fact I've just got a little bit of
a typo there.
Now in the interest of time I'm going to invoke the power of imagination and as
opposed to writing
multiple different templates and multiple different variables instead here I'm
just going to simply
type a little message here and I'll just say successfully configured ntp
servers okay so I'm
totally cheating here I'm not actually doing the configuration in this case
here but it's just
allowing you to see multiple tasks and again this one here can be task three I WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
allowing you to see multiple tasks and again this one here can be task three I
'm just taking
figuring sftp and again just to keep things nice and quick let's just use that
our imagination
and just print out the message successfully configuring sftp okay so we have
three different tasks one
of which we're actually going to do for real and in the interest of time the
other two were
going to pretend that they were successfully configured so what we're going to
have to do
is to define some variables as well as to create this little ginger template
okay so let's go and
do that just now let's go into the host files here so I'll go into blue box dot WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
do that just now let's go into the host files here so I'll go into blue box dot
yml and I'm going to
insert mode here's my three hyphens at the top for the ammo file and I'm going
to have a key called
DNS servers and a colon have a hyphen and then denote the actual servers I want
to have associated
with that particular device sauset 8.8.8.8 as well as 8.8.4.4 okay so escape
colon wq to write and
quit let's say vim yellow box dot yml again insert mode three hyphens make sure
we're calling the exact WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
we're calling the exact
same key and in this case here the DNS servers can be 8.8.8.8 and let's move it
to 192.168.1.1
okay and now I will go into my templates directory and I'm going to create a
file called DNS.j2
so let's go in here with vim and I'm going to insert mode we don't have to have
our hyphens
and I'm going to say curly brace percentage to have my control logic and I'm
going to say if DNS
servers and then percentage curly brace pretty much meaning that if there is a
DNS server key for a WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
DNS server key for a
particular host variable file we actually want to go about trying to configure
that DNS server
for that device now inside of that conditional statement we're going to have
another piece of
control logic and I'm going to loop through the DNS server so I'll just say for
DNS in DNS servers
and again percentage curly brace all I'm going to say is the keyword name
server and then have my
double curly braces for the variable value and then populate the variable I
have selected which WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
have selected which
is just a word DNS now because I have opened my for loop I'm going to have to
say curly brace
percentage end for percentage curly brace and at the very end notice the for
loop is encased within
the f block so once again we're going to have to use a control block to close
that off to so I will
say end if again with my percentage curly brace so this should be my little
configuration right now
if I go back to a CD back back I find I just say answerable playbook and I try WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to play one dot
yml hit enter give it a moment and look at this right here we have task one
configured DNS servers
we can see we have a change and for task two and task three it says
successfully configured
NTP servers and successfully configured SFTP wink wink we know nothing actually
happened with
respect to those last two tasks but again we're just pretending so it seems
here that we have a
working playbook and if I just happen to go to my yellow box here for example
and I do them
it say resolve dot conf and hit enter as we can see here the rendering has WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
it say resolve dot conf and hit enter as we can see here the rendering has
worked perfect so what we
now want to do is to transform this working playbook into a working and simple
role so I'm going to
create the directory structure here so what is the command that's going to be
answerable galaxy
in it and let's just call the role my servers okay because apparently we're
configuring SFTP servers
NTP servers wink wink as well as a DNS server okay so let's just hit enter here
now that role has
been successfully created if we happen to run a tree command on it there is the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
been successfully created if we happen to run a tree command on it there is the
directory structure
needed and what we now want to do is to begin breaking down our ansable
playbook as well as
things like our templates and our variables and begin populating them into the
correct files now
one thing just to be clear here we're still going to make use of our host fires
and group
bars directory that doesn't actually change basically the template used is
still going to be able to
suck in those values and all the inheritance is still in play the variables
that we're talking
about here are the ones that are defined in the playbook or the role in this
case let me show you
what I mean so the first thing you want to do is you want to look at our ans WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
what I mean so the first thing you want to do is you want to look at our ans
able playbook the
original one and begin looking at what can be made generic so let's go back
into the play so take
this part right here okay now again none of this is absolutely essential it's
down to your own
discretion but just for an example in the case here of the first task why don't
I leave the task name
task one hardcoded because it's always going to be the first task but perhaps
maybe this particular
part here the part that describes what is happening configuring DNS servers why WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
part here the part that describes what is happening configuring DNS servers why
don't I make that a
variable value so that perhaps it can be populated via a different variable
that is the type of
variables we're talking about here similarly with respect to the source right
here the actual j2
templates maybe someone else wants to use a different templates so in this case
here we can
remove the hard coding and in this case substitute in a much more generic
variable value that can
just be called same again for the destination perhaps we're not going to always
override the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
override the
ec resolve.conf configuration file once again a candidate for a variable value
so let's do the
same thing for task two and for task three okay so let's go in here and begin
to make this as
generic as we can or as generic as makes sense so what I will do here is I will
go in leave task
one the same but this part here the description let's just have our double
curly brace and we'll
give it a name we'll just say task underscore one okay the DNS j2 let's remove
this again we'll
have a double curly brace and we'll just say task one templates okay and again WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
have a double curly brace and we'll just say task one templates okay and again
the destination and
in fact let's maybe change that name from task one template and we'll go with
task one source
okay and again for the destination for all the same convention we'll say task
underscore one
this and task two again we'll leave the actual task two part but the
description will just change
to the variable of task underscore two and we'll remove the message here let's
just maybe change
the last part so we'll leave successfully configured hard coded let's give this
a variable name of
task two config and let's do the same thing for task three okay so again just WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
task two config and let's do the same thing for task three okay so again just
delete this part
leave successfully configuring and we'll have task three config okay so right
now this is a lot
more generic it means that someone can control this whereby they can change the
name here it's
still going to be task one after all they could perhaps feed in a different
source and perhaps a
different destination and what we could do is for the values that previously
existed we could
actually populate them in the defaults file so basically any user of the role
if they don't
actually supply any other variables themselves they will just naturally inherit WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
actually supply any other variables themselves they will just naturally inherit
all of the values
that we previously saw however if they want to change and override any values
they could specify
a more specific variable inside the vars directory so let's go begin defining
those variable values
then and let me go in to the my servers directory and then inside of the
defaults here if i say
vim main dot yml what i'm going to do is to go into insert mode okay so press i
and i'll just go WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and i'll just go
and define those particular variables okay so the first one is going to be task
underscore one
which is going to be the name presented and i'll just make the value updating d
ns server okay so
no one supplies a value this is the value they're going to inherit task one
source we'll give that
a default of dns.jtuber going to have to put this particular file into the
correct folder of course
we'll say task one desk that's going to be the etsy resolve.conf configuration
file unless something
else is specified let's say task two we're just updating i think it was ntp WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
else is specified let's say task two we're just updating i think it was ntp
servers i said and
task to underscore config we'll just say the ntp servers and then we'll have
task underscore three
as a variable just say updating sftp and then we have the task three config
variable which is going
to end up reading successfully configuring the sftp servers okay now right now
that is all the
variables that are defined in our playbook right now which is soon to be going
to be migrated
into our ancable role via tasks and in fact before i do that one thing we have WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
into our ancable role via tasks and in fact before i do that one thing we have
to ensure is that
this particular template the dns.jt is in the correct location so what i'm
going to do here is if i go
back to my home directory let's go into templates right here we can see we have
the dns.jt if i
cat this right here this is not going to be picked up quite just yet quite
simply because it's in
the wrong templates folder so all i'm going to do is copy this particular file
i'm going to copy
enter the correct folder within the my servers role case it's going to go into
the templates
directory there okay so now if i go back in and go into the my servers it WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
directory there okay so now if i go back in and go into the my servers it
should be copied over
let's go into cd for the templates inside of the role itself now we have the d
ns.jt perfect
now we still need to have our tasks defined so here's what i'm going to do let
me go back to my
home directory and all i'm going to do is let me cat the play one dot yaml file
okay and let me
just copy the tasks here okay so copy and i'm going to go back into the
directory structure for the
my servers this time i'm going to go into the tasks directory and enter the
here we have the main WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
here we have the main
dot yaml so if i happen to open this up okay what i'm going to do here is go
into this yaml file
i'm just going to paste then what i just copied there okay so here we have all
of our tasks now
the very first thing we're going to have to do here is actually remove the
tasks key we no longer need
that by virtue of the task being within this particular tasks file the key here
is actually
unnecessary and simple understands that everything we have defined here is
indeed a task okay so let's
remove this and what we're now going to have to do is to fix the indentation WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
remove this and what we're now going to have to do is to fix the indentation
basically out dent
everything to the very edge okay so let's think about this we now have defined
our answerable tasks
within this particular file okay and we have all of these variables things like
task one now what
value is that going to take well it's going to take the default value that we
just specified same here
for task one source task one test that type of thing okay so like i say the
value is by default
if we go back out a directory going to defaults we're going to pick up all the
values inside of
here okay and populate it within that particular file we just saw similarly WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
here okay and populate it within that particular file we just saw similarly
notice the task one source
is referencing something called dns.j2 if we go into the template directory
within the role itself
that file actually exists in this directory structure so it is going to be
found and like i say when
we happen to create a playbook and call the role itself if we happen to create
the play and the
same directory structure whereby we have a host first directory or group first
directory
but answerable is still going to find all of those values so they have not been
suddenly lost just
by virtue of using a role so if i happen to write a new playbook let's just WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
by virtue of using a role so if i happen to write a new playbook let's just
call this one play two
dot yml let's have her three hyphens at the top we'll give this playbook a name
and we'll just say
playbook that uses a role now the host i'm going to target is going to be the
all group now think
about what we just did within the role we had three separate tasks of course
two of the tasks
were basically dummy tasks that we just mocked up but once again imagine these
were really long
and complex answerable tasks now because they have been wrapped up into a role
we can just simply
call them like this okay we just use the keyword roles colon they may ever hyp WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
call them like this okay we just use the keyword roles colon they may ever hyp
hen to the not array
and let me specify all the roles we want to use while the role we happen to
create was called
my servers and right now that is now our answerable playbook that is all we
need to do now to actually
call that to reuse all that code be just simply reference the name of the role
okay so check this
out if i press escape colon wq write and quit if i say answerable playbook play
two dot yml and to
enter oh as we can see we actually have a little bit of a syntax error within WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
enter oh as we can see we actually have a little bit of a syntax error within
our variables
so let's just go into the my servers go into the defaults do them main dot yml
as we can see here i
didn't actually close off the quotation mark so let's just put in the final
quotation mark and let's
try the playbook once again hit enter and bam look at this right here we've
actually performed
three different tasks updating DNS servers updating ntp servers this part here
was a variable
similarly for task three updating sftp this part here was a variable whereas
this part here was WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
this part here was
hard coded and if you want to override those variables we could easily do so by
specifying them
and the vars directory so it's going to my servers role let's go into the vars
directory and i'll
go into the main dot yml file right here let's press insert for insert mode let
's just change
maybe the variable for task three config let's just change this to some awesome
pizza okay let's
press escape colon wq to write and quit if we go back to our play let's rerun
the playbook that calls
the role and to enter and check this out right here look at task three this WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
the role and to enter and check this out right here look at task three this
part here is actually
overwritten the default so it now says the task has successfully configured
some awesome pizza now
like i said the beauty of having these roles is if i just wanted to perhaps do
all these configurations
update sftp update ntp update dns well i can just do all of that by calling
this particular role
but if i wanted to perhaps build upon that i can still go and begin configuring
my own
answerable tasks let's just say tasks name so to say task four more stuff and WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
answerable tasks let's just say tasks name so to say task four more stuff and
again of course
this is a frivolous example right now i'm just printing out some text but you
can imagine if i
wanted to go and write another template to do another configuration all of that
would be possible
but look how modular this is i'm getting access to all those three tasks just
by simply calling the
role name and i can easily build upon that and do additional things and before
i save this actually
let me just fix the indentation here if i press escape colon wq to write and
quit let's rerun the
playbook check this out right here look at this scrolling on up we have the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
playbook check this out right here look at this scrolling on up we have the
first three tasks as
called by the answerable role and at the very end we have just added on our own
custom task
just like that this is the beauty power and portability of the answerable role