In this video, we will be creating our very first playbook.
There are online Yammer tools you can use to create playbooks, and that is for those who do not have
much experience with the editor or VM editor.
So when you log into the Linux machine, you would have to use a VI or VIM attitude to create playbooks.
But if you do not know how to use that, then the tools that are available for you are like online yaml
tools dot com or code beautify dot org.
You can also download tools and windows such as notepad plus plus create your YAML file and then you
can copy it over to the Linux system.
All right, so before we actually get into creating our very first playbook in our lab environment,
the best thing to do is that we take a snapshot because we have installed ANSIBLE software.
So we have taken the snapshot of we installed Linux already.
Now we will take a second snapshot after we have completed the Ansible software install.
So this way, if we make a mistake, we could go back to either the point when we did the Ansible software
install or the Linux software install.
All right.
So for that, we'll go to our.
Oracle virtual manager.
And here we'll have a Linux install point.
Okay.
Our machine is running already.
That's okay.
You can leave it running and you can click on take and type in the name and Sybil install dash point.
Now of course, you could pick any name that will remind you where or when you took the snapshot.
All right.
Click.
Okay.
All right.
This is going to be a lot faster, of course, but not as fast as if the machine is powered off.
All right.
So now you have two snapshots here.
You could go back to any one of them when you make a mistake.
All right.
So that is done.
You go go ahead and close this.
Now, let's go and see what first playbook we are writing.
All right.
The first thing we have to do is become root.
Now, you could run playbook as a root user or a normal user.
So it doesn't really matter.
What matters is what commands or what function that playbook is going to do.
If Playbook is going to update your system with the Yum!
Update, then of course you need to have the playbook being run or created as a root.
All right.
What we will do is we'll create a directory in ETSI Ansible and we call it playbooks.
This is where we keep all our playbooks throughout this course.
And then we'll see into this playbook and we'll create our first YAML file and we'll name it first yaml
once we vim the file are added.
Edit The file will define dash dash dash three hyphens that will signify that this is a YAML file.
Then we'll define the name of the playbook, which is my first playbook.
Then comes the host.
Where do you want to run this task or this playbook you want to?
We will start off it just local host then the task what exactly you want it to do.
If you notice the task is right below the indentation is right below name and host.
That is that matters a lot.
It cannot be before a few spaces before the host or name.
So what the task is, the name of the task is test connectivity.
And now if you pay close attention, the name, the hyphen is right below the task.
The T, it can move to the right if you want to, and the corresponding module's is going to be right
below it.
You cannot have that hyphen before the spaces of the task or the T are.
The module that we're going to run is the paying module.
And again, you notice that panga module is right below the name of the task.
All right, let's go into a Linux machine.
Right here is a Linux machine.
And before we do anything, let's create a snapshot, which we already did, by the way.
Sorry.
Yes, we have taken our snapshot.
Now go ahead and become route, which I am already route.
Yes.
When I do ID it shows I am root.
Okay.
Let's make a directory at see and symbol.
Playbooks.
This is the directory.
We will create all our playbooks.
Let's seed into the directory.
Let's create our first YAML file.
Define that it says yaml file.
But the name my first playbook.
Now you could put this double code or no codes.
It doesn't really matter now.
When you hit enter, you're going to notice one cool thing of using them editor that it will bring you
right below where it should be.
And that is under the name.
And right here you define the host where you want to run this YAML file against and we'll run it against
the local host.
Now, when you hit Enter, I'll bring you back to the host.
We could leave a space, an empty line to make it look more neat and organized.
Come down right here.
You define tasks.
What exactly you want this playbook to do the task.
Now, when you hit enter, it will bring you the it will do the indentation for you and move your cursor
to the right.
Now you could go back and start your indentation right here if you want to.
That's fine.
Or you could let the editor decide where it's going to be.
That's fine.
Name?
And what is the name of this playbook?
Sorry.
This tasks that task will test the connectivity of the local host.
And the module that it will use to test the connectivity will be paying module and that's pretty much
it.
Escape and save the file.
Now the file is saved.
Now let's do a few checks.
Check syntax of playbook.
You have to check to see if there are no syntax errors.
You can run this command or you could do a dry run, which is ansible playbook.
Once the check is complete, then you can run the playbook by running the simple ansible playbook command.
All right, let's go back to our Linux machine and we'll check the syntax first.
That would be Ansible Dash playbook.
Dash, dash syntax.
Dash check first yaml file.
All right.
It's telling it.
The host list is empty.
Only localhost is available.
That is fine.
Is only telling you that they are.
It's not seeing any host in the in the at ansible host file.
So that's fine.
Now let's do the same thing.
Instead of syntax check will do just check which will run as if it's you're running the actual file,
but it won't do anything.
It would just tell you the syntax looks good and perfect.
Nice.
Now we will run the actual playbook.
And that is Ansible.
Sorry.
Ansible Playbook.
And if you are inside of the playbooks directory where the playbook is located, then you could simply
put in the name of the playbook.
If you are somewhere else, then you have to put in the absolute path.
All right, let's run it now.
It's going to check against the local host is going to peg it and it's going to tell it.
Everything looks good.
Local host task is good, test connectivity is good.
All look good.
Now let's go back to our VI editor and go to our playbook.
And let's say if we move this one space back accidentally, right, and then come back down here and
now we run our Ansible playbook.
Right here, you see, it's giving you error message.
The error appears to be in Etsy as well.
Playbook first yaml, file line three, column two, but maybe elsewhere in the file depending on the
exact center C indentation matters a lot.
So if you go back to it and you move this one step over, then it should run fine.
When you run it, it will give you that message that it's everything looks good.
Same way.
If you go back to the Vim editor and you move this right here and now you run it.
It will give you that error message again because the pen is not aligned to the name.
So for that, you can move this three, four steps back.
That's fine.
But Ping has to be exactly aligned where the name is now.
You when you run it, it should be okay.
There you go.
See, it is running fine.
There are no.
H's beautiful.
All right, so that's what our first Ansible playbook is.
Now, we.
We wrote the playbook, but if you didn't want to write the playbook, if you remember, we ran the
Peng module without a playbook, and it was just Ansible, and we ran it against local host dash m dash
module.
That is, if you do not want to write a playbook and you run it ad hoc command, by the way, which
we will cover later on.
All right.
Moving on.
And please note running Ansible without a playbook, you will just run the script Ansible and a running
Ansible with the playbook.
Then you have to define that playbook or the location of the playbook.
For that you will run the command ansible dash playbook, sorry.
And then SpaceX followed by the Ansible playbook location.
All right.
So congratulations, you created your first playbook and we will go on and create a lot more playbooks
from this point on.