WEBVTT

1
00:00.000 --> 00:04.400
Okay friends, so let's begin looking at defining variables within our Playbooks

2
00:04.400 --> 00:05.040
directly.

3
00:05.040 --> 00:08.010
So what I'm going to do here is I will create a new Playbook. Let's just call

4
00:08.010 --> 00:08.880
this 'More Vars'

5
00:08.880 --> 00:13.840
because I have no originality. Let's go in here, go into Inset Mode, have our

6
00:13.840 --> 00:14.560
three hyphens at the

7
00:14.560 --> 00:18.890
top, get the Playbook in name, we'll just say 'testing more Vars stuff'. What

8
00:18.890 --> 00:20.160
host are we going to target?

9
00:20.160 --> 00:24.820
We'll just say 'all'. And the Tasks. We will say the name is 'printing

10
00:24.820 --> 00:26.800
variables'. Now I will use

11
00:26.800 --> 00:31.200
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

11
00:26.800 --> 00:31.200
the debug module with the message parameter and the message I will print is

12
00:31.200 --> 00:31.920
going to be

13
00:31.920 --> 00:37.060
my variable value is in within my colour braces. I will just say 'nugget var'

14
00:37.060 --> 00:38.160
okay and let me just

15
00:38.160 --> 00:41.800
close off the quotation marks. So right now I will just write and quit. And if

16
00:41.800 --> 00:43.360
I run Ansible Playbook

17
00:43.360 --> 00:47.210
against 'More Vars' I enter. Ansible is frantically trying to locate that

18
00:47.210 --> 00:48.400
variable file that I have

19
00:48.400 --> 00:52.000
specified. It'll be checking the group vars direct to the host vars directory,

20
00:52.000 --> 00:53.200
all those places.

21
00:53.200 --> 00:58.400
But alas as we can see here the 'nugget var' variable is undefined. Now like I

22
00:58.400 --> 00:59.040
say we could

23
00:59.040 --> 01:02.600
actually specify this within a configuration file which is actually the best
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

23
00:59.040 --> 01:02.600
actually specify this within a configuration file which is actually the best

24
01:02.600 --> 01:03.520
way to do this.

25
01:03.520 --> 01:07.160
But it is important that we do understand that it can be created directly

26
01:07.160 --> 01:08.400
within the Playbook itself.

27
01:08.400 --> 01:11.560
So what I'm going to do here is I'm going to use the 'Vars' key and this is

28
01:11.560 --> 01:12.240
going to allow me to

29
01:12.240 --> 01:17.040
define my variables directly here. So let's go up here. If I say 'Vars' and I

30
01:17.040 --> 01:18.400
specify the name of the

31
01:18.400 --> 01:21.890
key that I want to use which is going to be 'nugget var' and let's just give

32
01:21.890 --> 01:23.360
the value 'cbtn'.

33
01:23.360 --> 01:27.840
So now that is defined within the Playbook when we go to call this particular

34
01:27.840 --> 01:28.560
variable we should

35
01:28.560 --> 01:32.290
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

35
01:28.560 --> 01:32.290
actually get a response. So write and quit. Clear the screen, rerun the Play

36
01:32.290 --> 01:33.440
book. Check it out right

37
01:33.440 --> 01:38.270
here. We actually have a success. We are printing out my variable value is 'cb

38
01:38.270 --> 01:39.360
tn'. Now let me just

39
01:39.360 --> 01:43.090
show you a few things here. If we go in to say for example the group vars and

40
01:43.090 --> 01:44.000
we cut out all dot

41
01:44.000 --> 01:48.570
yaml we can see we happen to have a key called DNS_server. Let's go and call

42
01:48.570 --> 01:49.520
that key okay.

43
01:49.520 --> 01:54.120
So we're going to my 'more vars' and this time here I'm going to call the DNS

44
01:54.120 --> 01:55.040
server variable.

45
01:55.040 --> 01:58.720
Okay so we're in the Playbook. Hit enter and as we can see here we have the

46
01:58.720 --> 01:59.840
variable value is

47
01:59.840 --> 02:05.870
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

47
01:59.840 --> 02:05.870
2.2.2.2 and 8.8.8.8. The yellow box here is getting the value from its host

48
02:05.870 --> 02:06.960
variable file which is

49
02:06.960 --> 02:10.820
overriding the group variable but the blue box here is getting the group

50
02:10.820 --> 02:12.240
variable value. Now what

51
02:12.240 --> 02:16.680
if I happen to create another clash once again? What if I happen to maybe

52
02:16.680 --> 02:18.320
define the variable here

53
02:18.320 --> 02:22.620
within the Playbook as well? So I just say DNS_server and I try to get the

54
02:22.620 --> 02:24.800
value 9.9.9.9 within the

55
02:24.800 --> 02:28.940
Playbook if I run this here and I rerun the Playbook notice the variable

56
02:28.940 --> 02:30.480
defined directly within the
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

56
02:28.940 --> 02:30.480
defined directly within the

57
02:30.480 --> 02:34.440
Playbook actually overrides any of the configuration files. So remember we

58
02:34.440 --> 02:35.760
talked about the order of

59
02:35.760 --> 02:39.910
operations the priority. Remember at the lowest priority it would be group

60
02:39.910 --> 02:41.280
variables defined within

61
02:41.280 --> 02:45.540
the all.yaml file which should be overwritten by your own custom group

62
02:45.540 --> 02:46.800
variables with child

63
02:46.800 --> 02:50.580
groups taking precedence over parents and then the host variable files would

64
02:50.580 --> 02:52.400
supersede that yet again

65
02:52.400 --> 02:57.200
just be aware that variables defined within the Playbook themselves actually

66
02:57.200 --> 02:58.320
override all of these

67
02:58.320 --> 03:03.310
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

67
02:58.320 --> 03:03.310
files. Okay now one last thing we actually have this Playbook right here as we

68
03:03.310 --> 03:04.400
can see. Define the

69
03:04.400 --> 03:10.380
variable as 9.9.9.9. What if I try to pass in via the command line the actual

70
03:10.380 --> 03:11.680
variable value here

71
03:11.680 --> 03:15.190
DNS_server what is going to happen this time? So if I say Ansible Playbook give

72
03:15.190 --> 03:16.160
the name and I say

73
03:16.160 --> 03:21.160
dash e for extra variables and I specify that DNS_server should actually equal

74
03:21.160 --> 03:24.160
the value 192.168.1.1.1.

75
03:24.160 --> 03:30.030
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

75
03:24.160 --> 03:30.030
If I hit enter aha look at this the command line arguments actually supersede

76
03:30.030 --> 03:30.640
even the ones

77
03:30.640 --> 03:34.960
defined directly within the Playbook. So the true order of precedence is the

78
03:34.960 --> 03:36.080
commands passed in via

79
03:36.080 --> 03:40.360
the CLI then we have the variable defined in Playbooks themselves and then we

80
03:40.360 --> 03:41.440
go towards the files

81
03:41.440 --> 03:45.350
which is going to be host then the group files and at the very end variable

82
03:45.350 --> 03:46.880
defined in all.yaml.
