In this video we are going to learn ansible tags.
Tags are the reference or aliases to a task and a playbook.
Instead of running an entire Ansible playbook, you can use tags to target a specific task you need
to run.
Let's look at a playbook that we will be creating as an example in this video, we'll we'll name of
this playbook http by tags YAML of course a lot easier to use or to practice is through a HTTP install.
All right.
So let's go with our dash, dash, dash.
We define the name as set up Apache server.
That's a name of our playbook.
Where do we want to run our playbook against local host and the tasks?
The name of the first task is install hpdi.
Yum will be the module, the name of the package state of the package present meaning to install it.
We know all of this.
I'm sure now the new things that we will learn is defining the tags right here.
Just make sure the tags is fully.
Vertically aligned with the name Yum and the tag should be right below it.
That's how the indentation should be.
Once you are in the state and present and you hit enter, the tags will be automatically indented to
the right.
If it is intended to the right, then you have to move it back so it could be aligned with name and
yum.
Anyway, so tags colon space equal i dash http.
That's just a name that I picked.
You could pick any name that would make you remember or give you a reference to run this specific task.
All right.
The next task is start to service.
The module of the service will be service.
The name of the service.
And what do you want to do with the service?
You want to have it, start it, and the tags you want to use it is as for star to dash http.
Now of course, this is just a name that I picked.
You could pick any name.
Right here is the tag.
And if you wanted to run only this specific task, you will run the command ansible dash playbook http
by tags.
The YAML file of course define the yaml file with minus t option and the name of the tag.
You can also do dash dash tags and the name of the tag.
Same thing with this one.
You can run the command ansible playbook http by tags that yaml that's tag and TSS use it to start http
PD.
That's the name of the tag that I picked.
All right.
To list all tags in a playbook.
This is the command you run ansible dash playbook, http by tags, yaml space dash, dash list, dash
tag.
All right, so let's go to my Ansible playbook and we'll go through these commands one by one.
Here is my ansible control node.
I am an etsy ansible playbooks directory.
Who am I?
I'm logged in as root.
Okay, good.
Now let's create them htp by tags or any name of your yaml of your choice them htp by tags.
I have already created this YAML file earlier to save us time.
And here you notice everything from first line.
Second.
Third.
Fourth.
Fifth, sixth.
7 to 8 is the same to install the package.
Now the ninth line you notice, as I was mentioning, the tags has to be fully vertically aligned with
name and jump.
All right.
So the tag that we defined is AI PD.
You could also put the tag right below this line, but when it goes down the line, then you have to
put in.
So dash with it.
Then your tags work.
So this is one option to do it.
Or it's better you put it back up here and just simply put, tag colon space and the tag name.
Now coming down to the second task.
You all know this and the tags we're defining right here.
All right.
So if you have not done up to this point or you have not read in the playbook, pause the video right
now and write the playbook.
All right, once you're done.
Let's save our playbook.
All right.
The playbook is written now.
Let's run the command to list the tags inside of the playbook.
Ansible Bash Playbook.
HTTP Buy Tags.
Dash Dash List tags.
List of tags that are inside of this playbook.
Right.
It's telling you the playbook is on the local host and the name of the playbook is set up Apache Server,
which is the name of the playbook.
The test tags are right here.
One is ie dash http.
The second one is dash http.
This is the first one.
And this is the second one.
Very nice.
Okay.
Now let's move this to the left.
Go back to our slide.
To run a task using a tag, you have to use ansible playbook http by tags yaml, which is the yaml file
minus RT tag and the name of the http pd.
All right, let's do that.
All right, before we run that, we wanted to make sure the HTTP feed is removed from the system, so
just too yum.
So first check if the package is there, grab hpdi.
Yep.
The package is there.
So just do yum.
Remove HPD.
HPD is our favorite package that we could use as an example to install or write write playbooks for
Ansible.
And it's a lot faster, of course, as I said, to install.
All right, so our Hpdi package is gone now.
Run Ansible.
Playbook.
The name of the playbook.
And the tag.
The tag that you want to run.
I only want to run.
I tag meaning I to install it.
I only want to install it.
HTTP dd package nothing else.
I do not want to start the service, so let's run it.
Set up a pottery server gathering facts and solve these DPD.
All right.
So.
Changed equal one.
So it should work.
So we will check that by running rpm q graph for http pd and we notice the packages installed.
Now if you remember this, let's do cat http bi tags.
This also had another task which is to start the service.
So since we ran it with the tag, so let's see if it starts the service or not, which it should not
start.
The Service System.
CTL status.
PD.
There you go.
See, it says inactive.
Yes.
It did not start.
See how a tag works?
We could actually target specific task within a playbook.
All right, so the next one is to skip a task using a tag.
You can simply do Ansible Dash playbook.
The name of the playbook.
Dash to escape, dash tags.
And then the name of the tag.
So this way, the entire playbook would run.
Except that one task.
Now you're probably thinking, hey, hey, hey, wait a second.
Don't we have something like this that we learned earlier?
Because use task option to start a playbook at a specific task.
Right.
That we learned when we actually defined when we ran the command ansible playbook and the YAML file
then dashed to start at task and the task name.
Right.
And if you wanted to install the HTTP package only we could have run the the ansible command, the name
of the YAML file, start at task and then the name of the task to install HDP.
It should have worked, right?
Answer is a yes and no.
Yes is because it will run and it will install HTTP package and no is because it will also start the
service as well.
So what it is saying is start from this task and do everything else that comes after.
So which is completely different from the tags.
Tags is basically targeting only the task that you want to run and nothing else.
That's the difference.
And that's why I want to clarify this.
So you don't get confused that that is something we already did.