Okay friends, so let's begin looking at defining variables within our Playbooks
directly.
So what I'm going to do here is I will create a new Playbook. Let's just call
this 'More Vars'
because I have no originality. Let's go in here, go into Inset Mode, have our
three hyphens at the
top, get the Playbook in name, we'll just say 'testing more Vars stuff'. What
host are we going to target?
We'll just say 'all'. And the Tasks. We will say the name is 'printing
variables'. Now I will use
the debug module with the message parameter and the message I will print is WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
the debug module with the message parameter and the message I will print is
going to be
my variable value is in within my colour braces. I will just say 'nugget var'
okay and let me just
close off the quotation marks. So right now I will just write and quit. And if
I run Ansible Playbook
against 'More Vars' I enter. Ansible is frantically trying to locate that
variable file that I have
specified. It'll be checking the group vars direct to the host vars directory,
all those places.
But alas as we can see here the 'nugget var' variable is undefined. Now like I
say we could
actually specify this within a configuration file which is actually the best WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
actually specify this within a configuration file which is actually the best
way to do this.
But it is important that we do understand that it can be created directly
within the Playbook itself.
So what I'm going to do here is I'm going to use the 'Vars' key and this is
going to allow me to
define my variables directly here. So let's go up here. If I say 'Vars' and I
specify the name of the
key that I want to use which is going to be 'nugget var' and let's just give
the value 'cbtn'.
So now that is defined within the Playbook when we go to call this particular
variable we should
actually get a response. So write and quit. Clear the screen, rerun the Play WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
actually get a response. So write and quit. Clear the screen, rerun the Play
book. Check it out right
here. We actually have a success. We are printing out my variable value is 'cb
tn'. Now let me just
show you a few things here. If we go in to say for example the group vars and
we cut out all dot
yaml we can see we happen to have a key called DNS_server. Let's go and call
that key okay.
So we're going to my 'more vars' and this time here I'm going to call the DNS
server variable.
Okay so we're in the Playbook. Hit enter and as we can see here we have the
variable value is
2.2.2.2 and 8.8.8.8. The yellow box here is getting the value from its host WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
2.2.2.2 and 8.8.8.8. The yellow box here is getting the value from its host
variable file which is
overriding the group variable but the blue box here is getting the group
variable value. Now what
if I happen to create another clash once again? What if I happen to maybe
define the variable here
within the Playbook as well? So I just say DNS_server and I try to get the
value 9.9.9.9 within the
Playbook if I run this here and I rerun the Playbook notice the variable
defined directly within the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
defined directly within the
Playbook actually overrides any of the configuration files. So remember we
talked about the order of
operations the priority. Remember at the lowest priority it would be group
variables defined within
the all.yaml file which should be overwritten by your own custom group
variables with child
groups taking precedence over parents and then the host variable files would
supersede that yet again
just be aware that variables defined within the Playbook themselves actually
override all of these
files. Okay now one last thing we actually have this Playbook right here as we WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
files. Okay now one last thing we actually have this Playbook right here as we
can see. Define the
variable as 9.9.9.9. What if I try to pass in via the command line the actual
variable value here
DNS_server what is going to happen this time? So if I say Ansible Playbook give
the name and I say
dash e for extra variables and I specify that DNS_server should actually equal
the value 192.168.1.1.1.
If I hit enter aha look at this the command line arguments actually supersede WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
If I hit enter aha look at this the command line arguments actually supersede
even the ones
defined directly within the Playbook. So the true order of precedence is the
commands passed in via
the CLI then we have the variable defined in Playbooks themselves and then we
go towards the files
which is going to be host then the group files and at the very end variable
defined in all.yaml.