1
00:00:00,720 --> 00:00:04,260
In this video we are going to learn ansible tags.

2
00:00:04,500 --> 00:00:09,150
Tags are the reference or aliases to a task and a playbook.

3
00:00:09,300 --> 00:00:16,530
Instead of running an entire Ansible playbook, you can use tags to target a specific task you need

4
00:00:16,530 --> 00:00:17,310
to run.

5
00:00:18,090 --> 00:00:25,650
Let's look at a playbook that we will be creating as an example in this video, we'll we'll name of

6
00:00:25,650 --> 00:00:35,910
this playbook http by tags YAML of course a lot easier to use or to practice is through a HTTP install.

7
00:00:36,180 --> 00:00:36,720
All right.

8
00:00:36,720 --> 00:00:39,300
So let's go with our dash, dash, dash.

9
00:00:39,300 --> 00:00:42,120
We define the name as set up Apache server.

10
00:00:42,120 --> 00:00:44,250
That's a name of our playbook.

11
00:00:44,280 --> 00:00:48,780
Where do we want to run our playbook against local host and the tasks?

12
00:00:48,780 --> 00:00:51,620
The name of the first task is install hpdi.

13
00:00:51,900 --> 00:00:58,200
Yum will be the module, the name of the package state of the package present meaning to install it.

14
00:00:58,200 --> 00:01:00,450
We know all of this.

15
00:01:00,450 --> 00:01:05,670
I'm sure now the new things that we will learn is defining the tags right here.

16
00:01:05,880 --> 00:01:09,840
Just make sure the tags is fully.

17
00:01:10,730 --> 00:01:17,140
Vertically aligned with the name Yum and the tag should be right below it.

18
00:01:17,150 --> 00:01:19,490
That's how the indentation should be.

19
00:01:19,730 --> 00:01:27,260
Once you are in the state and present and you hit enter, the tags will be automatically indented to

20
00:01:27,260 --> 00:01:28,130
the right.

21
00:01:28,160 --> 00:01:34,250
If it is intended to the right, then you have to move it back so it could be aligned with name and

22
00:01:34,250 --> 00:01:34,970
yum.

23
00:01:35,060 --> 00:01:39,650
Anyway, so tags colon space equal i dash http.

24
00:01:40,100 --> 00:01:43,320
That's just a name that I picked.

25
00:01:43,340 --> 00:01:51,200
You could pick any name that would make you remember or give you a reference to run this specific task.

26
00:01:51,740 --> 00:01:52,220
All right.

27
00:01:52,220 --> 00:01:55,310
The next task is start to service.

28
00:01:55,580 --> 00:01:58,080
The module of the service will be service.

29
00:01:58,100 --> 00:01:59,570
The name of the service.

30
00:01:59,570 --> 00:02:01,220
And what do you want to do with the service?

31
00:02:01,220 --> 00:02:07,370
You want to have it, start it, and the tags you want to use it is as for star to dash http.

32
00:02:07,610 --> 00:02:11,680
Now of course, this is just a name that I picked.

33
00:02:11,690 --> 00:02:13,460
You could pick any name.

34
00:02:13,850 --> 00:02:15,440
Right here is the tag.

35
00:02:15,440 --> 00:02:23,000
And if you wanted to run only this specific task, you will run the command ansible dash playbook http

36
00:02:23,000 --> 00:02:23,720
by tags.

37
00:02:23,720 --> 00:02:29,900
The YAML file of course define the yaml file with minus t option and the name of the tag.

38
00:02:30,110 --> 00:02:34,940
You can also do dash dash tags and the name of the tag.

39
00:02:35,180 --> 00:02:37,010
Same thing with this one.

40
00:02:37,010 --> 00:02:44,930
You can run the command ansible playbook http by tags that yaml that's tag and TSS use it to start http

41
00:02:44,930 --> 00:02:45,110
PD.

42
00:02:45,170 --> 00:02:47,060
That's the name of the tag that I picked.

43
00:02:47,750 --> 00:02:48,290
All right.

44
00:02:48,530 --> 00:02:50,660
To list all tags in a playbook.

45
00:02:50,660 --> 00:02:57,830
This is the command you run ansible dash playbook, http by tags, yaml space dash, dash list, dash

46
00:02:57,870 --> 00:02:58,520
tag.

47
00:02:58,550 --> 00:03:03,680
All right, so let's go to my Ansible playbook and we'll go through these commands one by one.

48
00:03:05,330 --> 00:03:08,750
Here is my ansible control node.

49
00:03:11,070 --> 00:03:14,310
I am an etsy ansible playbooks directory.

50
00:03:14,340 --> 00:03:14,970
Who am I?

51
00:03:15,000 --> 00:03:16,350
I'm logged in as root.

52
00:03:16,380 --> 00:03:17,250
Okay, good.

53
00:03:17,460 --> 00:03:29,340
Now let's create them htp by tags or any name of your yaml of your choice them htp by tags.

54
00:03:29,430 --> 00:03:34,800
I have already created this YAML file earlier to save us time.

55
00:03:35,100 --> 00:03:40,080
And here you notice everything from first line.

56
00:03:40,080 --> 00:03:40,530
Second.

57
00:03:40,530 --> 00:03:40,860
Third.

58
00:03:40,860 --> 00:03:41,310
Fourth.

59
00:03:41,310 --> 00:03:42,360
Fifth, sixth.

60
00:03:43,190 --> 00:03:47,290
7 to 8 is the same to install the package.

61
00:03:47,300 --> 00:03:56,600
Now the ninth line you notice, as I was mentioning, the tags has to be fully vertically aligned with

62
00:03:56,600 --> 00:03:58,700
name and jump.

63
00:03:58,940 --> 00:03:59,270
All right.

64
00:03:59,270 --> 00:04:03,890
So the tag that we defined is AI PD.

65
00:04:03,890 --> 00:04:13,190
You could also put the tag right below this line, but when it goes down the line, then you have to

66
00:04:13,190 --> 00:04:13,820
put in.

67
00:04:14,830 --> 00:04:16,720
So dash with it.

68
00:04:16,930 --> 00:04:18,540
Then your tags work.

69
00:04:18,550 --> 00:04:20,680
So this is one option to do it.

70
00:04:20,680 --> 00:04:28,210
Or it's better you put it back up here and just simply put, tag colon space and the tag name.

71
00:04:28,360 --> 00:04:30,490
Now coming down to the second task.

72
00:04:30,520 --> 00:04:34,340
You all know this and the tags we're defining right here.

73
00:04:34,360 --> 00:04:34,870
All right.

74
00:04:34,870 --> 00:04:39,820
So if you have not done up to this point or you have not read in the playbook, pause the video right

75
00:04:39,820 --> 00:04:42,310
now and write the playbook.

76
00:04:42,820 --> 00:04:44,080
All right, once you're done.

77
00:04:44,970 --> 00:04:48,990
Let's save our playbook.

78
00:04:49,440 --> 00:04:49,770
All right.

79
00:04:49,770 --> 00:04:51,060
The playbook is written now.

80
00:04:51,060 --> 00:04:55,380
Let's run the command to list the tags inside of the playbook.

81
00:04:55,890 --> 00:04:58,590
Ansible Bash Playbook.

82
00:04:59,190 --> 00:05:02,220
HTTP Buy Tags.

83
00:05:02,280 --> 00:05:05,010
Dash Dash List tags.

84
00:05:05,250 --> 00:05:07,800
List of tags that are inside of this playbook.

85
00:05:09,640 --> 00:05:10,150
Right.

86
00:05:10,150 --> 00:05:17,350
It's telling you the playbook is on the local host and the name of the playbook is set up Apache Server,

87
00:05:17,350 --> 00:05:19,270
which is the name of the playbook.

88
00:05:19,690 --> 00:05:21,790
The test tags are right here.

89
00:05:21,940 --> 00:05:24,520
One is ie dash http.

90
00:05:24,550 --> 00:05:27,010
The second one is dash http.

91
00:05:27,280 --> 00:05:28,480
This is the first one.

92
00:05:28,480 --> 00:05:29,740
And this is the second one.

93
00:05:30,460 --> 00:05:31,450
Very nice.

94
00:05:31,640 --> 00:05:31,900
Okay.

95
00:05:33,670 --> 00:05:35,950
Now let's move this to the left.

96
00:05:36,100 --> 00:05:37,660
Go back to our slide.

97
00:05:37,750 --> 00:05:46,000
To run a task using a tag, you have to use ansible playbook http by tags yaml, which is the yaml file

98
00:05:46,030 --> 00:05:49,660
minus RT tag and the name of the http pd.

99
00:05:49,900 --> 00:05:51,040
All right, let's do that.

100
00:05:52,950 --> 00:05:58,480
All right, before we run that, we wanted to make sure the HTTP feed is removed from the system, so

101
00:05:58,480 --> 00:05:59,740
just too yum.

102
00:05:59,770 --> 00:06:04,360
So first check if the package is there, grab hpdi.

103
00:06:05,300 --> 00:06:05,630
Yep.

104
00:06:05,630 --> 00:06:06,560
The package is there.

105
00:06:06,560 --> 00:06:07,790
So just do yum.

106
00:06:07,850 --> 00:06:10,140
Remove HPD.

107
00:06:10,190 --> 00:06:18,020
HPD is our favorite package that we could use as an example to install or write write playbooks for

108
00:06:18,020 --> 00:06:18,740
Ansible.

109
00:06:19,130 --> 00:06:24,170
And it's a lot faster, of course, as I said, to install.

110
00:06:24,470 --> 00:06:27,830
All right, so our Hpdi package is gone now.

111
00:06:27,830 --> 00:06:29,210
Run Ansible.

112
00:06:32,670 --> 00:06:33,430
Playbook.

113
00:06:33,450 --> 00:06:35,040
The name of the playbook.

114
00:06:35,770 --> 00:06:37,480
And the tag.

115
00:06:37,480 --> 00:06:38,890
The tag that you want to run.

116
00:06:38,890 --> 00:06:40,900
I only want to run.

117
00:06:41,500 --> 00:06:44,970
I tag meaning I to install it.

118
00:06:44,980 --> 00:06:46,300
I only want to install it.

119
00:06:46,320 --> 00:06:48,850
HTTP dd package nothing else.

120
00:06:48,850 --> 00:06:53,020
I do not want to start the service, so let's run it.

121
00:06:54,660 --> 00:06:58,410
Set up a pottery server gathering facts and solve these DPD.

122
00:07:01,550 --> 00:07:02,300
All right.

123
00:07:02,450 --> 00:07:03,410
So.

124
00:07:04,130 --> 00:07:06,050
Changed equal one.

125
00:07:06,050 --> 00:07:07,970
So it should work.

126
00:07:07,970 --> 00:07:15,150
So we will check that by running rpm q graph for http pd and we notice the packages installed.

127
00:07:15,170 --> 00:07:21,620
Now if you remember this, let's do cat http bi tags.

128
00:07:21,620 --> 00:07:26,840
This also had another task which is to start the service.

129
00:07:26,840 --> 00:07:34,190
So since we ran it with the tag, so let's see if it starts the service or not, which it should not

130
00:07:34,190 --> 00:07:34,520
start.

131
00:07:34,520 --> 00:07:35,810
The Service System.

132
00:07:35,810 --> 00:07:37,160
CTL status.

133
00:07:38,530 --> 00:07:39,030
PD.

134
00:07:39,700 --> 00:07:40,090
There you go.

135
00:07:40,090 --> 00:07:41,300
See, it says inactive.

136
00:07:41,320 --> 00:07:41,680
Yes.

137
00:07:41,680 --> 00:07:43,060
It did not start.

138
00:07:43,210 --> 00:07:44,560
See how a tag works?

139
00:07:44,710 --> 00:07:49,840
We could actually target specific task within a playbook.

140
00:07:50,050 --> 00:07:54,650
All right, so the next one is to skip a task using a tag.

141
00:07:54,670 --> 00:07:57,430
You can simply do Ansible Dash playbook.

142
00:07:57,850 --> 00:07:59,290
The name of the playbook.

143
00:07:59,290 --> 00:08:01,600
Dash to escape, dash tags.

144
00:08:01,690 --> 00:08:03,860
And then the name of the tag.

145
00:08:03,880 --> 00:08:07,090
So this way, the entire playbook would run.

146
00:08:07,090 --> 00:08:09,730
Except that one task.

147
00:08:10,270 --> 00:08:14,710
Now you're probably thinking, hey, hey, hey, wait a second.

148
00:08:15,190 --> 00:08:19,450
Don't we have something like this that we learned earlier?

149
00:08:19,540 --> 00:08:25,450
Because use task option to start a playbook at a specific task.

150
00:08:25,450 --> 00:08:25,900
Right.

151
00:08:25,900 --> 00:08:32,530
That we learned when we actually defined when we ran the command ansible playbook and the YAML file

152
00:08:32,560 --> 00:08:35,500
then dashed to start at task and the task name.

153
00:08:35,500 --> 00:08:36,010
Right.

154
00:08:36,100 --> 00:08:44,860
And if you wanted to install the HTTP package only we could have run the the ansible command, the name

155
00:08:44,860 --> 00:08:50,540
of the YAML file, start at task and then the name of the task to install HDP.

156
00:08:50,740 --> 00:08:52,150
It should have worked, right?

157
00:08:52,810 --> 00:08:55,450
Answer is a yes and no.

158
00:08:56,440 --> 00:09:05,110
Yes is because it will run and it will install HTTP package and no is because it will also start the

159
00:09:05,110 --> 00:09:06,510
service as well.

160
00:09:06,520 --> 00:09:12,650
So what it is saying is start from this task and do everything else that comes after.

161
00:09:12,670 --> 00:09:15,930
So which is completely different from the tags.

162
00:09:15,940 --> 00:09:23,190
Tags is basically targeting only the task that you want to run and nothing else.

163
00:09:23,200 --> 00:09:24,430
That's the difference.

164
00:09:24,430 --> 00:09:26,650
And that's why I want to clarify this.

165
00:09:26,650 --> 00:09:30,760
So you don't get confused that that is something we already did.
