Day-03 | Write Your First Ansible Playbook | For Absolute Beginners

Abhishek.Veeramalla
30 May 202432:53

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

00:00

πŸ“š 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.

05:02

πŸ“˜ 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.

10:05

πŸ“™ 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.

15:08

πŸ› οΈ 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.

20:10

πŸ”§ 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.

25:12

πŸ–₯️ 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

Ansible is an open-source automation tool used for configuration management, application deployment, and task automation. It is central to the video's theme as the entire script revolves around teaching viewers how to use Ansible to automate various IT tasks. The script discusses different aspects of Ansible, such as Ansible ad hoc commands and Ansible Playbooks, which are used for executing more complex automation tasks.

πŸ’‘YAML

YAML, which stands for 'YAML Ain't Markup Language', is a human-readable data serialization language used for configuration files and networking protocols. In the context of the video, YAML is the language used to write Ansible Playbooks, which are scripts that define a series of tasks to be executed on managed nodes. The script provides a detailed explanation of YAML syntax and its importance in structuring data for automation.

πŸ’‘Playbooks

In Ansible, a Playbook is a file that contains a sequence of tasks to be executed on a group of managed nodes. The script introduces the concept of Playbooks and explains how they are structured using YAML. The video aims to teach viewers how to write their first Playbook to set up a web server and deploy a static application.

πŸ’‘Modules

Modules in Ansible are reusable units of code that perform a specific function, such as installing software or starting a service. The script discusses the use of modules within tasks in an Ansible Playbook, providing examples like the 'apt' module for package management and the 'copy' module for file transfer.

πŸ’‘Tasks

A task in an Ansible Playbook is a unit of work that is executed by a module. The script explains that each play in a Playbook can contain one or more tasks, which are defined by the user to achieve a particular goal, such as installing an Apache server or copying a file to a specific location on a remote machine.

πŸ’‘Hosts

In the context of the script, hosts refer to the remote machines on which the Ansible Playbook's tasks are to be executed. The video explains how to specify the hosts in a Playbook and gives an example where the Playbook is run against all hosts in an inventory file.

πŸ’‘Inventory

An inventory in Ansible is a list of all the hosts that the Playbook will be run against. The script mentions an inventory file that contains the list of instances (hosts) and shows how the Playbook uses this inventory to determine where to execute its tasks.

πŸ’‘EC2 Instance

An EC2 (Elastic Compute Cloud) instance is a virtual machine in the Amazon Web Services (AWS) cloud. The script uses EC2 instances as the target hosts for deploying a web server and a static application using Ansible. It demonstrates how to interact with these cloud-based hosts within an Ansible Playbook.

πŸ’‘Gather Facts

Gather Facts is an Ansible feature that collects information about remote hosts, such as OS type, network settings, and hardware specifications. The script mentions that every Ansible Play runs 'Gather Facts' as the first task to ensure it can connect to and retrieve necessary details about the target hosts.

πŸ’‘Ad-hoc Commands

Ad-hoc commands in Ansible are simple, one-time tasks that are executed on a group of hosts. The script contrasts ad-hoc commands with Playbooks, explaining that while ad-hoc commands are useful for quick tasks, Playbooks are more suitable for complex and multi-step automation.

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

play00:00

hello everyone my name is abishek and

play00:02

welcome back to my Channel today is

play00:05

episode three of anible Zero to Hero

play00:09

series we have already completed the

play00:13

first two episodes in this series where

play00:16

in day one we learned about the

play00:19

introduction to anible and getting

play00:22

started with

play00:23

anible in day two we learned about

play00:27

passwordless

play00:28

authentication in entry file in anible

play00:32

and anible ad hoc

play00:35

commands today is day three where we

play00:39

will jump into the world of anable

play00:42

playbooks before we will understand the

play00:45

basics of yaml because a lot of

play00:49

subscribers were asking about the basics

play00:52

of yaml so we will cover that first and

play00:55

then we will head towards understanding

play00:58

anible Playbook

play01:00

structure in the anible Playbook

play01:03

structure we will cover what are plays

play01:06

in anible what are modules tasks and

play01:11

collections not just that towards the

play01:14

end of this video we will also learn our

play01:18

very first anable Playbook where we will

play01:22

create a web server and deploy static

play01:26

application onto that web server which

play01:28

is hosted on on a ec2

play01:31

instance so please watch this video till

play01:34

the end it's going to be very

play01:37

informative and you are also going to

play01:39

gain practical

play01:41

knowledge before I get started a quick

play01:45

reminder all the notes related to this

play01:48

anible Zero to Hero series will be

play01:51

shared through the GitHub repository for

play01:53

example if you go to the day3 folder you

play01:57

can find the complete notes that I'm

play02:00

going to talk about and also the yaml

play02:04

files and any code that I'm going to use

play02:07

in the demos so you can start this

play02:10

repository Fork it watch it to get the

play02:13

continuous

play02:15

updates now let's move to the Whiteboard

play02:18

and start our first topic for today that

play02:22

is

play02:24

yaml so what exactly is yaml

play02:30

the textbook definition says yaml is a

play02:35

human

play02:36

readable data

play02:39

serialization

play02:41

language what does that

play02:44

mean let's say you want to write a

play02:48

python

play02:49

application and this python application

play02:53

needs an input which can be as simple as

play02:58

list of

play03:00

users or list of students in a class so

play03:04

basically you want to pass some data as

play03:09

an input to your python

play03:12

application obviously this data can be

play03:16

fed to the python application in

play03:18

different ways one is you can pass this

play03:22

data as input using a text file you can

play03:27

pass this data as input using Json or

play03:32

our topic for today that is

play03:36

yaml now your question can be abishek

play03:39

what's the problem with text file you

play03:41

know if I'm passing this information

play03:43

through a text file or data through a

play03:45

text file I don't need to learn any

play03:48

other languages such as Json or

play03:52

yaml so text file is not recommended

play03:55

especially if the data is simple it's

play03:57

still fine but imagine if the data is

play04:00

complex and has thousands of line text

play04:03

file is not recommended because it's not

play04:07

a proper format or there is no standard

play04:12

structure where data has to be put in a

play04:16

text file example maybe if I'm writing

play04:20

this text file I can put list of

play04:24

students name where each student name in

play04:27

a new line so someone else who is

play04:30

writing this text file they might put

play04:33

two students name in a single line or

play04:36

someone else who is writing this text

play04:38

file they might put first student name

play04:41

in the first line and maybe give some

play04:45

spaces and second student name in the

play04:48

third line third student name maybe in

play04:50

the Fifth Line so the problem if you are

play04:56

asking user to put the data in a text

play04:58

file and pass it to your application the

play05:02

data can be presented in different

play05:05

formats why because there is no

play05:07

standardization or a set requirement

play05:10

that text file has to be written in this

play05:12

format only otherwise your python

play05:15

application can throw a syntax error

play05:17

that's not possible because there is no

play05:20

syntax or semantics to a text file this

play05:24

is where templating languages such as

play05:27

Json or yaml comes into the picture so

play05:32

they are standard templating language

play05:37

files that

play05:39

are agreed accepted and defined

play05:43

according to the standards so if you

play05:45

don't write or if you don't put data

play05:48

into the Json file as per the

play05:50

requirement of the Json file

play05:54

then you will get syntax errors that

play05:57

means everyone who is writing a Json

play06:00

file or everyone who is writing a yaml

play06:03

file they need to follow a specific

play06:06

standard that's why you will see that in

play06:09

devops when you are passing some

play06:12

information either to kubernetes or to

play06:16

anible and many other applications you

play06:19

use standard templating language such as

play06:23

yaml where data is structured and

play06:26

available in a serialized way you might

play06:29

also ask but abishek why not Json and

play06:33

why yaml the answer is simple yaml is

play06:37

much more human readable and you don't

play06:40

need to learn it's not very difficult to

play06:43

learn yaml it's very easy to get started

play06:46

with and also very much human readable

play06:50

you will understand once I explain the

play06:53

structure of yaml file so now I hope you

play06:56

understood why usually data is

play06:59

represented and passed as input either

play07:03

in Json files or yaml files and not in

play07:06

text files now the next thing is how to

play07:10

write yaml it's very very simple even if

play07:14

you follow the document that I have in

play07:16

this repo you can learn yaml in exactly

play07:20

10 minutes all that you need to know is

play07:23

four things first is you need to know

play07:27

how to write strings numbers and

play07:30

booleans in yaml all three of them have

play07:34

same syntax just provide the name column

play07:38

space and value even for integer just

play07:41

provide the name of the integer colum

play07:44

space and the value name of the Boolean

play07:47

colum space

play07:49

value then you need to know when to use

play07:52

a list and what is the Syntax for list

play07:55

when to use dictionary that is key value

play07:58

pair and what what is the Syntax for it

play08:02

finally list of dictionaries which is of

play08:05

course covered if you learn both lists

play08:08

and

play08:09

dictionaries if you know four of these

play08:11

things then you know yaml and how to

play08:15

write yaml let's take an example abishek

play08:20

let's write a yaml file where I will

play08:23

write about myself in a yaml file okay

play08:26

the task is to just write about myself

play08:29

in a yaml file so first I will put the

play08:34

name of the yaml

play08:35

file as abishek do

play08:41

yaml every time you start writing a yaml

play08:44

file so assume using whim I have created

play08:47

this file name and what I'm going to

play08:49

write in this file first line you always

play08:52

have to provide three

play08:55

hyphens this will tell that this is a

play08:58

yaml file so you all always have to put

play09:00

three

play09:01

hyphens then let's say I'm going to

play09:04

write about myself the first thing that

play09:05

I'll write is what is my

play09:09

name okay which is obviously a string so

play09:13

column space

play09:18

abishek if you want you can put that in

play09:20

single quote or double quote then I will

play09:24

say the age which is an integer but the

play09:28

syntax is same column space and here I'm

play09:32

going to say let's say some H so

play09:36

whenever you're writing string you can

play09:38

just put it in single quote or double

play09:39

quote when it comes to age which is a

play09:41

number you can just call in space and

play09:44

put it then let's say if I'm working or

play09:50

not which is a bullion right so bullan

play09:54

should be true or false but the syntax

play09:56

is same so if you just know how to write

play09:58

string you know integer as well as

play10:01

bullan as well then let's say what are

play10:04

my

play10:07

tasks so my task is not a single task I

play10:10

have list of tasks so what I will do is

play10:15

I'll click on enter and make sure you

play10:18

leave two characters don't put anything

play10:20

for the first two characters from the

play10:22

third character just put hyphen space

play10:25

and start writing about your task so my

play10:29

first first task is let's say YouTube

play10:32

content creation again click on enter

play10:35

leave two spaces then from the third

play10:38

hyphen

play10:40

work again third and let's say what is

play10:44

my next task take care of

play10:47

home let's say I have another task to do

play10:51

some exercise every day so this is how I

play10:55

will write a list for string

play10:59

number and or integer and Boolean the

play11:02

syntax is same when it comes to list

play11:05

first put the name of the list then

play11:07

leave two spaces from third give a

play11:10

hyphen and write the

play11:12

list the list of your task then let's

play11:16

say I want to put my

play11:18

address address in address I have

play11:22

multiple key value pairs again leave two

play11:26

spaces from the third what is my first

play11:29

key value pair

play11:33

Street okay what is the value I'll put

play11:35

some Street then

play11:38

City then

play11:40

State similarly pin code country so each

play11:45

of these things is a key value pair

play11:46

street is a key value is something city

play11:49

is a key value is something state is a

play11:50

key value is something so the only

play11:53

difference between list syntax and the

play11:57

key value pair syntax is when you are WR

play11:59

writing list what you will do is for

play12:01

each item in the list you will use a

play12:04

hyphen whereas for each item in a

play12:07

dictionary you will not use this

play12:10

particular hyphen because they are key

play12:11

value pairs so you will just declare

play12:14

directly you'll not use the hyphens

play12:16

that's it if you know this you know how

play12:19

to write yaml files of course it will

play12:22

take some time when the complexity of

play12:25

the yaml increases but this is the basic

play12:28

structure

play12:29

you can also use any online yaml linter

play12:33

so I've have already written this if I

play12:36

just do the linting it says yes it's a

play12:40

valid yaml whatever you have written

play12:44

abishek now that we understood about

play12:47

yaml files let's learn about anible

play12:51

Playbook

play12:53

structure so anible Playbook is

play12:56

basically a yaml file

play13:00

and just like any yl file even anable

play13:04

Playbook starts with three

play13:08

hyph and a Playbook is list of

play13:16

plays what does that mean Playbook is a

play13:20

combination of plays or a list of plays

play13:22

so each play that you write in your

play13:25

anable playbook just give a hyphen and

play13:28

write write a play again give a hyphen

play13:32

WR a play but abishek what goes into

play13:35

this place very simple let's say your

play13:39

task is to set up a DB server as well as

play13:47

set up a App

play13:50

server so you can write the DB server

play13:53

installation and configuration in one

play13:56

play app server installation and

play13:58

configur

play13:59

in a different play so this will be your

play14:02

DB server play and this will be your app

play14:05

server play we have this example in the

play14:09

anable documentation as well if you go

play14:11

to start writing anible

play14:15

playbooks no so the same example start

play14:19

with three hyphens so this is web server

play14:22

play you can see it is started with a

play14:24

hyphen and this is TB server play which

play14:28

again hyphen and this is your complete

play14:31

play now abishek what goes into the

play14:34

place okay you explained about Playbook

play14:38

now what goes into the

play14:41

play a play has first the

play14:47

host host section which explains against

play14:51

which host should this play execute just

play14:55

like in ad hoc command you provide list

play14:58

of host right similarly in a play first

play15:01

you start with explaining the list of

play15:03

host against which it should run second

play15:07

you provide the remote user with which

play15:12

this play has to

play15:13

execute do you want to execute this play

play15:16

using a root user Ubunto user whatever

play15:20

user that you would like to then you

play15:23

start writing the

play15:25

task that's it task is basically a again

play15:30

right within the DB server itself your

play15:33

first task can be installation of DB

play15:35

second start can be starting the DB

play15:38

service if you go here you will find the

play15:40

same

play15:41

thing your first task within the task is

play15:45

to install post gray second is to start

play15:50

the post gr service right

play15:55

so each Playbook understand it carefully

play15:59

anible Playbook is basically a yaml

play16:03

file within the yaml file you start with

play16:07

three hyphens and inside the Playbook

play16:11

you can have n number of plays One play

play16:14

two plays three Place how do you decide

play16:17

depending upon your task you can split

play16:20

your task into multiple place you can

play16:22

definitely write both of them as well in

play16:25

a single play but they have clear

play16:27

separation so better put them this as a

play16:30

play and this as a play and within each

play16:34

play you need to provide against what

play16:37

Host this play has to execute using

play16:40

which user what are the tasks within

play16:43

that play if you want you can put

play16:45

variables as well but we will learn

play16:47

about that in

play16:49

future now coming to tasks abishek what

play16:53

do you put in the task just like in a ad

play16:56

hoc command what were you doing in a ADH

play16:59

command you were using

play17:02

modules right we were using shell module

play17:04

we were using Y module apt module

play17:07

similarly even within the task we will

play17:10

use

play17:13

modules okay so if we try to put that in

play17:18

a simple way one more time at the head

play17:22

level you have

play17:26

playbooks playbooks is basically

play17:29

list of

play17:31

place right it is a list of place and

play17:36

within each play so if you assume this

play17:40

in Playbook you have play one and play

play17:42

two within each play you have things

play17:46

like host is the first one that you

play17:49

declare then you declare what is the

play17:53

remote uncore

play17:56

user then you declare

play17:59

task again task is basically a list and

play18:04

in each task you will use a anible

play18:11

module what is a module module is the

play18:15

one that is actually performing

play18:18

execution okay till now the place host

play18:21

task you know they are not doing the

play18:23

execution but if you want to install DB

play18:29

in that task you have to use the module

play18:32

that can perform the installation of

play18:34

your

play18:36

DB if you want to start your DB service

play18:40

then in that task you have to use the

play18:42

module that can start your DB service so

play18:45

modules are the one that are actually

play18:48

performing execution of your actions

play18:52

using anible so if you refer to this

play18:56

example here if you see to install

play18:59

postgress the module that we were using

play19:02

is anible built-in module y to start the

play19:07

postgress service we were using anible

play19:10

built-in module called service I'm going

play19:13

to explain about the built-in modules

play19:16

custom modules and collections in next

play19:18

two minutes but before that let's spend

play19:21

good amount of time understanding the

play19:23

structure of Playbook because once you

play19:26

understand it you can write any kind of

play19:28

Playbook

play19:29

book I'm repeating again using this

play19:32

example or let's take the example that

play19:35

we have in the day3

play19:39

folder so what we are going to do is in

play19:43

this example I'm going to install a

play19:47

Apache service and I'm going to deploy a

play19:51

static application onto the Apache

play19:54

service so I have two tasks right

play19:57

starting with my

play19:59

Playbook I will say three hyphens then

play20:02

in this case I just have one play that

play20:05

is to install this web server and deploy

play20:10

the application they are not two

play20:11

different things they are only one thing

play20:13

that belongs to my

play20:15

application right both of them are

play20:18

related to my web server itself so I put

play20:21

them in a single play if at all I have

play20:24

something related to database or I have

play20:26

something related to any other

play20:28

application I will create another player

play20:31

but only because I need one player I'll

play20:35

start with host I'll say host as

play20:38

all become true means run with root user

play20:42

if I'm not providing this then what

play20:46

happens is my anable Play will run with

play20:51

the user that I have configured in this

play20:53

case it will be UB to because in the

play20:55

last class we configured the uban to

play20:58

user

play20:59

but of course to perform activities like

play21:02

uh apt install or to perform uh you know

play21:05

installation of web server I cannot do

play21:07

it with UB to user so I'm using this

play21:09

syntax called become true whenever I put

play21:12

this in my play that play will run as a

play21:15

root

play21:17

user perfect then I have multiple tasks

play21:20

here task one is to install the Apache

play21:25

server task two is to just take take my

play21:28

HTML file and put that in the bar www

play21:33

HTML location abishek how do you know

play21:38

that within this task this is the module

play21:42

to start Apachi server and how do you

play21:45

know to copy file because I'm not

play21:48

writing code I'm only using yaml file

play21:50

how do you know to copy the file this is

play21:53

the module and this is the Syntax for

play21:55

the module very simple again if you go

play21:59

to the anable

play22:00

documentation click on learn about

play22:03

modules there are just 20 to 30 buil-in

play22:07

modules that we use on a day-to-day

play22:09

basis throughout this series also I'm

play22:12

going to use those built-in modules as

play22:14

we keep using we will learn them but if

play22:18

you go to this particular

play22:20

section just uh scroll down you will see

play22:23

uh a section called collection index

play22:26

click on that and here you you have

play22:29

collections in anible namespace click on

play22:32

that and you will see this one called

play22:35

anible

play22:37

builtin so just go to that answer will

play22:41

builtin and these are the modules that

play22:43

we have so these are the built-in

play22:46

modules depending upon our activity we

play22:49

are going to pick the module from here

play22:52

you might be thinking abishek there are

play22:53

so many modules here do I need to

play22:55

remember everything definitely not

play22:58

required

play22:59

very simple according to your task so

play23:02

what is my task here I'm going to show

play23:04

you how will I write this yaml file in

play23:07

next two to three minutes it will not

play23:09

take anything more than that what we

play23:11

will do is first I want to install a

play23:16

Apache server and that is on Ubunto

play23:19

right so this is my first task how will

play23:22

I usually install using app so just go

play23:26

to that page

play23:28

search for

play23:30

apt so I have a module called apt module

play23:33

click on that and directly you can see

play23:38

the syntax to install the Apache server

play23:41

or you can just replace anything with

play23:45

Apachi here right if you want to install

play23:47

let's say python or if you want to

play23:49

install nodejs or if you want to install

play23:51

any particular thing if it is available

play23:54

on that machine if it can be installed

play23:56

using appt you just have to copy paste

play23:59

this right so anible buil-in appt name

play24:06

State basically you provide the name of

play24:09

the package that you want to install

play24:11

State means if it is present it will

play24:14

install if you say the state as absent

play24:17

then it will delete the existing package

play24:20

or the service on that particular UB to

play24:23

install basically present absent means

play24:26

install and uninstall

play24:29

now you might be wondering but abishek

play24:31

what exactly is this buil-in I'll come

play24:34

back to that there are built-in modules

play24:36

and custom modules but before that this

play24:39

is our first task and now going back to

play24:41

the second task what is my second task

play24:44

once I have my Apachi installed the

play24:48

second thing that I have to do is I need

play24:50

to take a HTML page which I have here

play24:55

okay this is my

play24:56

index.html I just want to take this HTML

play24:59

file and I want to deploy it onto the

play25:02

web server that I just created so what

play25:05

do I need to do I need to take HTML file

play25:08

on my laptop and copy it onto the ec2

play25:11

instance to a particular location so the

play25:14

module that I need is

play25:17

copy just again go to the buil-in

play25:19

functions search for copy so it says

play25:23

copy module copy files to remote

play25:25

locations exactly what I need just click

play25:28

on that

play25:30

and again you can search for the syntax

play25:32

of

play25:34

it see this is exactly what I've done

play25:37

I've taken this what is my source source

play25:41

is just the name of the file because the

play25:43

file is in the same location as my

play25:45

anable playbook right they both are in

play25:48

the same folder first Playbook yaml and

play25:51

index.html they're in the same location

play25:54

so

play25:58

Source will be the name of the file

play26:01

destination where www HTML because that

play26:05

is where I have to put my file owner I

play26:08

can create as root group I can put as

play26:11

root and I can keep the same file

play26:13

permissions so that is exactly what I've

play26:16

done

play26:18

here so source is index.html tation V

play26:23

wwwh HTML root root and

play26:27

64 now let's take this file and let's

play26:30

see if it will do the job for us right

play26:35

so I'll take my

play26:37

terminal I already have the same file on

play26:40

my terminal in last class we configured

play26:43

the inventory file already so what do we

play26:46

have in the inventory

play26:50

file so I have one instance and two

play26:53

instance doesn't matter how do i group

play26:56

because I'm going to run

play26:59

on all the machines so now I'll say

play27:03

anible in adhoc command we were using

play27:07

something called anible but now we will

play27:09

use anible hyphen

play27:13

Playbook minus I what is my inventory

play27:16

file location right just replaced anible

play27:20

adhoc command with anible Playbook minus

play27:22

I inventory. inii then provide the name

play27:26

of your file first Playbook do

play27:30

yaml if you look at the Playbook do yaml

play27:33

in the play I have mentioned all so it

play27:36

will be executed against all the host in

play27:39

my inventory file okay now let's run and

play27:44

see the first task that every anable

play27:47

play will run is gather facts what is

play27:51

gather facts basically it will see if it

play27:54

can connect to that ec2 instance or not

play27:56

and get all the required information

play27:59

from it okay now if we go to the E2

play28:05

instance let's see if in this easy to

play28:09

instance manage node

play28:12

one let's connect to

play28:20

it so we will see if the Apache service

play28:25

is installed and if the file is put in V

play28:28

www HTML

play28:32

folder first let's do sud sudo PS hyphen

play28:38

EF grab Apachi

play28:44

2 okay uh we don't find that is there

play28:48

any typo

play29:10

I actually have the state as absent

play29:13

right so what has

play29:17

happened you know it tried to uninstall

play29:20

of course that is not present so it did

play29:23

not install but the second task would

play29:26

have been successfully created first

play29:28

I've changed this to present I'm not

play29:30

executing it but I will just show you if

play29:33

I go to

play29:36

Pudo

play29:38

LS

play29:39

where www HTML you can see the

play29:43

index.html file but this particular

play29:47

thing the installation of Apache server

play29:49

was not successful because the state was

play29:52

absent I have changed it and I will run

play29:54

it one more time

play29:58

again the first task that we'll execute

play30:00

is gathering facts Gathering facts is

play30:02

nothing but it was trying to see if it

play30:04

can connect to those E2

play30:07

instances install Apachi

play30:10

httpd SE it clearly said changed that

play30:14

means the task is executed it said okay

play30:17

okay that means it is already there so

play30:20

the summary is that overall on each of

play30:24

the instance it executed three tasks one

play30:27

is gather fat one is install Apachi one

play30:30

is copy the file one got

play30:33

changed because the other was already

play30:36

there the file was already there so only

play30:38

one task got executed now let's see

play30:41

running the same

play30:43

command see I have the Apachi service

play30:46

here the process is running and if I

play30:48

just do Pudo system

play30:51

CTL

play30:54

status Apachi uh 2 see Apachi 2 is also

play31:00

running now all that I need to do is

play31:03

again go back to the

play31:06

instance this is my

play31:10

instance right and in this instance I'll

play31:13

enable the security group because I need

play31:15

to allow Ingress traffic on Port

play31:19

80

play31:21

otherwise I cannot access that

play31:23

application which I have deployed I want

play31:25

to see if it works edit inbone traffic

play31:28

rules add rule just say HTTP coming from

play31:36

anywhere save

play31:38

rules okay

play31:41

now let's go back to the ec2

play31:47

instance take that instance public IP

play31:51

copy paste

play31:53

it enter

play32:01

okay something went wrong here yeah so

play32:04

I'm learning anible with abishek vamala

play32:07

this is exactly what I have in the

play32:11

index.html so this is how you execute

play32:14

your anible playbooks at the first site

play32:17

because you are doing it for the first

play32:19

time you might find it very complicated

play32:21

you might be thinking abishek what are

play32:23

these tasks how do I learn these modules

play32:26

but as we keep going we have 14 days we

play32:29

will cover different modules and

play32:32

whenever I'm writing the module I will

play32:34

refer to the documentation and I will

play32:36

show you how I'm picking the examples

play32:38

from the documentation and you will

play32:41

understand how to write complicated

play32:43

anable playbooks thank you so much see

play32:45

you all in the next video take care

play32:47

bye-bye

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
AnsiblePlaybooksYAMLDevOpsAutomationEducationZero to HeroWeb ServerEC2 InstanceTutorial