Day-03 | Write Your First Ansible Playbook | For Absolute Beginners
Summary
TLDRIn this episode of the 'Ansible Zero to Hero' series, Abishek introduces viewers to Ansible Playbooks, emphasizing the importance of YAML for data serialization. He explains the basics of YAML syntax, including strings, numbers, lists, and dictionaries, and demonstrates writing a YAML file. Abishek then delves into the structure of an Ansible Playbook, detailing how to create plays with hosts, tasks, and modules. He guides through a practical example, showing how to install an Apache server and deploy a static application on an EC2 instance. The tutorial also covers how to execute an Ansible Playbook and verify its success on an AWS instance.
Takeaways
- đ The video is part of the 'Ansible Zero to Hero' series, focusing on day three where the basics of YAML and Ansible Playbooks are covered.
- đ The speaker introduces YAML as a human-readable data serialization language, emphasizing its use over text files for structured data input to applications like Ansible.
- đ YAML's advantages include its simplicity and readability, making it easier for humans to read and write compared to other data formats like JSON.
- đšâđ« The tutorial covers the basics of YAML syntax, including how to write strings, numbers, booleans, lists, and dictionaries, which are fundamental to understanding YAML files.
- đ The video explains how to create a YAML file, starting with a three-hyphen indicator, followed by key-value pairs and list declarations.
- đ ïž Ansible Playbooks are introduced as YAML files that contain a list of plays, each targeting specific hosts and executing a series of tasks.
- đ§ The structure of an Ansible Playbook is detailed, with explanations of 'plays', 'tasks', and 'modules', highlighting the importance of modules for executing actions.
- đ The 'gather facts' process is mentioned, which is the first task executed by Ansible to connect to the target host and gather necessary information.
- đ„ïž A practical example is given where the speaker demonstrates creating an Ansible Playbook to install an Apache server and deploy a static application on an EC2 instance.
- đ The video script provides a step-by-step guide on writing YAML files and Ansible Playbooks, including the use of built-in modules such as 'apt' for package management and 'copy' for file transfer.
- đ The execution of the Ansible Playbook is shown, including troubleshooting steps like checking for successful installation of services and deploying files to the correct directories.
Q & A
What is the main focus of the 'Anible Zero to Hero' series by Abishek?
-The 'Anible Zero to Hero' series focuses on teaching viewers how to use Ansible, starting from the basics and progressively moving towards more advanced concepts and practical implementations.
What topics were covered in the first two episodes of the series?
-In the first two episodes, Abishek covered an introduction to Ansible and getting started with it, as well as passwordless authentication in the entry file in Ansible and Ansible ad hoc commands.
Why is YAML covered before Ansible Playbooks in episode three?
-YAML is covered before Ansible Playbooks because many subscribers asked for the basics of YAML, and Ansible Playbooks are written in YAML, which is a human-readable data serialization language.
What is the significance of YAML in the context of Ansible?
-YAML is significant in the context of Ansible because it is used to write Ansible Playbooks. It provides a standardized format for defining configurations, tasks, and data structures that Ansible can execute.
Why are templating languages like JSON or YAML preferred over text files for passing data to applications?
-Templating languages like JSON or YAML are preferred because they offer standardized formats with a set structure, ensuring that data is passed in a consistent and error-free manner, unlike text files which can vary in format and may lead to syntax errors.
What are the basic elements one needs to know to write YAML files?
-To write YAML files, one needs to know how to write strings, numbers, and booleans, understand when to use lists and their syntax, and when to use dictionaries (key-value pairs) and their syntax.
What is the main difference between a list and a dictionary in YAML syntax?
-In YAML syntax, a list is defined using a hyphen for each item, while a dictionary uses key-value pairs without the hyphen, with each key directly followed by its value.
What does the 'become true' statement do in an Ansible Playbook?
-The 'become true' statement in an Ansible Playbook allows the play to be executed with elevated privileges, typically as the root user, which is necessary for tasks that require administrative rights.
How does the 'gather facts' task in Ansible work?
-The 'gather facts' task is the first task executed by every Ansible play, which checks if Ansible can connect to the target host and collects all the required information from it to proceed with the execution of the play.
What is the purpose of the 'copy' module in Ansible?
-The 'copy' module in Ansible is used to copy files from the local system to the remote locations on the target hosts, which is useful for tasks like deploying applications or configuration files.
Outlines
đ Introduction to Anible Zero to Hero Series
Abishek introduces the third episode of his Anible Zero to Hero tutorial series, summarizing the content covered in the first two episodes, which included Anible basics and passwordless authentication. The focus of this episode is on Anible Playbooks and the basics of YAML, a human-readable data serialization language. The video promises to cover YAML fundamentals and Anible Playbook structure, culminating in a practical demonstration of creating a web server and deploying a static application on an EC2 instance. Viewers are encouraged to follow along with notes and code available on a GitHub repository for continuous updates.
đ YAML Basics and Standard Templating Language
This paragraph delves into the definition and purpose of YAML, contrasting it with other data representation methods like text files and JSON. YAML is highlighted as a human-readable, standardized language for data serialization, which is crucial for complex data structures. Abishek explains the importance of standardization in templating languages for DevOps tools like Kubernetes and Anible. The paragraph also provides a beginner's guide to writing YAML, covering strings, numbers, booleans, lists, and dictionaries, and offers a personal example of a YAML file to illustrate the syntax and structure.
đ Anible Playbook Structure and Execution
Abishek explains the structure of Anible Playbooks, which are essentially YAML files containing a list of plays. Each play represents a set of tasks to be executed on specific hosts and can include modules, tasks, and handlers. The explanation covers the components of a play, such as the host section, remote user, and tasks. The paragraph also introduces the concept of modules in Anible, which are used to perform actions like installing packages or services, and emphasizes the importance of understanding the Playbook structure for effective automation.
đ ïž Practical Anible Playbook Example
In this segment, Abishek provides a practical example of writing an Anible Playbook to install an Apache server and deploy a static application. He details the steps involved in creating a single play with tasks for installing the web server using the apt module and copying an HTML file to the appropriate directory using the copy module. The explanation includes how to specify hosts, users, and tasks within the Playbook, and how to reference Anible's built-in modules for specific actions.
đ§ Running Anible Playbooks and Module Usage
Abishek demonstrates how to execute an Anible Playbook using the command line, including specifying the inventory file and the Playbook file. He discusses the initial gathering of facts by Anible to establish connections and collect information about the target hosts. The video shows the process of running the Playbook, which includes installing Apache and copying an HTML file to a web server directory. The paragraph also highlights the importance of checking the status of services and making necessary adjustments to security groups to allow traffic.
đ„ïž Final Deployment and Testing of the Web Application
The final paragraph covers the steps taken to ensure the web application is accessible. Abishek shows how to enable the security group for the EC2 instance to allow HTTP traffic on port 80. He then tests the deployment by accessing the application through the instance's public IP. The paragraph concludes with a brief discussion on the learning curve associated with Anible and the importance of referring to documentation when writing more complex Playbooks.
Mindmap
Keywords
đĄAnsible
đĄYAML
đĄPlaybooks
đĄModules
đĄTasks
đĄHosts
đĄInventory
đĄEC2 Instance
đĄGather Facts
đĄAd-hoc Commands
Highlights
Introduction to the third episode of the Anible Zero to Hero series.
Explanation of the basics of YAML, a human-readable data serialization language.
YAML's advantages over text files for complex data due to standardized format.
Comparison between YAML and JSON, highlighting YAML's human readability.
Step-by-step guide on writing YAML files, including strings, numbers, booleans, lists, and dictionaries.
Demonstration of creating a YAML file for personal information.
Introduction to Anible Playbooks as YAML files that consist of a list of plays.
Description of the structure of Anible Playbooks, including plays, host, remote user, and tasks.
Explanation of how to use modules within tasks in Anible Playbooks.
Example of installing an Apache server and deploying a static application using Anible Playbooks.
How to use the 'become' keyword in Anible Playbooks to execute tasks as a different user.
Using the 'apt' module to install packages and the 'copy' module to deploy files in Anible.
Executing the first Anible Playbook to install Apache and deploy a web page.
Explanation of 'gather facts' in Anible and its role in collecting information about the target hosts.
Troubleshooting the Apache installation and correcting the state from 'absent' to 'present'.
Enabling security group rules to allow HTTP traffic for accessing the deployed application.
Final demonstration of accessing the deployed web application via the EC2 instance's public IP.
Emphasis on learning Anible modules through documentation and practical examples throughout the series.
Transcripts
hello everyone my name is abishek and
welcome back to my Channel today is
episode three of anible Zero to Hero
series we have already completed the
first two episodes in this series where
in day one we learned about the
introduction to anible and getting
started with
anible in day two we learned about
passwordless
authentication in entry file in anible
and anible ad hoc
commands today is day three where we
will jump into the world of anable
playbooks before we will understand the
basics of yaml because a lot of
subscribers were asking about the basics
of yaml so we will cover that first and
then we will head towards understanding
anible Playbook
structure in the anible Playbook
structure we will cover what are plays
in anible what are modules tasks and
collections not just that towards the
end of this video we will also learn our
very first anable Playbook where we will
create a web server and deploy static
application onto that web server which
is hosted on on a ec2
instance so please watch this video till
the end it's going to be very
informative and you are also going to
gain practical
knowledge before I get started a quick
reminder all the notes related to this
anible Zero to Hero series will be
shared through the GitHub repository for
example if you go to the day3 folder you
can find the complete notes that I'm
going to talk about and also the yaml
files and any code that I'm going to use
in the demos so you can start this
repository Fork it watch it to get the
continuous
updates now let's move to the Whiteboard
and start our first topic for today that
is
yaml so what exactly is yaml
the textbook definition says yaml is a
human
readable data
serialization
language what does that
mean let's say you want to write a
python
application and this python application
needs an input which can be as simple as
list of
users or list of students in a class so
basically you want to pass some data as
an input to your python
application obviously this data can be
fed to the python application in
different ways one is you can pass this
data as input using a text file you can
pass this data as input using Json or
our topic for today that is
yaml now your question can be abishek
what's the problem with text file you
know if I'm passing this information
through a text file or data through a
text file I don't need to learn any
other languages such as Json or
yaml so text file is not recommended
especially if the data is simple it's
still fine but imagine if the data is
complex and has thousands of line text
file is not recommended because it's not
a proper format or there is no standard
structure where data has to be put in a
text file example maybe if I'm writing
this text file I can put list of
students name where each student name in
a new line so someone else who is
writing this text file they might put
two students name in a single line or
someone else who is writing this text
file they might put first student name
in the first line and maybe give some
spaces and second student name in the
third line third student name maybe in
the Fifth Line so the problem if you are
asking user to put the data in a text
file and pass it to your application the
data can be presented in different
formats why because there is no
standardization or a set requirement
that text file has to be written in this
format only otherwise your python
application can throw a syntax error
that's not possible because there is no
syntax or semantics to a text file this
is where templating languages such as
Json or yaml comes into the picture so
they are standard templating language
files that
are agreed accepted and defined
according to the standards so if you
don't write or if you don't put data
into the Json file as per the
requirement of the Json file
then you will get syntax errors that
means everyone who is writing a Json
file or everyone who is writing a yaml
file they need to follow a specific
standard that's why you will see that in
devops when you are passing some
information either to kubernetes or to
anible and many other applications you
use standard templating language such as
yaml where data is structured and
available in a serialized way you might
also ask but abishek why not Json and
why yaml the answer is simple yaml is
much more human readable and you don't
need to learn it's not very difficult to
learn yaml it's very easy to get started
with and also very much human readable
you will understand once I explain the
structure of yaml file so now I hope you
understood why usually data is
represented and passed as input either
in Json files or yaml files and not in
text files now the next thing is how to
write yaml it's very very simple even if
you follow the document that I have in
this repo you can learn yaml in exactly
10 minutes all that you need to know is
four things first is you need to know
how to write strings numbers and
booleans in yaml all three of them have
same syntax just provide the name column
space and value even for integer just
provide the name of the integer colum
space and the value name of the Boolean
colum space
value then you need to know when to use
a list and what is the Syntax for list
when to use dictionary that is key value
pair and what what is the Syntax for it
finally list of dictionaries which is of
course covered if you learn both lists
and
dictionaries if you know four of these
things then you know yaml and how to
write yaml let's take an example abishek
let's write a yaml file where I will
write about myself in a yaml file okay
the task is to just write about myself
in a yaml file so first I will put the
name of the yaml
file as abishek do
yaml every time you start writing a yaml
file so assume using whim I have created
this file name and what I'm going to
write in this file first line you always
have to provide three
hyphens this will tell that this is a
yaml file so you all always have to put
three
hyphens then let's say I'm going to
write about myself the first thing that
I'll write is what is my
name okay which is obviously a string so
column space
abishek if you want you can put that in
single quote or double quote then I will
say the age which is an integer but the
syntax is same column space and here I'm
going to say let's say some H so
whenever you're writing string you can
just put it in single quote or double
quote when it comes to age which is a
number you can just call in space and
put it then let's say if I'm working or
not which is a bullion right so bullan
should be true or false but the syntax
is same so if you just know how to write
string you know integer as well as
bullan as well then let's say what are
my
tasks so my task is not a single task I
have list of tasks so what I will do is
I'll click on enter and make sure you
leave two characters don't put anything
for the first two characters from the
third character just put hyphen space
and start writing about your task so my
first first task is let's say YouTube
content creation again click on enter
leave two spaces then from the third
hyphen
work again third and let's say what is
my next task take care of
home let's say I have another task to do
some exercise every day so this is how I
will write a list for string
number and or integer and Boolean the
syntax is same when it comes to list
first put the name of the list then
leave two spaces from third give a
hyphen and write the
list the list of your task then let's
say I want to put my
address address in address I have
multiple key value pairs again leave two
spaces from the third what is my first
key value pair
Street okay what is the value I'll put
some Street then
City then
State similarly pin code country so each
of these things is a key value pair
street is a key value is something city
is a key value is something state is a
key value is something so the only
difference between list syntax and the
key value pair syntax is when you are WR
writing list what you will do is for
each item in the list you will use a
hyphen whereas for each item in a
dictionary you will not use this
particular hyphen because they are key
value pairs so you will just declare
directly you'll not use the hyphens
that's it if you know this you know how
to write yaml files of course it will
take some time when the complexity of
the yaml increases but this is the basic
structure
you can also use any online yaml linter
so I've have already written this if I
just do the linting it says yes it's a
valid yaml whatever you have written
abishek now that we understood about
yaml files let's learn about anible
Playbook
structure so anible Playbook is
basically a yaml file
and just like any yl file even anable
Playbook starts with three
hyph and a Playbook is list of
plays what does that mean Playbook is a
combination of plays or a list of plays
so each play that you write in your
anable playbook just give a hyphen and
write write a play again give a hyphen
WR a play but abishek what goes into
this place very simple let's say your
task is to set up a DB server as well as
set up a App
server so you can write the DB server
installation and configuration in one
play app server installation and
configur
in a different play so this will be your
DB server play and this will be your app
server play we have this example in the
anable documentation as well if you go
to start writing anible
playbooks no so the same example start
with three hyphens so this is web server
play you can see it is started with a
hyphen and this is TB server play which
again hyphen and this is your complete
play now abishek what goes into the
place okay you explained about Playbook
now what goes into the
play a play has first the
host host section which explains against
which host should this play execute just
like in ad hoc command you provide list
of host right similarly in a play first
you start with explaining the list of
host against which it should run second
you provide the remote user with which
this play has to
execute do you want to execute this play
using a root user Ubunto user whatever
user that you would like to then you
start writing the
task that's it task is basically a again
right within the DB server itself your
first task can be installation of DB
second start can be starting the DB
service if you go here you will find the
same
thing your first task within the task is
to install post gray second is to start
the post gr service right
so each Playbook understand it carefully
anible Playbook is basically a yaml
file within the yaml file you start with
three hyphens and inside the Playbook
you can have n number of plays One play
two plays three Place how do you decide
depending upon your task you can split
your task into multiple place you can
definitely write both of them as well in
a single play but they have clear
separation so better put them this as a
play and this as a play and within each
play you need to provide against what
Host this play has to execute using
which user what are the tasks within
that play if you want you can put
variables as well but we will learn
about that in
future now coming to tasks abishek what
do you put in the task just like in a ad
hoc command what were you doing in a ADH
command you were using
modules right we were using shell module
we were using Y module apt module
similarly even within the task we will
use
modules okay so if we try to put that in
a simple way one more time at the head
level you have
playbooks playbooks is basically
list of
place right it is a list of place and
within each play so if you assume this
in Playbook you have play one and play
two within each play you have things
like host is the first one that you
declare then you declare what is the
remote uncore
user then you declare
task again task is basically a list and
in each task you will use a anible
module what is a module module is the
one that is actually performing
execution okay till now the place host
task you know they are not doing the
execution but if you want to install DB
in that task you have to use the module
that can perform the installation of
your
DB if you want to start your DB service
then in that task you have to use the
module that can start your DB service so
modules are the one that are actually
performing execution of your actions
using anible so if you refer to this
example here if you see to install
postgress the module that we were using
is anible built-in module y to start the
postgress service we were using anible
built-in module called service I'm going
to explain about the built-in modules
custom modules and collections in next
two minutes but before that let's spend
good amount of time understanding the
structure of Playbook because once you
understand it you can write any kind of
Playbook
book I'm repeating again using this
example or let's take the example that
we have in the day3
folder so what we are going to do is in
this example I'm going to install a
Apache service and I'm going to deploy a
static application onto the Apache
service so I have two tasks right
starting with my
Playbook I will say three hyphens then
in this case I just have one play that
is to install this web server and deploy
the application they are not two
different things they are only one thing
that belongs to my
application right both of them are
related to my web server itself so I put
them in a single play if at all I have
something related to database or I have
something related to any other
application I will create another player
but only because I need one player I'll
start with host I'll say host as
all become true means run with root user
if I'm not providing this then what
happens is my anable Play will run with
the user that I have configured in this
case it will be UB to because in the
last class we configured the uban to
user
but of course to perform activities like
uh apt install or to perform uh you know
installation of web server I cannot do
it with UB to user so I'm using this
syntax called become true whenever I put
this in my play that play will run as a
root
user perfect then I have multiple tasks
here task one is to install the Apache
server task two is to just take take my
HTML file and put that in the bar www
HTML location abishek how do you know
that within this task this is the module
to start Apachi server and how do you
know to copy file because I'm not
writing code I'm only using yaml file
how do you know to copy the file this is
the module and this is the Syntax for
the module very simple again if you go
to the anable
documentation click on learn about
modules there are just 20 to 30 buil-in
modules that we use on a day-to-day
basis throughout this series also I'm
going to use those built-in modules as
we keep using we will learn them but if
you go to this particular
section just uh scroll down you will see
uh a section called collection index
click on that and here you you have
collections in anible namespace click on
that and you will see this one called
anible
builtin so just go to that answer will
builtin and these are the modules that
we have so these are the built-in
modules depending upon our activity we
are going to pick the module from here
you might be thinking abishek there are
so many modules here do I need to
remember everything definitely not
required
very simple according to your task so
what is my task here I'm going to show
you how will I write this yaml file in
next two to three minutes it will not
take anything more than that what we
will do is first I want to install a
Apache server and that is on Ubunto
right so this is my first task how will
I usually install using app so just go
to that page
search for
apt so I have a module called apt module
click on that and directly you can see
the syntax to install the Apache server
or you can just replace anything with
Apachi here right if you want to install
let's say python or if you want to
install nodejs or if you want to install
any particular thing if it is available
on that machine if it can be installed
using appt you just have to copy paste
this right so anible buil-in appt name
State basically you provide the name of
the package that you want to install
State means if it is present it will
install if you say the state as absent
then it will delete the existing package
or the service on that particular UB to
install basically present absent means
install and uninstall
now you might be wondering but abishek
what exactly is this buil-in I'll come
back to that there are built-in modules
and custom modules but before that this
is our first task and now going back to
the second task what is my second task
once I have my Apachi installed the
second thing that I have to do is I need
to take a HTML page which I have here
okay this is my
index.html I just want to take this HTML
file and I want to deploy it onto the
web server that I just created so what
do I need to do I need to take HTML file
on my laptop and copy it onto the ec2
instance to a particular location so the
module that I need is
copy just again go to the buil-in
functions search for copy so it says
copy module copy files to remote
locations exactly what I need just click
on that
and again you can search for the syntax
of
it see this is exactly what I've done
I've taken this what is my source source
is just the name of the file because the
file is in the same location as my
anable playbook right they both are in
the same folder first Playbook yaml and
index.html they're in the same location
so
Source will be the name of the file
destination where www HTML because that
is where I have to put my file owner I
can create as root group I can put as
root and I can keep the same file
permissions so that is exactly what I've
done
here so source is index.html tation V
wwwh HTML root root and
64 now let's take this file and let's
see if it will do the job for us right
so I'll take my
terminal I already have the same file on
my terminal in last class we configured
the inventory file already so what do we
have in the inventory
file so I have one instance and two
instance doesn't matter how do i group
because I'm going to run
on all the machines so now I'll say
anible in adhoc command we were using
something called anible but now we will
use anible hyphen
Playbook minus I what is my inventory
file location right just replaced anible
adhoc command with anible Playbook minus
I inventory. inii then provide the name
of your file first Playbook do
yaml if you look at the Playbook do yaml
in the play I have mentioned all so it
will be executed against all the host in
my inventory file okay now let's run and
see the first task that every anable
play will run is gather facts what is
gather facts basically it will see if it
can connect to that ec2 instance or not
and get all the required information
from it okay now if we go to the E2
instance let's see if in this easy to
instance manage node
one let's connect to
it so we will see if the Apache service
is installed and if the file is put in V
www HTML
folder first let's do sud sudo PS hyphen
EF grab Apachi
2 okay uh we don't find that is there
any typo
I actually have the state as absent
right so what has
happened you know it tried to uninstall
of course that is not present so it did
not install but the second task would
have been successfully created first
I've changed this to present I'm not
executing it but I will just show you if
I go to
Pudo
LS
where www HTML you can see the
index.html file but this particular
thing the installation of Apache server
was not successful because the state was
absent I have changed it and I will run
it one more time
again the first task that we'll execute
is gathering facts Gathering facts is
nothing but it was trying to see if it
can connect to those E2
instances install Apachi
httpd SE it clearly said changed that
means the task is executed it said okay
okay that means it is already there so
the summary is that overall on each of
the instance it executed three tasks one
is gather fat one is install Apachi one
is copy the file one got
changed because the other was already
there the file was already there so only
one task got executed now let's see
running the same
command see I have the Apachi service
here the process is running and if I
just do Pudo system
CTL
status Apachi uh 2 see Apachi 2 is also
running now all that I need to do is
again go back to the
instance this is my
instance right and in this instance I'll
enable the security group because I need
to allow Ingress traffic on Port
80
otherwise I cannot access that
application which I have deployed I want
to see if it works edit inbone traffic
rules add rule just say HTTP coming from
anywhere save
rules okay
now let's go back to the ec2
instance take that instance public IP
copy paste
it enter
okay something went wrong here yeah so
I'm learning anible with abishek vamala
this is exactly what I have in the
index.html so this is how you execute
your anible playbooks at the first site
because you are doing it for the first
time you might find it very complicated
you might be thinking abishek what are
these tasks how do I learn these modules
but as we keep going we have 14 days we
will cover different modules and
whenever I'm writing the module I will
refer to the documentation and I will
show you how I'm picking the examples
from the documentation and you will
understand how to write complicated
anable playbooks thank you so much see
you all in the next video take care
bye-bye
Voir Plus de Vidéos Connexes
5.0 / 5 (0 votes)