Hi everyone, welcome to this video
In this video we are going to demonstrate how to create a playbook file in Ansible and
how to use a playbook file to carry out tasks on a managed node
So in our last video we looked at how to install Ansible on Ubuntu 24.04 and how to configure
the control node to access the managed nodes. So this video assumes that you already have installed Ansible on Ubuntu 24.04 and have
also configured the managed node which will be controlled by the control node
Alright so now in this video we are going to look at the concept of playbooks and we
are going to see how to use an inventory, how to incorporate modules and variables in
a playbook file. So here is our lab setup. So here this is my control node and this is my managed node
Alright now before anything else we are going first of all to configure the managed node
in our inventory. Now an inventory is a database of managed hosts or targets that Ansible controls
And by default the host file is located in the ansible-hosts. So this is the default inventory path
So you can view this using the nano text editor. And let's open the file
So here you can see we have accessed the hosts file and by default everything is commented
So now it's upon you now to make your or define your managed hosts
And there are several ways of doing this. So one of the most basic or fundamental way of specifying a host is by typing out its
IP address. For example you can type in my case the managed hosts IP is 45.79.214.22 like that
So that's one way of doing this. The most preferred way is to organize the host and a host group
So for example we can organize these under a host group called let's say for example
web servers like that. And we enclose the host group inside the double brackets as you can see
So now these hosts, these managed hosts falls under the category of a group called web servers
And now we are going to save the changes. So I'm going to press ctrl-o, hit enter and ctrl-x to exit the hosts file
All right. Okay so now we have specified the managed node or the target in our inventory file
Now you can list all the nodes or the managed nodes on your system or control node using
the ansible all list hosts command like that. Hit enter. So this will list the IP addresses of all the hosts that will be managed by ansible
So in this case we have only one host and this is its IP address here
All right. Now later I'm going to show you how to create a custom inventory file but at the moment
we are going to go with a default inventory file. Now the next step is to create a playbook
So for demonstration I'm going to create a simple ansible playbook that prints a simple
message on the terminal. So to create a playbook you can run, you can use the nano text editor or vim editor, whichever
editor you're using, then followed by the name of the playbook. So here I'm going to call it hello.yaml
So ensure that you have the .yaml file extension because playbooks are written in yaml language
So I'm going to hit enter. All right now to start off I'm going to type three hyphens to mark the beginning of the
playbook file. I'm going to hit enter. Then I'm going to specify the host group in my inventory file
All right so here we gave we gave our managed node or we grouped our managed node under
the group host called web servers. Okay like that. All right so ensure that the group host you specify here has been specified in the inventory
file otherwise you're going to get an error when running the playbook
Okay so next I'm going to give this playbook a name. So let's say for example print a message on the terminal
You can give it any arbitrary name you want. So now we are going to specify the tasks to be carried out by this playbook
So here I'm going to specify the tasks. And by the way be careful about the indentation
So make sure you have correctly idented your playbook file. So here we have tasks
So under tasks we have I'm going to give the task a name
So display a simple message to standard out
All right then I'm going to use the debug module. All right so now in ansible modules are standalone scripts of code that perform actions on managed nodes
So debug is responsible for printing out a message to standard out and it takes a parameter
called message. Okay so this one contains the stream to be printed to standard out
So for example hello world. Welcome to ansible
All right. So ensure that your playbook resembles what you have here
All right so take note of the indentation. All right. Okay so now I'm going to save this playbook
So I'm going to press ctrl-o then ctrl-x. All right now before you run a playbook it's always advisable to check whether the syntax
is correct. And you can do so by running the command ansible playbook then syntax check
Then followed by the name of the playbook. In our case hello.yaml
Then I'm going to hit enter. So as you can see the syntax is correct and at the end we get the name of the playbook
All right. So this shows that our playbook is quite good. So now let's run the playbook. Sorry
Then followed by the yaml file or the playbook which is hello.yaml and hit enter
So as you can see the runtime is in progress and it has completed
All right. So as you can see here we get the message hello world welcome to ansible
All right. So this shows that the ansible has successfully run as we expected
All right. So here's a play recap. So you can see the task executed successfully
All right. All right. So yeah. So this shows that the playbook has successfully executed
All right. Now let me clear the screen. Now I earlier mentioned that you can also create a custom inventory file
All right. Away from the default one. Now the advantage of a custom inventory file it allows you to organize different sets of
managed nodes. OK. So now what I'm going to do I'm going to create a custom inventory file
All right. So I'm going to create a directory specifically for this
So I'm going to create a directory called custom underscore inventory
Then I'm going to navigate into this directory. So cd custom underscore inventory. OK
Then I'm going to use the nano editor. I'm going to specify the inventory name
So let me just give it the name inventory. Then I'm going to hit enter
All right. So similarly here I'm going to group. I'm going to place my managed node under the web servers host group like that
So I'm going to specify its IP address 214.22
Then I'm going to save the changes and exit. All right. Let me go a directory up or go to the home directory
So now you can use this custom inventory using the hyphen I option
So you can run the ansible playbook file. So ansible playbook. So we are going to use the hyphen I option to specify the custom inventory file and the
path to this inventory file is custom underscore inventory then inventory like that
Then followed by the path of the playbook file. In this case hello.yaml
OK so we have specified the hyphen I option. Which allows us to spell out or specify the custom inventory
And lastly we have specified the playbook file. So hit enter. So as you can see once again the playbook run successfully even when we specify the
custom inventory file. All right. OK. So now we are going to create another playbook file and we are going to look we're going
to use more modules for a better practical example. All right. So now I'm going to clear the screen
Now in this example I'm going to create an ansible playbook that installs Apache web
server on the managed node. All right. So now I'm going to run the command nano
And I'm going to give the playbook name. So for example install underscore Apache dot yaml. OK
Like that. OK so now this playbook will now give you more practice on ansible modules. OK
So as before I'm going to start with three hyphens. OK. Then I'm going to specify the hosts group
So here we have web servers. OK. Then here I'm going to give the name of the playbook
So for example install Apache web server. OK
Then here I'm going to give an attribute for privilege escalation. So here we have become yes
So what this does it allows the remote user configured on the managed node to execute
this playbook. OK. So on the terminal we will provide a directive to provide the SSH user for the sudo user
configured on the managed node. So on the managed node we already have a sudo user configured and the sudo user is called
linux techie. Now we'll later on go to the hosts file and define this user. OK
So for now we are going to specify the become directive and this allows for privilege escalation
so that the playbook can run successfully on the remote node. OK
So now let's go on. So here I'm going to specify the tasks and we are going to have three tasks. OK
So the first task is to we are going to update the local cache
So update the local cache on the remote node. OK. So here sorry
Update local cache. All right. So here we are going to use the apt module. OK
So this is a package management module. All right. So here under apt we are going to have update underscore cache. Yes. OK
So that is what you have. So here there's a space. Sorry. OK
After the full colon. So this task updates the local cache. So this is the equivalent of running the command sudo apt update. OK
Now moving on. We have the next task. So here I'm going to specify the name
So here we are going to install Apache. OK. So once again I'm going to use the apt module
All right. And here we have two parameters. So here I'm going to specify the name of the package to be installed
In this case Apache 2. OK. And I'm going to specify the state
So here we have present. OK. So the apt module in this case takes two parameters
The name Apache 2 and state present. So state present ensures that the Apache 2 package is installed
All right. Then to our last task. So we are going to ensure that Apache service starts
So start Apache. OK. So we are going to use a different module
So here we have the service module. Sorry. We have the service module
So here once again we're going to have the name parameter Apache 2
And we're going to specify the state. So here we're going to say started
All right. So that is our playbook file. OK. We have three tasks
We are going to update the local cache. Thereafter we install Apache
And finally we're going to make sure Apache is started. So now let me save this playbook. OK
Now the next step I'm going back to the hosts file. OK
Then I'm going to specify the user that is going to log in and carry out or execute the playbook. OK
So here I'm going to navigate all the way down. And I'm going to specify ansible underscore ssh user
So we have Linux Techie. So this is the user residing on the remote system on which this playbook is going to
be executed. And you must ensure that the user is privileged or has pseudo privileges
All right. Then I'm going to save this and exit. OK. So now you also need to ensure that you copy the SSH key to the user
So you can run the command ssh copy id followed by the user Linux Techie at the IP address
So 45.79.214.22 and hit enter. So we already have saved the SSH key to the remote system under this user
So I'm going to exit out out of that command. OK. All right
So now we have our playbook file ready. And we have made the necessary changes to the hosts file
So the next step is to now run the playbook. So ansible playbook followed by the name of the playbook file
In this case install Apache.yaml. All right. Then I'm going to specify an attribute hyphen K
So this will prompt me for the remote user's pseudo password. OK
So here we are prompted for the pseudo user's password. So this is the password for the Linux Techie user
I'm going to provide this. So on the screen nothing will be printed out for security reasons and hit enter
So as you can see the playbook is running. The tasks are running as you can see
OK the local cache has been updated. So we are installing Apache now
All right Apache has been installed. Apache is running. OK. So as you can see now here we have OK is equal to 4 and change this is equal to 2
So this means that two tasks have been carried out. OK. So we have updated the local cache and installed Apache. OK
So let's now see and confirm whether Apache is running. So to do so I'm going to launch my browser here and I'm going to open a new tab and I'm
going to type out the IP address of the server. So it's 49.79.214.22 and I'm going to hit enter
All right so here we get the default Apache 2 page. So this confirms that the Apache web server has successfully been installed and is running
All right. So this confirms that a playbook executed successfully. Now let's switch over and see how you can make use of variables inside a playbook
Let's now shift our focus to variables in playbooks. Now if you're coming from a programming language such as Java or Python or C++ you must have
covered the concept of variables. And variables are used pretty much the same way in Ansible playbooks
Now a variable is simply an abstract value that temporarily stores data or information
Now to better demonstrate this I'm going to create a simple playbook that displays a message
Hello World on the screen. So let me just create a sample playbook file
I'm going to call it greetings.yaml. Greetings.yaml. All right. All right
So once again I'm going to start off with three hyphens as you can see
Then I'm going to specify the hosts attribute. In this case web servers
All right. Then I'm going to specify the name of the playbook
So for example variable example usage. All right. So now to define a variable I'm going to use the vars directive
So the vars directive specifies the variable name. So in this case I'm going to call it greetings. Sorry
Greetings so feel free to give it any abstract name you want
So here greetings is our variable and it stores the value. Let's say hello world. OK
All right. So let's move on and specify a task. So in this case we are going to call the task something like print message on the terminal
All right. And once again we will use the debug module and the message parameter
Like that. OK. So here we are going to reference the variable
So we do this using the double calibrises. Then we specify the variable name. Greetings. OK. Then
So this greetings will reference this variable here. So we are going to have hello world
Then I'm going to say something like welcome to Ansible. And I'm going to enclose all the statements inside double quotation marks like that. OK
So this is the complete playbook as you can see. So here we have defined the vars directive
So this defines the start of our variable. So in our case greetings is the variable here
And this is the value of the variable hello world. And down here we are displaying a message
So we have referenced the greetings variable using the double calibrises. OK
And we have enclosed the entire statement using the double quotation marks
So let me save this. So I'm going to press control O, hit enter, then control X to exit from the playbook
All right. Now let's run the playbook. So once again I'm going to run the command Ansible playbook
Entered by the playbook name. In this case, greeting.yml. And I'm going to hit enter
All right. So on your screen you can see the playbook has run
And as you can see here, hello world has been referenced. OK
So this was the value of the greetings variable. So just to preview the playbook file
So this is what you had. OK. So greetings took the value of hello world, which you can see here
Hello world. All right. Then it went ahead to print this string here
Welcome to Ansible, which is this string that you can see on the standard output
All right. Let's now look at another example. So let me clear the screen
Now you can also have an array of variables. All right. So now to demonstrate this, I'm going to include a number of variables
So let me create a new playbook to demonstrate this. So once again, I'm going to create another playbook
So let's say students details dot yaml
All right. So hosts web servers. OK
So let me now go straight to the to defining the variables
So but before that, let me just give the playbook a name
So student details. OK
Then I'm specifying the variable name using the vars directive. So so here we're going to have a number of variables
So the first variable is a student. So here I'm going to give it the value of Alex. OK
Then the city. Let's say Miami. OK
Then let me specify the tasks. So here tasks. So the name of the task is display
Student details. So here I'm going to use the debug module
Then I'm going to use the message parameter and see the student
Name is then I'm going to reference the variable name here. So here. Student. OK
And I'll see. And lives in. City
Then I'm going to enclose all the statement inside the double quotation marks. OK
As you can see there. So this is going to reference this value
This is going to reference this variable which carries the value of Alex
And this variable will reference the value stored in city which is Miami
All right. Let me save this and exit. OK. Let me run the playbook
And as you can see now. We have the student name is Alex
And lives in Miami. OK. So here we have used multiple variables to do the job. OK
Now as I previously mentioned you can also have an array. You can use an array with variables. OK
So now to demonstrate this. So I have a playbook file here that
Installs multiple packages. So the playbook is a 01 playbook.yaml. So let's preview
Let's preview this playbook. So as you can see I have a playbook that has three tasks
So the first task installs a package called NeoFetch which displays system information
The second one installs Bashtop which is a system resource monitor. And finally we have the third one called Doof which is a disk space utilization monitor
All right. So now as you can see in this playbook there's a lot of repetition in the tasks
So we have the first task. The second one and the third one
And we can find that they look all much similar. And the playbook looks quite long
Now we can use an array of variables to simplify this playbook
So now I'm going to create a playbook to demonstrate this. So I'm going to create a playbook and I'm going to call it
Let me see. Let me just call it. Install essential packages dot yaml
All right. So let me start by creating the playbook. So here we have the hosts, web servers
The name of the playbook. Let's say install multiple packages. All right
Then you have the become directive which allows for privilege escalation. So this will take the value of yes
Then here we're going to specify the variable using the vars directive
And here I'm going to give it the name packages. So now let's install the packages
So the first one is neo-fetch. Then the second one is a bash top
And finally we have the doof package. All right. Now let's come to the tasks
So let me give it a name. So install many packages
Then here once again we have the apt module. Then we have name
So this is where we are going to reference the packages variable
Like that. And as always we are going to enclose it with double quotation marks
Like that. Okay. Then finally we will declare the state which is present
So this ensures that the packages are installed. All right. So that's our playbook
And as you can see it's much shorter and much neater than the previous playbook which had
multiple tasks defined. All right. So now I'm going to save this and I'm going to exit
So now let's run the playbook. But before we do so let's check the syntax and see whether it's correct
So syntax check. Then the name of the playbook install essential packages
All right the playbook looks good. So now let me run the playbook file
Install essential packages dot yaml with a hyphen k option for privilege escalation
So this can be written as ask hyphen become pass
So this will prompt me for the remote user's sudo password. I'm going to specify this and hit enter
And as you can see the playbook is running and it's installing the packages defined in
the playbook. So this will take a bit of time as you can see on your screen
All right so the packages have been installed. All right. So let's go over to the remote host
So let's run NeoFetch. And as you can see NeoFetch has displayed the information as we expected
Let's run a bashtop. And as you can see the resource monitor is displaying
All right let me press Q to exit. Let me run the command called doof
So this is a disk usage monitor which is a perfect replacement for the DF command
And as you can see we get information about mounted volumes, the total size, the used
size and the available size. All right. So that's how you can use an array using a variable
OK so that's it about this lecture. I hope you found this valuable and insightful
So we have talked about how you can create a playbook. We have discussed how to create an inventory, how to use modules and also how to declare
and use variables. So that's it for this lecture. If you found this valuable please do leave a like and subscribe to our channel
So see you in the next video