- All right, so we need to write a playbook
that uses the ngnx role
to install ngnx on (mumbles) family host.
So, let me first use ansiable-galaxy role list
just to verify.
And there we go,
geerlingguy nginx is already installed.
Geerlingguy.
If geerlingguy has written it, is good.
You can safely use it.
So, let me create lab8.yaml
and in lab8.yaml
install nginx, hosts, all.
In tasks, well, no tasks,
we are going to start with pre-tasks.
So, what are we going to do?
Well, I'm still in development phase.
Let's do remove Apache.
Next, do we need anything else?
Perform package update
and then we can use the roles.
So, in the roles we are going to run
geerlingguy.nginx.
And after running the role,
remove the yum package cache.
Well, formally you can use any tasks.
If you wanna be more specific about it,
you can run post tasks.
And I think we have a problem here in the indentations.
So, let me fix that.
And let's have a look at the example as well.
So, the role is geerlingguy nginx
pre-task roles and post task,
right now we have them all in the play header
as level one items.
This is why I love having a look at examples.
So, pre-tasks, and there you can see pre-tasks and roles
and that is looking okay.
So, I can get back to my lab eight.
So, remove Apache.
How are we going to do that?
Well, if we are going to do it decently,
service, name httpd, state stopped, enabled no,
as well as package.
Oh,
I'm skipping over the name here
because that doesn't really add anything.
Name httpd, state absent,
and oh, now look at that.
We need some conditionals as well.
And in order to make that a conditional,
shall we we use a block?
So, in a block we can include multiple tasks.
And this again is one of these cases
where I think you should verify your syntax
this time let's go to the Ansible documentation.
So, docs.ansible.com.
So, let's go to the Ansible documentation,
community documentation.
Oh,
2.9 and I want an example for block.
Block is typically one of these cases
that you don't use too often,
so it's wise to look it up and there we go.
So, we have tasks and in tasks, all right,
the block is just a part of the task.
So, it's the other way around,
smart move to look it up.
So, block is just a part of the task
and yeah, that is what we've got here.
So, this should be good.
And then how did we have the dependencies?
You can see the dependencies are in the block specification.
The when statement is in the block specification
and you know what,
when Ansible facts distribution equals CentOS,
this is so close to what I need
and I'm just going to copy paste it.
So, oh, there we go.
So, I'm first finishing this block configuration
and that is gonna be right here.
So, when Ansible facts distribution
that needs to be indented at a block level.
So, we have that all right.
And we will continue this later.
Name perform package update,
that's a part of the block.
So, that would be, well, you know what?
We are on (mumbles) family only.
I'm changing my mind.
Why would you use package,
if you are on one specific distribution?
Always go for the most specific solution.
Now, we have this yum
and yum certainly has something to update packages.
Ansible-doc on yum will tell us about it.
So, yum updates only,
that suite is okay.
And force yum to check if cache is out of date
and re-download if needed.
I think that should be yum update only.
So, let's double check these updates
especially in the list.
So, we need to specify what we want to do, right?
So, I am going to use state
and there we can see state latest.
So, that means that I am going to use
name, star, state latest.
That should be doing it.
When Ansible distribution facts equals.
Now, the when can be specified as a list.
So, Ansible facts distribution underscore family.
It's all about family is either Red Hat
Or Ansible facts distribution family is Rocky.
Hey, is that correct?
No, because if you use a list,
then all of them must be true.
So, we better get back to an or statement.
So, when, Ansible facts distribution blah blah,
this is what we need.
Next, we are going to include the role finally.
And then we have the post task
and in the post task we are going to do what?
We are going to run just one task,
which is going to be the yum task again.
And what is the yum task going to do?
Yum task is...
Well we have an update cache and an update only.
So, last task is to run the yum package cache
but where is it?
Sudo minus I.
We have this var cache directory.
And in var cache, we have a dnf directory.
And in the dnf directory, just looks like a cache.
And I don't know
there's a specific option to remove the cache,
but I do know
that we are just going to use file path var cache,
dnf, state absent recurse.
Yes.
Is this going to do it?
Well, at least it worked
giving it a first try.
So, Ansible playbook on lab8.yaml.
Here we go.
And oh, no.
Yeah, of course.
If you make big playbooks like this
then you might get errors.
You can see that yum name starred doesn't work out.
So, let's get back to the Ansible doc for yum.
And do we see anything for name argument?
So, name, oh, when using status latest
this can be a single quote, star, single quote.
So, single quote, star, single quote.
It's a quoting issue.
Often in Ansible,
it will be a quoting issue.
So, let's put this between single quotes
and let's give it another try, try number two.
And what do we get?
That's an interesting one.
The role geerlingguy nginx was not found in home Ansible.
Yeah, that's me, that's a typo.
Geerling is the family name
and guy that is the dude,
the Geerling dude.
And that's a double G.
As you can see,
we are doing a little bit better, but oh, no.
Remove Ansible.
What do we get?
Conditional check, Ansible facts distribution family
or Ansible facts distribution Rocky error
while evaluating conditional.
And we can see,
has no attribute distribution family.
That's an interesting one.
We can quickly check that Ansible
on Ansible one minus m setup,
and let's just search for family.
And aha! It's not distribution family,
it is OS family.
And everybody, you might think,
"Hey Sander, don't you know where you were going
when you were starting?"
Of course I do.
But the thing is that
it's so easy to get confused
and you will learn so much more from my errors.
I mean, if everything is working smooth from the beginning
you are going to try to memorize it.
I don't want you to memorize it.
I want you to understand how you can find the information.
So, OS family let's work with OS family.
Yes. OS family.
There we go again, Ansible playbook on lab8.yaml.
And of course perform package update
that might take a minute or two.
So, as you can see the packages update is finally done
and well, this is sufficient proof
that the Pre-tasks are doing what they need to do.
Right now, we are in the Ansible role.
So, the role is continuing
and we should be good in just a little while.
You can see a little bit of work
to be done on the directory.
So, what do we see a recurse option
requires state to be directory, owner, root, blah blah.
So, how about we do that without?
Then the sad thing is that you need to run it again.
Do we really need to run it again?
Let's have a look at Ansible playbook
dash dash help pipe less.
We have not yet looked at all these different options.
And what I'm interested in here
is list task and start at task.
So, Ansible playbook lab8.yaml
dash dash list tasks is showing us what?
Is showing us all these different tasks.
So, I am going to use a start at task file.
That makes it so much easier
to just run this file task again
and there you can see without a recurse
it is doing all right.
So, it was a little bit of work.
I hope you appreciated the insight
into getting to the result
but this is what I had in mind for the lesson eight lab.
Let's continue with the next lesson.