Day-15 | Ansible Zero to Hero | #ansible #devops

Abhishek.Veeramalla
25 Jan 202355:51

Summary

TLDRIn this comprehensive DevOps tutorial, Abhishek guides viewers through the practical application of Ansible, a powerful IT automation tool. Starting with the basics, he demonstrates how to install Ansible on an EC2 instance and set up passwordless SSH for seamless server communication. The video progresses to executing ad-hoc commands and crafting Ansible playbooks in YAML format, including tasks like installing and starting Nginx. Abhishek also introduces the concept of Ansible roles to streamline complex playbooks, showcasing the structured approach to automation, ideal for configuring systems like Kubernetes clusters.

Takeaways

  • πŸ“˜ Day 15 of the complete DevOps course focuses on practical knowledge of Ansible, following a theoretical discussion from Day 14.
  • πŸ› οΈ Ansible is installed initially on an EC2 instance, with recommendations to use a Linux machine for simplicity, although Mac or Windows can also be used.
  • πŸ”‘ The first practical step with Ansible is setting up passwordless authentication using SSH keys to facilitate communication with target servers.
  • πŸ” Ansible uses an inventory file to manage target server addresses, which can be grouped for specific tasks or configurations.
  • πŸ€– Ansible ad-hoc commands are used for executing single or small sets of tasks directly from the command line, as opposed to writing a full playbook.
  • πŸ“ Ansible playbooks are written in YAML format and are used for more complex sets of tasks, such as installing and starting services like nginx.
  • πŸ”§ Playbooks can be executed with the 'ansible-playbook' command, which runs the defined tasks on the specified hosts in the inventory file.
  • πŸ”Ž The 'verbose' option in Ansible commands increases the level of detail in the output, which is helpful for understanding Ansible's internal processes.
  • πŸ“š Ansible roles are introduced for structuring complex playbooks efficiently, sequestering tasks, variables, and other elements into organized files and folders.
  • πŸš€ The 'ansible-galaxy' command is used to initialize new roles, which can then be referenced in playbooks to modularize and simplify complex configurations.
  • πŸ”— The video script references a GitHub repository containing examples of Ansible playbooks and roles for further learning and reference.

Q & A

  • What is the main focus of the video script?

    -The main focus of the video script is to provide a comprehensive guide on installing and using Ansible for DevOps practices, including practical knowledge and hands-on examples.

  • Why does the instructor recommend starting with a Linux machine for learning Ansible?

    -The instructor recommends starting with a Linux machine because things are generally easier when you start doing them on Linux, although it's also possible to use Mac or Windows if a Linux machine is not available.

  • What is the first step in using Ansible as mentioned in the script?

    -The first step in using Ansible is to install it on your machine. The script provides instructions on how to install Ansible using the package manager specific to the operating system, like apt for Ubuntu.

  • What is the purpose of the 'sudo apt update' command in the script?

    -The 'sudo apt update' command is used to update all the packages on the system to their latest versions, which is a recommended step to perform after installing or setting up a new machine.

  • How does the script describe the process of setting up passwordless authentication for Ansible?

    -The script explains setting up passwordless authentication by generating an SSH key pair on the Ansible server using 'ssh-keygen', and then copying the public key to the 'authorized_keys' file on the target server.

  • What is an Ansible Playbook and why is it used?

    -An Ansible Playbook is a file that contains a set of tasks to be executed on target servers. It is used to automate and standardize the process of configuring and managing infrastructure.

  • What is the difference between Ansible ad-hoc commands and Ansible Playbooks as per the script?

    -Ansible ad-hoc commands are used for executing single or a few tasks immediately, while Ansible Playbooks are used for executing a series of tasks, often in a more complex or structured manner.

  • How can multiple servers be managed using Ansible?

    -Multiple servers can be managed by grouping them in the inventory file. The script mentions that Ansible can execute commands or Playbooks on specific groups of servers, such as 'web servers' or 'db servers', as defined in the inventory file.

  • What is the significance of the 'ansible-playbook' command mentioned in the script?

    -The 'ansible-playbook' command is used to execute an Ansible Playbook. It is different from the 'ansible' command, which is used for running ad-hoc commands.

  • Can you explain the concept of 'become' in Ansible as described in the script?

    -The 'become' concept in Ansible allows you to execute tasks with elevated privileges. In the script, 'become root' is used to switch to the root user to perform tasks that require administrative permissions.

  • What is the role of the 'gather_facts' step in an Ansible Playbook execution?

    -The 'gather_facts' step is the first task that gets executed in every Ansible Playbook. It collects information about the target server, such as its operating system, network configuration, and installed packages, which can be used in the playbook.

  • How can one learn more about the internal workings of Ansible?

    -To learn more about the internal workings of Ansible, the script suggests using the verbose option ('-v', '-vv', or '-vvv') with the 'ansible-playbook' command, which provides detailed debug information about the tasks being executed.

  • What is the purpose of the 'ansible-galaxy' command in the script?

    -The 'ansible-galaxy' command is used to initialize and manage Ansible roles. In the script, it is used to create a new role for Kubernetes, which helps in organizing and structuring complex playbooks.

  • Can you describe the structure of an Ansible role as mentioned in the script?

    -An Ansible role, as described in the script, has a specific structure that includes tasks, handlers, tests, vars, defaults, files, templates, meta, and a README. Each of these components serves a specific purpose in organizing and executing the role's tasks.

  • What are Ansible roles and why are they important for complex playbooks?

    -Ansible roles are a way to organize and structure complex playbooks into reusable and maintainable units. They are important for complex playbooks because they improve efficiency, making it easier to manage tasks, variables, and configurations.

Outlines

00:00

πŸ“š Introduction to DevOps and Ansible

In this segment, Abhishek, the host, welcomes viewers back to his channel and introduces Day 15 of the complete DevOps course. He recaps the previous session on configuration management and the role of Ansible, comparing it with Puppet. The focus of the current session is to provide practical knowledge of Ansible, starting from the basics. Abhishek shares his experience with setting up an EC2 instance on AWS for those without a Linux machine, emphasizing the ease of using Linux for Ansible. He guides through the installation of Ansible using the package manager appropriate for the user's operating system, with a demonstration using an Ubuntu instance. The importance of updating packages and using 'sudo' for root privileges is highlighted. The paragraph concludes with a verification step to ensure Ansible is installed correctly.

05:02

πŸ”‘ Setting Up Passwordless Authentication for Ansible

Abhishek proceeds to explain the necessity of passwordless authentication for Ansible to communicate with target servers effectively. He demonstrates how to generate an SSH key pair using 'SSH-keygen' and emphasizes the importance of keeping the private key secure. The public key is then copied to the 'authorized_keys' file on the target server to enable passwordless SSH access. The process is shown for an Ubuntu machine, but Abhishek mentions that it can be adapted for other systems like CentOS. The paragraph concludes with a successful SSH connection to the target server without a password, illustrating the setup's success.

10:02

πŸ› οΈ Exploring Ansible Ad-Hoc Commands

In this part, Abhishek discusses the use of Ansible ad-hoc commands for executing simple tasks on target servers without writing a full playbook. He explains the concept of an inventory file, which lists target server IP addresses, and how to use the 'ansible' command with the inventory file to run commands like file creation on the target server. The use of modules in Ansible is introduced, with 'shell' as an example for executing shell commands on the target. The paragraph concludes by emphasizing that while playbooks are not always necessary for simple tasks, ad-hoc commands provide a quick way to perform single or a few tasks on remote servers.

15:04

πŸ“ Understanding Ansible Modules and Ad-Hoc Commands

Abhishek continues the discussion on Ansible by explaining the vast array of modules available and how to find the appropriate module for specific tasks. He demonstrates using the 'shell' module to execute commands like 'nproc' and 'df' to gather system information from the target server. The paragraph also covers how to use the 'copy' module to transfer files between servers, highlighting the flexibility of Ansible's ad-hoc commands. Abhishek encourages viewers to explore the Ansible documentation to understand the modules better and to try different commands to gain hands-on experience.

20:06

πŸ–₯️ Grouping Servers in Ansible for Targeted Task Execution

This segment delves into the organization of servers within the Ansible inventory file, allowing for targeted execution of tasks on specific groups, such as 'web servers' or 'DB servers'. Abhishek illustrates how to group servers within the inventory file and execute Ansible commands on these groups. The explanation includes the use of the 'ansible' command with the inventory file and group names to perform operations on selected servers. The paragraph concludes with an interview question preview about grouping servers and executing tasks on specific virtual machines using Ansible.

25:07

πŸš€ Writing the First Ansible Playbook for Installing and Starting Nginx

Abhishek introduces the concept of Ansible Playbooks and demonstrates writing a simple playbook to install and start Nginx on a server. He explains the YAML format used for writing playbooks and outlines the structure, including the playbook header, host specification, 'become' directive for root access, and tasks. Two tasks are defined: one for installing Nginx using the 'apt' module and another for starting the service using the 'service' module. The paragraph concludes with the execution of the playbook and a verification step to check if Nginx is running successfully on the target server.

30:08

πŸ” Using Verbose Option to Understand Ansible Internally

In this part, Abhishek discusses the use of the verbose (-v) option in Ansible to gain a deeper understanding of its internal operations. He explains how the verbose option provides detailed logs, which can be instrumental for debugging and learning the intricacies of Ansible. The example provided walks through the execution of the playbook with increased verbosity, showcasing the step-by-step process that Ansible follows to establish connections, execute tasks, and handle packages on the target server.

35:09

πŸ“š Introduction to Ansible Roles for Efficient Playbook Writing

Abhishek introduces the concept of Ansible roles, explaining their purpose in structuring complex playbooks efficiently. He describes roles as a way to segregate tasks and variables, making playbooks more manageable, especially when dealing with large-scale configurations like Kubernetes clusters. The paragraph includes a demonstration of creating a new role using the 'ansible-galaxy' command and outlines the structure of the role, including various files and folders such as tasks, handlers, templates, and more. The explanation emphasizes the benefits of using roles for organized and scalable Ansible playbooks.

40:11

🌐 Utilizing Ansible Galaxy and GitHub for Role-Based Playbook Management

This segment focuses on the practical use of Ansible Galaxy for initializing roles and GitHub for sharing and managing playbooks. Abhishek demonstrates the process of creating a role for Kubernetes using Ansible Galaxy and discusses the structure of the role with its various components. He then references his GitHub repository, where he shares tested playbooks and roles, and encourages viewers to follow it for updates and examples. The paragraph concludes with an invitation for viewers to explore the repository and use the provided examples as a learning resource.

45:11

πŸ—‚οΈ Dissecting the Components of Ansible Roles

Abhishek provides an in-depth explanation of the components found within an Ansible role, detailing the purpose of each folder and file. He covers the use of 'tasks' for defining actions, 'handlers' for exception management, 'defaults' and 'vars' for variable storage, 'meta' for metadata and licensing information, 'test' for unit testing, and 'templates' for file templating. The explanation aims to clarify the role structure and its components, guiding users on how to leverage roles for efficient playbook organization and execution.

50:11

πŸ“˜ Conclusion and Further Learning Resources on Ansible

In the concluding segment, Abhishek wraps up the video by encouraging viewers to practice the concepts learned, such as Ansible ad-hoc commands and the execution of the demonstrated playbook. He also points to additional resources, including a video on the 18 most asked Ansible interview questions, for further learning. Abhishek invites feedback and questions in the comment section and expresses his gratitude for viewers' engagement, promising to address any queries in future videos.

Mindmap

Keywords

πŸ’‘DevOps

DevOps is a set of practices that combines software development (Dev) and IT operations (Ops) to shorten the systems development life cycle and provide continuous delivery of value to end users. In the video, the theme revolves around a complete DevOps course, emphasizing the importance of DevOps in modern software development and deployment processes.

πŸ’‘Configuration Management

Configuration management is the process of tracking and maintaining information about the software and hardware components of a system throughout its lifecycle. In the script, the instructor discusses the theory of configuration management, highlighting its significance in DevOps and how tools like Ansible play a key role in this domain.

πŸ’‘Ansible

Ansible is an open-source automation tool used for configuration management, application deployment, task automation, and provisioning. The video script mentions Ansible as a key player in configuration management, comparing it with other tools like Puppet, and demonstrating its practical use throughout the course.

πŸ’‘EC2 Instance

An EC2 (Elastic Compute Cloud) instance is a virtual server in the Amazon Web Services (AWS) cloud. The script provides a practical example of creating an EC2 instance for learning Ansible, which is a common practice for setting up a Linux machine for DevOps practices.

πŸ’‘Package Manager

A package manager is a software tool that automates the process of installing, upgrading, and removing software packages. The script mentions package managers like apt for Ubuntu, which are used to install Ansible and other software packages on Linux systems.

πŸ’‘SSH Keygen

SSH keygen is a command used to generate a pair of cryptographic keys used in SSH (Secure Shell) for authenticating to an SSH server. The video script explains how to use SSH keygen to create a public-private key pair for setting up passwordless authentication, which is a prerequisite for using Ansible.

πŸ’‘Ad-hoc Commands

In Ansible, ad-hoc commands are simple commands that can be executed on remote hosts without writing a full playbook. The script introduces ad-hoc commands as a way to perform single tasks, such as creating files on a target server, demonstrating the flexibility of Ansible.

πŸ’‘Playbook

An Ansible playbook is a file that contains a sequence of tasks to be executed on a group of remote servers managed by Ansible. The script describes the process of writing an Ansible playbook in YAML format, which is used for more complex automation tasks, such as installing and starting nginx.

πŸ’‘Inventory File

An inventory file in Ansible is a file that lists the managed nodes (hosts). The script explains the importance of the inventory file in defining the targets for Ansible commands and playbooks, and how to use it for grouping servers for different tasks.

πŸ’‘YAML

YAML (YAML Ain't Markup Language) is a human-readable data serialization standard used in programming languages and software applications. The script mentions YAML as the format used for writing Ansible playbooks, emphasizing its readability and ease of use for configuration files.

πŸ’‘Roles

In the context of Ansible, roles are a way to structure playbooks and are a collection of various files that define tasks, variables, and other elements. The script introduces the concept of roles towards the end, explaining how they can improve efficiency and organization when writing complex playbooks.

Highlights

Introduction to Day 15 of the Complete DevOps course focusing on practical knowledge of Ansible.

Explanation of the importance of configuration management and Ansible's role as a key player.

Comparison between Ansible and Puppet, discussing their differences and use cases.

A step-by-step guide on creating an EC2 instance for beginners to start learning Ansible.

The necessity of installing Ansible on a Linux machine and the process outlined for doing so.

Instructions on updating packages and installing Ansible using the package manager on Ubuntu.

Recommendation to use the system's package manager for easier installation and path configuration.

Verification of Ansible installation using the 'ansible --version' command.

The requirement of having at least two servers for practical Ansible demonstrations.

Demonstration of setting up passwordless authentication between servers for Ansible communication.

Explanation of SSH key generation and the importance of public and private keys in authentication.

Guide on copying the public key to the target server for passwordless SSH access.

Introduction to Ansible ad-hoc commands as an alternative to writing playbooks for simple tasks.

Difference between Ansible ad-hoc commands and playbooks, with use cases for each.

How to execute Ansible ad-hoc commands using the inventory file and module arguments.

The concept of grouping servers in the inventory file for targeted playbook execution.

Writing the first Ansible playbook to install and start Nginx, explained step by step.

Use of YAML format for writing Ansible playbooks and the significance of each section.

Execution of the Ansible playbook and the process of using the 'ansible-playbook' command.

Utilization of the verbose option for increased debug information when running Ansible.

Introduction to Ansible roles for structuring complex playbooks and improving efficiency.

Explanation of the role structure, including tasks, handlers, templates, and other components.

The ansible-galaxy command to initialize new roles for organized and scalable playbooks.

GitHub repository sharing of Ansible examples for learning and practical application.

Transcripts

play00:02

hello everyone my name is Abhishek and

play00:04

welcome back to my channel so today we

play00:06

are at day 15 of our complete devops

play00:08

course so in the previous class that is

play00:10

day 14 we talked about the theory of

play00:12

configuration management uh what is

play00:14

configuration management why ansible has

play00:15

become a key player we try to compare it

play00:18

with puppet and like we talked about

play00:20

various other things as well uh

play00:22

regarding ansible so it was completely a

play00:25

theoretical discussion and today's topic

play00:27

we'll try to cover the Practical

play00:29

knowledge of ansible perfect so let's

play00:31

start from the basics right from the

play00:33

basics uh so what I've done is I just

play00:35

created an ec2 instance uh like it's

play00:38

your choice if you have a Linux machine

play00:40

already you can proceed with it but if

play00:43

you are a first timer let's say you're

play00:45

learning ansible for the very first time

play00:46

I would definitely recommend you to

play00:48

start with uh like you know a Linux

play00:51

machine not Windows or Mac uh the reason

play00:54

for that is you know things will be much

play00:56

easier when you try when you start doing

play00:58

on Linux but uh it's fine uh I mean if

play01:01

you don't have a Linux machine and if

play01:03

you can't create for some reasons a

play01:05

Linux machine on ec2 instance then yeah

play01:06

you can also try with your Mac or

play01:09

Windows

play01:10

Okay so

play01:12

what is the first thing that you need to

play01:14

do so the very first thing that you need

play01:15

to do is to install ansible right

play01:17

definitely to try any tool you need to

play01:20

start with the installation of the tool

play01:22

so let me show you how to install

play01:23

ansible in the very first case

play01:26

so there are a lot of documentations

play01:28

with which talks about installation of

play01:30

ansible right uh like you know you can

play01:33

go to the official ansible docs as well

play01:35

so if you see here this is the official

play01:37

ansible docs you can also install

play01:39

ansible from here

play01:41

but what I would recommend you is

play01:44

depending upon your uh distribution that

play01:47

you have installed like for example if

play01:48

you see here uh this is a Ubuntu machine

play01:50

that I created from AWS like this is

play01:53

easy to instance that I launched so

play01:55

similarly uh like you might have

play01:58

launched a sentence one or any other

play01:59

thing so it comes with a default package

play02:02

manager right so for example in terms of

play02:04

Ubuntu I get apt so what you can

play02:07

typically do is start with sudo apt

play02:10

update okay Sudo is because for running

play02:12

any apt commands you would need uh root

play02:15

privileges so that's why you have to uh

play02:18

prefix it with sudo so sudo apt update

play02:21

okay as you do that your all the

play02:22

packages will be updated so this is

play02:24

definitely this is something that you

play02:26

have to definitely do for the first time

play02:28

as you install your ec2 instance so once

play02:31

you do it okay so I have already done it

play02:33

so it took a slightly less time but uh

play02:36

in your case it might take a little more

play02:38

time don't worry about it once you do it

play02:40

uh you can simply do this sudo apt

play02:44

install

play02:48

okay

play02:49

so this will install ansible for you in

play02:51

my case like I told you it is already

play02:53

installed and it is set to the newest

play02:55

version so app did not do anything but

play02:57

uh in your case once you run this

play02:59

command it would install ansible for you

play03:01

okay isn't it so simple like you can go

play03:04

to the official ansible documentation

play03:05

and try things as well but like I told

play03:08

you sometimes it becomes tricky because

play03:11

this documentation is a very generic one

play03:14

so how does it do it basically installs

play03:16

python for you then it installs pip for

play03:18

you and using pip in it installs ansible

play03:20

so the reason for that is basically they

play03:23

wanted to keep the documentation as

play03:25

generic as possible irrespective of

play03:27

Windows Linux Mac or everything because

play03:29

you are doing it through python uh the

play03:31

installation would work but the easiest

play03:34

way is uh to use your package manager

play03:36

because all your package managers have

play03:39

ansible as your packages so even if

play03:43

you're on Mac you can simply do a brew

play03:45

install uh ansible if you are on Windows

play03:48

you can do chocolate a install Windows

play03:50

so

play03:51

uh definitely go with your own package

play03:54

manager so that it is added by default

play03:56

to the path as well whereas if you are

play03:58

doing using this python pip sometimes

play03:59

you also have to add it to your path

play04:01

okay if your python is configured in a

play04:03

different location

play04:04

okay so we are done with the first

play04:07

things that is to install ansible verify

play04:09

it using this command ansible

play04:11

hyphen hyphen version so as I do ansible

play04:14

hyphen iPhone version you can clearly

play04:15

see that it mentions that ansible is

play04:18

properly installed okay

play04:20

okay make sense so what is the next

play04:23

thing that you have to do the next thing

play04:25

is using ansible once you've I mean you

play04:28

want to start playing with ansible right

play04:29

so to start playing with ansible you

play04:32

would need one more server yeah I mean

play04:34

in the minimum you'll need two servers

play04:36

that is uh this server has ansible and

play04:38

to practically explain okay you need one

play04:41

more server so that you know you can

play04:43

configure the other server using this

play04:45

ansible server right so that's the

play04:47

reason why I have created two servers

play04:49

okay so this is the ansible server that

play04:52

we have currently logged in and this is

play04:53

the target server okay let me increase

play04:55

the font

play04:56

so this is the target server so what

play04:59

I'll do is using this ansible server

play05:02

I'll configure the target server okay so

play05:04

if you see both of them are Ubuntu so

play05:07

that's the reason I named it as Target

play05:08

Ubuntu machine

play05:10

but irrespective of it you can create

play05:12

anything okay it doesn't matter like I

play05:15

mentioned in the previous class as well

play05:16

all that ansible requires is a

play05:18

passwordless authentication so if

play05:20

ansible can authenticate without any

play05:23

password to these machines so ansible

play05:26

can do anything okay it can configure

play05:28

anything on that server okay so I have

play05:31

two servers now firstly I'll show you

play05:33

how to set up passwordless

play05:34

authentication right because as I told

play05:37

you the prerequisite is ansible should

play05:40

be able to communicate this server

play05:42

without any password so for that let's

play05:44

see how to do that okay

play05:47

so uh go to this instance I mean just

play05:49

click on this instance you will get uh

play05:51

the private IP address like you see here

play05:54

so this is the private IP address always

play05:56

talk to the always talk to the private

play05:57

IB address because you know both of them

play05:59

are in the same VPC right and both of

play06:02

both of the instants are in ec2 uh

play06:05

I mean or on AWS so use the private IP

play06:08

address it will be very easy

play06:10

so copy this private IP address and let

play06:12

me show you uh if I do SSH to it okay so

play06:16

let me SSH to the private IP address it

play06:18

will ask for the password because uh I

play06:21

haven't configured any passwordless

play06:22

authentication yet because I want to

play06:24

show it to you from the scratch

play06:26

Okay so

play06:29

if you follow any documentations

play06:31

sometimes people will confuse you

play06:33

because uh like you know there are

play06:35

multiple ways to configure passwordless

play06:36

authentication but the easiest way okay

play06:39

so some documentations might talk about

play06:42

SSH copy ID okay so SSH copy ID is one

play06:45

way of doing it just enter SSH copy ID

play06:48

and provide the IP address of the target

play06:50

server and everything will be set yes

play06:52

SSH copy ID is the easiest one but what

play06:55

happens is sometimes the SSH copy ID

play06:58

does not have enough permissions to do

play07:00

it for you so that's why I will show you

play07:02

a very easy way okay and if you have any

play07:05

questions to this way always uh like you

play07:07

know like I always mention you put your

play07:10

questions in the comment section I'll

play07:11

definitely answer but I'll explain you

play07:13

in a very straightforward way so I'm

play07:15

pretty sure you shouldn't get any

play07:16

questions

play07:18

so on the Ubuntu user what you will do

play07:21

is simply type

play07:23

SSH hyphen Keygen okay what SSS SSH

play07:28

hyphen Keygen will do for you is it will

play07:30

generate a key for you right that's why

play07:32

it's called SSH hyphen keychain what is

play07:34

the key it will generate a public

play07:36

private IP okay so that means it will

play07:38

create a public key and it will create a

play07:40

private key so is it asking uh it is

play07:42

asking is this location fine home Ubuntu

play07:45

dot SSH ID RSA yeah it's okay for me uh

play07:48

it says that you already have something

play07:50

can I overwrite no problem

play07:53

okay in your case you might not have it

play07:55

already Okay then enter so what happens

play07:59

is once you do this there is a set of

play08:02

files that is your public key private

play08:04

key known host key known host file that

play08:07

is stored in this specific location so

play08:09

this directory gets created and there

play08:11

are a bunch of files that are stored in

play08:13

this directory okay so slash home slash

play08:16

Ubuntu dot SSH so how are these things

play08:19

created basically these things are

play08:21

created using the command called SSH

play08:23

hyphen keychain okay so you can enter

play08:26

SSH hyphen keychain on your machine and

play08:28

after that you get ID RSA ID RSA pob

play08:33

so this is your private key and this is

play08:35

your public key so private key is used

play08:37

to log into this machine and private key

play08:40

is something that you can that you

play08:41

should never share with anybody okay so

play08:44

what is that you need to share if

play08:45

somebody says that okay let's

play08:48

communicate with each other always go

play08:50

with the public key so what we will try

play08:51

to do is like I told you we will try to

play08:54

talk to this target server okay from

play08:56

this ansible server in a passwordless

play08:58

way okay so we don't want to

play09:00

authenticate using the password so for

play09:02

that reason what I'll do is I'll take

play09:04

this public key okay cat public key

play09:08

uh sorry I'm not in that directory so uh

play09:12

what I can do cat followed by this

play09:15

location followed by ID underscore

play09:19

RSA

play09:21

dot Pub okay so this is my public key

play09:24

let me copy this public key okay and

play09:27

once I copy this public key what I will

play09:29

do is again login to your second machine

play09:32

okay that is your target server so take

play09:35

a new tab okay and I'm already logged in

play09:37

here let me also increase the font for

play09:39

this one

play09:43

okay I hope the font size is good so

play09:46

this is the uh other I other server or

play09:50

the target server if you see the IP

play09:51

address 172 31 6228 okay now what I will

play09:55

do is

play09:57

just do the SSH keys in here as well

play10:00

okay once you do the SSH keys in here as

play10:02

well okay it will

play10:05

again create the same set of files so if

play10:07

you do LS

play10:09

dot SSH

play10:11

so you will see here that you have

play10:13

authorized keys private key and public

play10:16

key okay makes sense so now what we'll

play10:18

do is just open the authorized

play10:24

okay authorized keys in the authorized

play10:27

case what you need to do is the one that

play10:30

you copied here right so you open the ID

play10:33

RSA Pub copied the content and then

play10:35

paste the content here

play10:38

okay so just paste the content from here

play10:41

to

play10:45

here

play10:47

and just save this file now let me see

play10:50

what happens okay I'll execute the SSH

play10:52

command one more time okay SSH

play10:55

followed by the IP address here okay

play10:58

this is the IP address let me do SSH to

play11:01

the Target server

play11:04

enter

play11:05

awesome what happened I was able to

play11:08

authenticate without any password right

play11:10

if you see here clearly I did not give

play11:13

any password I just said SSH okay up

play11:16

Arrow I just did SSH the IP address of

play11:20

the target server and as I do SSH I am

play11:23

able to connect to the Target server

play11:24

without any password so this is the

play11:27

prerequisite for your ansible right so

play11:29

prerequisite for ansible is you need to

play11:31

have a password

play11:32

passwordless authentication and how did

play11:35

I set up the passwordless authentication

play11:36

in simple terms like if I have to

play11:39

explain it in one single line what I did

play11:41

is I just copied the public IP address

play11:44

of the ansible server to the authorized

play11:48

case of the target servers let's say you

play11:50

will have one more server okay if you

play11:52

have one more server what you will do

play11:53

let's say we have a Centos server okay

play11:56

so again you will follow the same steps

play11:58

like uh now you already have the public

play12:00

key so you don't have to do SSH key gen

play12:02

what you will do is you'll just copy the

play12:04

public key and you will create the

play12:07

centroid server open the centralized

play12:09

server that means SSH to the centralized

play12:10

server in a new terminal and open the

play12:13

authorized keys and paste your public

play12:15

key in the authorized keys of your

play12:16

Centos server then ansible will be able

play12:19

to communicate or the ansible host will

play12:21

be able to communicate to that Target

play12:23

server in a passwordless authentication

play12:25

way

play12:27

awesome I hope that things are very

play12:29

clear till now okay what did we do we

play12:32

first step we installed ansible and

play12:34

second step we set up the passwordless

play12:36

authentication with ansible now we are

play12:38

half done okay so these are the two

play12:40

things that are important and once you

play12:42

do these two things we are almost 50

play12:44

done because rest all things are pretty

play12:47

easy I'll show you we can write ansible

play12:49

playbook in no time don't worry about it

play12:51

so again ansible perfect ansible is

play12:55

installed now before writing ansible

play12:58

playbooks okay what is ansible Playbook

play13:00

so ansible Playbook is nothing but

play13:02

let's take example of shell okay in

play13:05

Shell scripting you will write a file

play13:07

okay and what do you call that file you

play13:08

call that file as a shell script file

play13:11

right if you are writing in Python you

play13:13

call them as a python files so similarly

play13:15

in ansible instead of calling them as

play13:17

ansible files we usually call them as

play13:20

playbooks that's it okay so whenever

play13:22

somebody is asking you have you written

play13:24

any ansible playbooks that means have

play13:26

you written any ansible files okay but

play13:28

is it mandatory to always write ansible

play13:31

playbooks okay so like for example if

play13:34

you're talking about shell so sometimes

play13:36

you might only have to run some basic

play13:39

shell commands like for example if I

play13:40

have to list some files I can simply do

play13:42

LS hyphen ldr as well right I can simply

play13:45

do lsf and LTR and it listed the files

play13:48

here

play13:49

for that do I always have to write this

play13:51

shell script saying that uh okay uh

play13:54

script.sh and in the script.sh I'll say

play13:57

she bang followed by slash bin slash

play14:00

bash and LS7 LTR and then run this file

play14:03

is it required no so you can also run

play14:05

the shell commands similarly with python

play14:08

you don't have to write Python scripts

play14:10

all the time and execute the Python

play14:12

scripts right you can simply say python3

play14:14

and here you can write some commands and

play14:16

your commands can get executed

play14:19

answer will also support something

play14:20

similar okay so my main intention to

play14:23

explain all of these things is you don't

play14:26

have to write playbooks all the times

play14:27

okay you can also run ansible commands

play14:30

from the CLI okay so it depends on the

play14:33

tasks that you are doing

play14:35

because the reason why I'm explaining is

play14:37

I have seen uh people writing playbooks

play14:40

even for simple tasks so look for

play14:42

example okay this is my ansible server

play14:44

okay sorry this is my ansible server and

play14:47

this is my target server let's say the

play14:49

task is to just create some files on

play14:51

this target server okay so using ansible

play14:54

your requirement that you got from your

play14:56

team is that okay please use ansible and

play14:59

create some files in 100 Target servers

play15:01

but in our case right now we just have

play15:03

one target server I'll also show you

play15:05

with more Target servers don't worry

play15:06

about it so we just have one target

play15:08

server okay uh the task is just create

play15:12

some files on the target server

play15:14

this is a very simple task right so for

play15:16

this simple task I don't I don't want to

play15:18

write a Playbook what is the other way

play15:20

so you can like I told you you can

play15:22

simply run some ansible commands as well

play15:24

it's not always you have to do playbooks

play15:26

so in ansible terminology it is called

play15:29

as ansible ad hoc commands

play15:33

okay so let me show you before writing

play15:35

playbooks how to run ansible ad hoc

play15:38

commands so to run ansible ad hoc

play15:40

commands what you can do is okay ansible

play15:43

followed by

play15:46

the inventory file location like if you

play15:49

followed my yesterdays class I explained

play15:51

what is an inventory file inventory file

play15:53

is nothing but it's a file which stores

play15:55

your IP addresses of the target servers

play15:58

like for example I created an inventory

play16:00

file here let me open this file and show

play16:02

you okay cat inventory what did I do I

play16:05

just added the IP address of my target

play16:07

server like this is my target server and

play16:10

I just added the IP address of the

play16:11

target server in the inventory file okay

play16:14

so this inventory file can be anywhere

play16:16

okay by default ansible uh stores the

play16:19

inventory file in slash Etc slash

play16:22

ansible slash Host this is the default

play16:25

file but you know it's not always

play16:27

convenient to use it from here so

play16:28

whenever you write your playbooks or

play16:30

whenever you run your ads you will add

play16:32

her commands uh you can store the

play16:34

inventory file in the same same location

play16:35

as well so that's the reason why I

play16:37

stored the inventory file here if I have

play16:40

10 servers what I need to do is I can

play16:42

simply uh open the inventory file and

play16:45

append the IP address of 10 servers

play16:48

perfect so ansible

play16:51

minus I the location of the inventory

play16:55

file or the name of the inventory file

play16:56

followed by the IP address okay what is

play16:59

the IP address of your target server if

play17:01

you have one target server you can

play17:03

provide one target server if you have

play17:05

100 Target servers you can group them in

play17:07

the inventory file and you can provide

play17:09

the name of the group I'll also show

play17:10

that don't worry okay so because I just

play17:12

have one target server I can simply say

play17:14

this one or there is also another thing

play17:17

that ansible supports is just say all

play17:19

okay so by saying all it means

play17:22

everything in the inventory file all the

play17:24

servers in the inventory file

play17:26

okay so ansible minus I inventory all

play17:31

minus M that stands for module now you

play17:35

might get a question that okay Abhishek

play17:36

how do you know uh what is the module

play17:38

I'll show you that as well don't worry

play17:40

minus a just create a file okay touch

play17:43

test or touch uh

play17:47

devops

play17:48

class

play17:50

just run this simple command okay I'll

play17:53

explain you the order of this simple

play17:54

command what I am doing in the simple

play17:56

command what is a module in ansible what

play17:58

is this minus a which stands for

play18:00

arguments okay don't worry about it but

play18:02

before that just run this simple command

play18:04

and see what happens

play18:07

okay so what happened is answer will

play18:10

give you that okay it changed something

play18:12

and this yellow line means that

play18:14

everything is good so if you have any

play18:16

errors you will get some red color lines

play18:19

which indicates that there are some

play18:20

errors now let's go to the Target server

play18:22

and see if the file is created LS hyphen

play18:24

LTR okay if you see here devops class

play18:27

and when is it created right now okay

play18:30

so I just created the file so what did I

play18:33

do I use the ansible

play18:36

thank you

play18:43

so I use the ansible ad hoc command so

play18:47

is it very simple right it is very very

play18:50

simple

play18:51

and why will you write a Playbook you

play18:53

will write a Playbook only when you want

play18:55

to execute set of these commands Okay so

play18:57

here there is only one comment that I

play18:59

wanted to execute on the target server

play19:01

right or there is only one task that I

play19:03

wanted to perform on the target server

play19:04

so for performing this one or two tasks

play19:07

you can always go with the ansible

play19:09

advert commands similarly with shell as

play19:11

well right if you want to perform one or

play19:13

two shell commands you can directly

play19:15

execute that one or two commands for

play19:16

example if I want to just say uh LS

play19:19

hyphen LTR and find the size of each

play19:21

file okay so what I would do I would

play19:24

simply do this right LS minus LTR and if

play19:26

there are 10 folders I would see the uh

play19:29

like you know size of these 10 folders

play19:30

using di minus sh I don't have to create

play19:33

a shell script for this similarly if you

play19:35

want to execute one or two ansible

play19:38

commands or one or two tasks using your

play19:41

ansible on your target servers always go

play19:43

for advert commands the reason why I'm

play19:46

stressing this is many interviewers ask

play19:48

these questions what is the difference

play19:50

between ansible ad hoc commands

play19:54

versus ansible

play19:58

Playbook okay so the difference is that

play20:01

ansible advert command for one or two

play20:03

does ansible Playbook is for multiple uh

play20:06

commands perfect I hope the things are

play20:08

clear till now okay what did we do we

play20:10

installed ansible then we configured the

play20:13

passwordless authentication with ansible

play20:14

and we also executed ansible advert

play20:17

commands to create some files or you

play20:19

know to execute some tasks on the target

play20:21

server this is amazing right so we did

play20:24

already three different things on

play20:25

ansible in no time now the question here

play20:28

is okay I know many people are getting

play20:30

this question here that okay how do you

play20:33

know what what is this module uh called

play20:35

shell and how do you know what are the

play20:36

arguments to pass don't worry about it

play20:38

nobody knows everything because there

play20:41

are thousands of models in ansible so

play20:44

you can simply open your browser and

play20:46

search for ansible modules okay this is

play20:49

something that everybody does okay don't

play20:51

worry about it click on all modules

play20:53

because day in day out ansible keeps

play20:55

updating the modules because every day

play20:58

they is so much contribution that is

play21:00

happening to ansible then so like you

play21:02

know people keep adding uh commands to

play21:05

ansible people keep updating the

play21:07

commands to ansibles so always go to the

play21:09

documentation and like you know you want

play21:11

to just create a file okay so I know

play21:14

that using shell you can create so you

play21:16

know this is a shell model execute shell

play21:17

commands on the targets

play21:19

so what I did is I just looked into the

play21:22

documentation and I understood from the

play21:24

documentation oh okay so ansible

play21:26

supports something called as shell

play21:28

commands and using the Shell command I

play21:30

can create some files okay that is what

play21:32

I did and ansible also provides you

play21:35

examples like you know here in this case

play21:37

ansible told you that okay if you want

play21:38

to write a Playbook this is the yaml

play21:40

syntaxing that you want to follow but

play21:42

what we did we just did one single

play21:44

command so that's the reason why I did

play21:46

it through and you will add her comments

play21:49

so minus m stands for modules and minus

play21:52

a stands for what is the command that

play21:54

you wanted to execute right for example

play21:56

let me change this and show you

play21:59

you can execute any shell commands uh

play22:02

let me see what is the

play22:04

number of processors on the target

play22:06

server so what I did I just said n proc

play22:09

and it said that okay the target server

play22:11

has only one

play22:13

CPU let me see

play22:16

DF okay so DF is to identify what is the

play22:20

disk right so disk usage and you know

play22:25

everything about your disk so

play22:27

I got the output using DF

play22:30

perfect so this is how you can modify

play22:32

the advert command arguments and you can

play22:35

also do much thing many other things

play22:37

like you know you know you can copy

play22:38

files from this server to the other

play22:40

server using the ad hoc command like uh

play22:43

if you want to see that just come here

play22:45

in the ansible module section just

play22:47

search for copy okay so see what are the

play22:50

different copy things that ansible is

play22:51

providing so it said that okay so there

play22:53

is an argument called copy there is a

play22:55

module called copy okay so I understood

play22:57

from here what I'll do is I can replace

play23:00

this module from Shell to copy okay and

play23:04

I need to understand what are the

play23:05

arguments that's it so here you can

play23:07

directly jump to the example and see

play23:09

what are the arguments so for the copy

play23:11

these are the different arguments that

play23:13

are passed okay so simply what I can do

play23:15

is in minus a I can replace this

play23:18

arguments like I can say simply

play23:20

SRC colon what is the location I can

play23:24

provide the location of the file

play23:26

after that I can say destination right

play23:28

now I'm just showing it to you how to do

play23:30

it okay and then

play23:32

provide owner details if they are

play23:34

required and all of those things so this

play23:36

is how you will learn ansible ad-hoc

play23:38

commands so I showed you one command you

play23:40

can try different things like like you

play23:42

can try copy you can try SCP you can try

play23:46

uh like you know deleting files on your

play23:48

target server so try all of them by your

play23:50

own

play23:51

now let us see what happens when you

play23:54

have multiple servers right now I just

play23:57

showed you with one server what I did is

play23:59

I added that to the inventory file and I

play24:02

showed you using executing uh this way

play24:05

right so for example I said all

play24:07

what if there are two different servers

play24:09

okay and one set of servers is your

play24:11

teams and one set of server is other

play24:13

teams are let's understand this way one

play24:16

set of servers are

play24:18

PB servers okay database related servers

play24:21

and the other set of servers are web

play24:24

servers okay so there is a use case that

play24:27

runs certain number of playbooks only on

play24:30

the DB servers and run certain number of

play24:32

playbooks only on the web servers okay

play24:34

so if that is a requirement what you can

play24:37

simply do is that in your inventory file

play24:39

okay open the inventory file and in the

play24:41

inventory file you can do grouping of

play24:43

your servers okay so here you can simply

play24:45

mention as DB servers

play24:48

this is just for an example and here you

play24:51

can mention as

play24:52

web server so the brackets are very

play24:54

important okay you have to definitely

play24:56

use the brackets let's say I have web

play24:58

servers with an IP address

play25:02

172.31.62.100 okay so now if I have to

play25:07

execute the ansible commands only on the

play25:10

web servers what I can do is instead of

play25:12

all I can simply say

play25:15

web servers so what will happen is

play25:17

ansible will go to the inventory file it

play25:20

will look into the inventory file it

play25:22

will see what are the list of web

play25:23

servers okay and it will execute the

play25:26

ansible commands on the list of web

play25:28

servers if there is one web server it

play25:30

will execute on one web server if there

play25:32

are 100 web servers it will execute on

play25:34

100 web servers

play25:36

that means the group of machines that

play25:39

are under the name called Web servers

play25:42

so this is another interview question

play25:43

for you so the question is that

play25:46

how do you group the servers in ansible

play25:49

or how can you execute certain number of

play25:51

tasks only on certain number of virtual

play25:54

machines using ansible so for that the

play25:57

answer is that in ansible everything is

play26:00

configured in inventory file all the

play26:02

server names and server details are

play26:04

configured in the inventory file you can

play26:06

do grouping of your servers in the

play26:07

inventory file and tell ansible that

play26:10

okay execute this Playbook or run this

play26:12

ansible command only on certain number

play26:15

of servers or execute only it on web

play26:17

servers or execute only it on DB servers

play26:20

right

play26:21

are things clear till now perfect

play26:26

okay so now let's see how to write

play26:28

ansible Playbook okay so the reason I

play26:31

told you is to write ansible Playbook is

play26:33

when you want to execute multiple tasks

play26:35

so let me take a scenario okay so the

play26:38

scenario here is we want to install

play26:41

nginx okay and what we want to do is we

play26:45

want to start nginx

play26:47

okay so let's say this is the task you

play26:50

want to install nginx and you also want

play26:52

to start nginx so for this what you need

play26:54

to do is you want to write a Playbook

play26:57

perfect so let me call it as

play27:00

first hyphen Playbook and in which

play27:05

format is ansible playbooks written they

play27:07

are written in the yaml format okay so

play27:09

that's why we will say ansible

play27:11

playbook.yaml

play27:13

perfect so to start writing ansible

play27:15

Playbook don't worry I'll definitely

play27:18

keep it very very easy for you guys okay

play27:20

so to to start writing ansible Playbook

play27:22

just start with three hyphens okay so

play27:25

the three hyphens indicates that this is

play27:27

a yaml file and after that what we'll do

play27:30

is you will start explaining about your

play27:31

playbook that is what is the name of

play27:34

your playbook okay so the name of the

play27:36

Playbook let's say is to install and

play27:39

start

play27:40

nginx okay so after that what you will

play27:45

do is you will provide the list of hosts

play27:48

where this Playbook has to run

play27:50

okay so to provide these details like I

play27:54

told you we usually group the servers in

play27:56

the inventory file and if you want to

play27:59

execute in all of them so just provide

play28:01

uh the server names as all so when you

play28:04

provide the server names as all then it

play28:06

simply gets executed on all the servers

play28:08

in your inventory file

play28:11

okay once that that is done so there are

play28:14

two cases one is do you want to execute

play28:16

this Playbook using the same user or do

play28:19

you want to execute this Playbook using

play28:21

the root user what do I mean by that so

play28:23

there are some times where you want to

play28:25

run this Playbook as root user that is

play28:27

because let's say you want to install uh

play28:31

Engineers so if I go to the other server

play28:34

and show you if I do something like uh

play28:37

apt install nginx what will happen is it

play28:40

will tell you that okay unable to

play28:42

acquire dpmg sorry dpk D package logs

play28:46

that is because you are not a root user

play28:48

so what you usually do is you use sudo

play28:50

right so use sudo and then you add the

play28:54

command that is because you want to

play28:55

execute this command as a root user

play28:57

similarly if you want to do something

play28:59

similar on ansible you have to use

play29:03

something called as become root okay so

play29:05

what this become root does is it will

play29:07

execute your ansible playbooks as a root

play29:10

user

play29:11

perfect once this is done you will start

play29:14

writing the tasks okay so in yaml the

play29:18

syntaxing might look difficult for you

play29:21

for the very first time but once you

play29:23

start writing it's not that difficult

play29:25

okay so what we did here we started with

play29:27

three hyphens which indicates that this

play29:29

is the ml file and then we use this

play29:32

hyphen to start the Playbook that means

play29:33

this hyphen indicates that you know it's

play29:36

a list so you are going to provide list

play29:38

of playbooks you can write one playbook

play29:40

in a file or you want to you can also

play29:42

write multiple playbooks in a file so in

play29:44

my case I can only write I only want to

play29:46

write one one Playbook so that's why I

play29:49

said okay let me start this one Playbook

play29:51

and if I have multiple playbooks what I

play29:53

need to do is I can also do this

play29:55

okay hyphen name and

play29:58

my

play30:00

second

play30:03

Playbook okay but don't worry about it

play30:06

now we are writing only one Playbook

play30:07

here

play30:09

so we started with the name of the

play30:11

Playbook then we told uh in which files

play30:13

or on which servers this uh Playbook has

play30:17

to be executed so that is the host which

play30:19

takes from the inventory file so I put

play30:22

here as all so that all the servers in

play30:24

the inventor file gets executed then I

play30:27

use the become command so what become

play30:30

does is uh it will allow you to execute

play30:32

it as a root user or you know you can

play30:34

also use uh something called as become

play30:36

underscore user and you can provide any

play30:38

user that you want to

play30:40

use

play30:42

perfect so now inside the task again

play30:44

whenever you have list of tasks okay

play30:47

whenever you have list of something in

play30:49

uh yaml what you need to do is you have

play30:52

to start with hyphen okay which

play30:53

indicates that okay so there are list of

play30:55

things that you wanted to do so my first

play30:58

task would be again provide the name of

play31:00

the task always describe your task using

play31:01

name okay so install

play31:04

Engineers so this would be my first task

play31:07

and what is the command I would use okay

play31:10

so apt so in either you can do this okay

play31:13

shell and you can say apt install nginx

play31:18

so that is also quite possible

play31:21

this is something that we learned right

play31:23

so we use a shell command we use the

play31:26

shell model and I said apt install nginx

play31:28

you can also do this or ansible by

play31:31

default provides a module for apt so you

play31:33

can also do this like you know you can

play31:35

say apt followed by name what is the

play31:38

name of the module that you want to use

play31:40

so I want to use I mean I want to

play31:43

install nginx so provide the detail as

play31:46

nginx and then you say state is present

play31:49

that means to install

play31:52

so this command is exactly equivalent to

play31:56

shell followed by

play31:59

apt install nginx

play32:03

and it will also start the engine X so

play32:06

uh

play32:07

for that we'll write a new task but okay

play32:09

so shell app install nginx and apt name

play32:14

engineering State present both are the

play32:16

same things now you can ask me okay if

play32:18

both of them are same why do I have to

play32:20

run this one okay to keep it more

play32:22

generic okay so whenever you want to run

play32:24

a certain number of tasks on Ubuntu okay

play32:27

always go with the package manager

play32:29

because sometimes when you are writing

play32:31

the shell commands you might miss some

play32:32

things whereas if you are using the app

play32:34

which is a package manager module that

play32:36

ansible is providing things will be much

play32:38

easier and even into even tomorrow if

play32:41

the shell command is changed or

play32:42

something you can always rely on this

play32:45

ansible module that is provided by the

play32:47

ansible perfect apt name is nginx state

play32:51

is present

play32:54

this is Task number one and how do I

play32:56

write the task number two again uh match

play32:58

the syntax here okay match the

play33:01

indentation here and in the indentation

play33:03

use another hyphen and write your second

play33:05

task what is my second task the second

play33:07

task is to start in Linux so the first

play33:11

task that I have written is to install

play33:13

nginx and the second task that I've

play33:14

written is to start in Linux and to

play33:16

start in Linux again what you can do

play33:19

there are two options here one is

play33:24

you can say shell and you can say

play33:29

system CTL start

play33:33

Engineers or this is one possible way or

play33:36

again like I mentioned above ansible

play33:39

provides a module okay that is called as

play33:41

a service model OKAY using the service

play33:44

module you can say

play33:46

what is the name of the service that is

play33:48

nginx

play33:52

followed by what is the state

play33:55

so the state here is

play33:58

started so I want to have my service in

play34:01

started State that's it so our Playbook

play34:04

is done we wrote our first Playbook

play34:05

isn't it very simple so to write our

play34:08

first Playbook what did we do we started

play34:10

with three hyphens okay the three

play34:12

hyphens indicate that this is a yaml

play34:14

file and then we started to explain what

play34:16

this Playbook is okay and why did we use

play34:19

this hyphen here The Hyphen The Hyphen

play34:21

here is to indicate that there can be

play34:23

list of playbooks okay and this is my

play34:25

first Playbook okay so in the first

play34:28

Playbook what you are doing is you are

play34:30

explaining install and start in Linux

play34:32

and what are the hosts against which

play34:34

this has to get executed so this has to

play34:37

get executed against the all the hosts

play34:39

in the inventory file then become root

play34:42

what does this become root does is this

play34:44

become root will switch the user to root

play34:46

because we wanted to execute some

play34:48

package manager related commands

play34:51

if you just want to uh like you know

play34:53

execute everything through the Ubuntu

play34:55

user let's say your Ubuntu user has

play34:57

permissions to do everything then you

play34:59

don't have to use this line in your

play35:01

playbook

play35:02

okay once you provide all of these

play35:04

details then you go with your actual

play35:07

tasks okay in the actual task you

play35:09

explain okay what is the first task and

play35:12

what is the second task so in the first

play35:14

task I provided install nginx okay so I

play35:17

wanted to install nginx as the first one

play35:19

and then I wanted to ah sorry I did not

play35:22

save this file just give me a moment so

play35:24

that I'll just rewrite all of these

play35:26

things

play35:29

okay yeah so

play35:32

these are the two tasks that are created

play35:33

and this is the description of the task

play35:35

now once we have all of these things

play35:37

okay once we have two tasks here once

play35:40

you have uh written the name of your

play35:42

playbook and then you have explained

play35:43

everything about your playbook what you

play35:45

can do is just save this file okay and

play35:48

now what we'll do is we will execute

play35:51

this Playbook to execute this Playbook

play35:53

previously we use the ansible command

play35:56

right so ansible command is used to run

play35:58

some ansible commands whereas if you

play36:00

want to run ansible playbooks the

play36:02

command that you want to use is ansible

play36:04

hyphen Playbook okay so the difference

play36:07

between ansible and ansible Playbook

play36:09

commands is that ansible is used to run

play36:11

ansible ad-hoc commands whereas ansible

play36:14

Playbook is used to run your ansible

play36:16

playbook files that we have just written

play36:19

perfect as always provide the location

play36:22

of your inventory file if your inventory

play36:24

file is in the default location then you

play36:26

don't have to provide these details like

play36:27

you know if you're using the slash Etc

play36:29

host ansible file as your inventory file

play36:31

then don't worry uh the details will be

play36:35

automatically picked up but always make

play36:37

sure that uh you know provide the

play36:39

inventory file in the location itself so

play36:41

that you know it is it will be more

play36:42

convenient rather than using the default

play36:44

one

play36:45

after that provide the name of your

play36:49

playbook okay the name of the Playbook

play36:50

is

play36:51

firstplaybook.yaml now let's see the

play36:54

magic and let's also try to understand

play36:55

what is happening

play36:57

okay first of all click enter then it

play37:00

said that the first task that is

play37:02

executed is gathering facts the second

play37:04

task that executed is installed nginx

play37:07

and the third task is start engine X so

play37:10

what has happened firstly gather facts

play37:12

so gather fax is nothing but ansible

play37:14

will try to get all the information of

play37:16

your target server it will also see if

play37:18

is it able to authenticate without any

play37:20

password or not then it will get more

play37:22

details that are required on the target

play37:24

server so this is the step that gets

play37:26

executed irrespective of What playbook

play37:29

that you are writing okay gather facts

play37:30

is the first task that gets executed

play37:33

after that install nginx so install

play37:35

nginx is the task that we wrote and what

play37:37

happens after that start in Linux okay

play37:40

so all the three tasks are passed let us

play37:42

see if in nginx is installed here or not

play37:44

so let's run the command sudo

play37:49

system serial

play37:53

status nginx let us see what has

play37:57

happened

play37:58

perfect it said that nginx is running

play38:00

okay so we have done great job till now

play38:03

what I would like to ask is everybody

play38:05

who are following this video uh make

play38:07

sure that you practice these examples

play38:10

okay one is the ansible ad hoc command

play38:12

that I showed you and also make sure you

play38:14

you execute this same Playbook that I

play38:16

have written Okay so it is very simple

play38:19

one follow the video okay and write the

play38:22

same playbook in the same order okay

play38:24

and if you have any questions uh till

play38:26

now I would like everybody to put that

play38:29

in the comment section perfect

play38:31

so we understood what is happening here

play38:33

but for the viewers I wanted to show

play38:35

this in detail so to read understand

play38:38

what is happening within the ansible

play38:40

okay what ansible is doing internally

play38:42

just add the verbose okay minus V and

play38:46

execute the you can add a 2v 3vs uh it

play38:50

only increases the verbosity within the

play38:51

ansible so if you add uh three V's that

play38:54

means ansible prints whatever it is

play38:56

doing with highest verbosity so what is

play38:58

verbosity it basically means debug okay

play39:01

so you will get the debug logs here so

play39:04

previously we understood what was

play39:05

happening uh just like you know ansible

play39:07

created a server and all but if you use

play39:10

verbose if you want to understand

play39:11

ansible in more detailed way what

play39:13

ansible is doing on the target server

play39:15

step by step use the verbose option and

play39:18

ansible gives you everything like you

play39:19

know what what it has started to do

play39:21

firstly when you executed this first

play39:23

playbook.yaml it was searching for path

play39:26

on this Ubuntu Server okay and then it

play39:30

uh use some configuration options after

play39:32

that it started with this install nginx

play39:35

Okay where it gathered the facts The

play39:37

Playbook name is install nginx it

play39:39

gathered the facts C it is trying to

play39:41

establish establish the SSH connection

play39:43

okay so it said that okay SSH connection

play39:46

was established successfully

play39:48

once it established the SSH connection

play39:50

then it was looking for some python uh

play39:53

related stuff okay because ansible by

play39:56

default uses python okay then it also

play39:58

found that what is gather facts doing it

play40:00

is doing all of these things one is the

play40:01

SSH connection one is the uh if you are

play40:04

if your target server has uh you know

play40:06

all the dependencies that are required

play40:08

python dependencies that are required to

play40:10

execute and once that is done it will

play40:13

start with the c there is lot of

play40:15

information it will start with the

play40:17

install in Linux and even in the install

play40:19

nginx for each and every task what is it

play40:22

doing how is it trying to establish the

play40:23

connection and what has happened see uh

play40:26

it also gave you the Json file which

play40:28

indicates

play40:29

what has the things that it has done so

play40:31

it has installed a package for you

play40:32

called nginx and it used apt to do that

play40:36

we did it Force no it just used the app

play40:38

it did not use any force and then D

play40:43

package options everything is present

play40:44

here okay so to learn ansible in a

play40:47

better way use the verbose option and

play40:49

that is what I did when I started to

play40:51

learn ansible in the very first things

play40:53

because I also also wanted to write my

play40:56

own ansible modules so right now I'm

play40:59

only explaining you to how to execute

play41:01

your ansible playbooks but as I told you

play41:03

you can always contribute to ansible and

play41:05

write your own ansible modules so to

play41:07

understand that you need to understand

play41:08

what ansible is doing internally so for

play41:11

that use the verbosity option

play41:13

perfect so what we have done we have

play41:16

also executed our first ansible Playbook

play41:19

now

play41:20

what are the next things to learn in

play41:22

ansible once you learn this you need to

play41:25

understand that okay so I wrote my

play41:27

answerable playbook and this is looking

play41:28

very simple I learned ansible completely

play41:31

no

play41:32

the reason here is this is a very basic

play41:34

installation right we just installed

play41:36

engineic and we just started the in the

play41:38

next service but there are cases where

play41:40

you want to use your ansible playbooks

play41:42

to configure your kubernetes cluster

play41:44

okay so this is a very classic example

play41:46

let me tell you what I wanted to uh what

play41:50

usually people do in real time

play41:52

organizations let's say kubernetes.ml

play41:57

okay so the task here is

play42:00

create

play42:02

three ec2 instances on

play42:06

AWS okay and

play42:09

configure

play42:13

one of those

play42:15

ec2 instances as master

play42:19

and configure

play42:22

two other

play42:24

ec2 instances as worker

play42:29

okay so what is the task create three

play42:31

ec2 instances on AWS configure one ec2

play42:34

instance as master and configure two ec2

play42:36

instances as worker so for that what

play42:38

usually people or what usually develops

play42:40

engine is test is for this uh we use

play42:42

terraform okay and for the two other

play42:45

tasks we use ansible so this is uh you

play42:48

done using ansible and this is also done

play42:50

using ansible so people might ask can't

play42:53

we do this using uh ansible we can do

play42:55

that okay so we can always do uh the

play42:58

installation or uh stuff like creation

play43:02

using ansible as well but terraform is a

play43:04

tool that is specifically designed for

play43:06

that purposes okay you can create ec2

play43:08

instances using ansible as well uh it's

play43:10

like for example if you want to purchase

play43:12

movie tickets okay you can do it through

play43:14

bookmyshow as well and you can do it

play43:15

through paytm as well but you would

play43:17

always go for the best option right so

play43:19

the best option here in this case is

play43:21

terraform so terraform is an

play43:23

infrastructure management tool or it's a

play43:25

instruction as code tool so that's why

play43:26

uh whenever you wanted to create

play43:28

anything with respect to infrastructure

play43:30

go with terraform so uh devops Engineers

play43:32

usually create these ec2 instances using

play43:34

terraform and perform these two actions

play43:37

using ansible now if these two actions

play43:39

cannot be written in one one simple

play43:42

Playbook right for example if you look

play43:44

at our first playbook.yaml this has only

play43:46

two tasks but if you want to configure

play43:48

both your kubernetes control plane as

play43:51

well as your kubernetes data plane or

play43:53

Master as well as your worker nodes

play43:55

your playbook will become very huge okay

play43:58

so almost you will have some 50 to 60

play44:00

tasks and if you start writing this

play44:03

entire 50 to 60 tasks in one

play44:05

playbook.aml file it will be impossible

play44:07

to read okay so for that very own reason

play44:10

what ansible has done and there will be

play44:13

lot of variables and there will be lot

play44:15

of uh configuration files you have to

play44:17

handle some errors right to avoid that

play44:20

uh specific kind of problems what

play44:22

ansible has done is it has come up with

play44:25

a concept called ansible roles okay what

play44:28

is that ansible roles

play44:31

now what is ansible rules so ansible

play44:34

rules is nothing but like you know in a

play44:36

very simple terminology if I have to

play44:38

explain your ansible roles ansible roles

play44:41

is efficient way of writing ansible

play44:43

playbooks that will only improve your

play44:45

efficiency to write complex playbooks

play44:47

okay the best example that you can do

play44:50

that you can give is whenever some

play44:51

interviewer interviewer is asking you

play44:53

this question you can simply explain

play44:55

them that okay let's say I want to

play44:57

configure a kubernetes using ansible so

play45:00

it will have close to some 50 to 60

play45:02

tasks and you have a lot of variables

play45:05

you have lot of parameters you have

play45:07

certificates right you have secrets that

play45:09

you have to configure while creating

play45:10

this kubernetes cluster so for that very

play45:13

own reason if you try to do it with

play45:15

rules like you can segregate each and

play45:17

everything and you can properly

play45:19

structure your ansible playbooks so

play45:21

that's why the concept of roles is

play45:23

introduced

play45:24

so if you want to start playing with

play45:27

rules okay what you can simply do is you

play45:30

can use this command called ansible

play45:31

Galaxy okay and then you can say

play45:34

initialize okay ansible Galaxy role init

play45:38

stands for initialize and just start

play45:41

saying that uh I want to create a role

play45:43

for kubernetes okay so let me uh create

play45:45

a folder for this before that

play45:52

okay so this is my second playbook for

play45:55

example my second Playbook is to

play45:57

configure kubernetes so what I'll do is

play46:00

I'll do ansible hyphen Galaxy role

play46:04

init kubernetes now let us see what

play46:07

happens as you execute this command okay

play46:10

what happened is ansible said that role

play46:12

kubernetes was created successfully what

play46:14

is that let me do LS so if you see LS if

play46:17

you do LS you'll see that there is a

play46:19

folder called kubernetes that is created

play46:22

let me open that folder and see what is

play46:24

inside that okay

play46:27

LS

play46:30

kubernetes okay you will see bunch of

play46:33

files that are created for you this is

play46:35

the concept of roles okay if you do LS

play46:38

hyphen LTR and see what is inside each

play46:41

and every folder okay so whenever you

play46:44

execute this command ansible creates you

play46:47

bunch of files and folders okay and

play46:49

using these files and folders you can

play46:51

structure your ansible playbooks okay so

play46:55

you have templates you have files you

play46:56

have readme where you can explain what

play46:58

this Playbook is and what are the rules

play47:01

and responsibilities of this Playbook

play47:02

you can create tasks like previously we

play47:04

used to write tasks in the playbook.yaml

play47:06

right whereas here you will use the task

play47:09

and then you have handlers you have

play47:11

tests vars defaults and meta I'll

play47:14

explain you each and every of this thing

play47:16

don't worry but till now what I wanted

play47:18

to explain you is that whenever you want

play47:21

to write some complicated playbooks okay

play47:23

what what you would do is you will start

play47:26

using the ansible Galaxy command to

play47:29

create roles and what happens if you

play47:31

create roles is you can write structured

play47:33

and efficient ansible playbooks okay so

play47:37

to write this entire playbooks in this

play47:39

live session uh it will get slightly

play47:41

complicated so that that's the reason

play47:43

why I have put everything on my GitHub

play47:46

repository okay so if you see here this

play47:48

is my GitHub repository I I already know

play47:51

that most of you are following me on

play47:52

GitHub and if you are not then I have

play47:55

the link in the description don't worry

play47:56

you can

play47:58

go and click on that GitHub and you will

play48:00

see a repository called ansible examples

play48:02

so I folk this from ansible hyphen

play48:05

ansible examples but I would recommend

play48:06

you to follow this repository not the

play48:09

parent one because my goal is to keep

play48:11

updating this ansible repository with

play48:13

more examples so that whenever I create

play48:16

a new example you can watch for it for

play48:17

example today there is no kubernetes

play48:19

example here

play48:20

I don't have any kubernetes example that

play48:23

is stored in this repository so in

play48:25

future I'll add that thing so that you

play48:27

can basically watch for it

play48:30

so for example here there are a bunch of

play48:33

playbooks right so let's say that I want

play48:35

to look for JBoss Standalone okay I want

play48:37

to install a JBoss server and I want to

play48:40

configure the JBoss server so what you

play48:43

will do is instead of writing everything

play48:45

inside the playbook.yaml like uh this is

play48:48

the playbook.yaml file so the yaml file

play48:50

that is present at the parent level or

play48:52

at the top of your folder is your parent

play48:55

playbook.yaml that is the same as we

play48:57

wrote here okay so if you remember we

play49:00

wrote something called as first

play49:01

playbook.aml right so this is the

play49:03

site.aml side.aml is here parent

play49:06

playbook.aml in this case okay what did

play49:08

I provide I just provided the name of

play49:10

the host and then I said that everything

play49:13

else you can find in the role okay so

play49:16

previously what we used to write we used

play49:18

to write uh what is the name of this

play49:19

Playbook what are the hosts and then we

play49:22

used to write task here itself right

play49:23

instead what we do is whenever we are

play49:26

writing roles we just segregate and we

play49:29

say that okay this is the whole us and

play49:31

if you want to look at the tasks and

play49:32

other details just find it in the roles

play49:34

folder and what is the rules folder

play49:36

JBoss Standalone okay let us see what is

play49:38

there in the JBoss Standalone folder

play49:40

okay go to the roles and inside roles

play49:43

you have JBoss Standalone okay if you go

play49:45

to jibos Standalone then we have all of

play49:48

these files and folders right so

play49:50

whenever you do by default you get bunch

play49:53

of things if you don't require some of

play49:55

these things for example here second

play49:57

Playbook and inside this I have

play49:59

kubernetes

play50:01

CD second Playbook

play50:04

okay and inside this CD kubernetes

play50:07

so here you have bunch of these things

play50:09

right so if you let's say you don't want

play50:11

to read me dot yaml let's say you don't

play50:12

want default you can delete them no

play50:14

worries okay so here you have files and

play50:17

let's tasks and templates so inside the

play50:19

task we write everything that is related

play50:22

to this task.aml okay that is all the

play50:25

tasks so previously we used to write

play50:26

everything on the parent playbook.yaml

play50:29

but to segregate and to make things

play50:31

better what you do is you create a role

play50:33

and inside the role you write all of

play50:35

this tasks

play50:37

perfect so this is a view segregate and

play50:41

improve the efficiency but what are the

play50:43

other files and folders here okay I

play50:45

explained what is Task but what is this

play50:47

files what is this folders what is this

play50:49

templates so let me explain you that as

play50:51

well like for example okay so this is

play50:54

the entire structure right you have

play50:56

templates you have files you have tasks

play50:59

you have handlers tests wires defaults

play51:02

and meta let's start with meta So Meta

play51:04

is basically used to write some metadata

play51:07

information inside this file for example

play51:10

let's say I have uh I want to provide

play51:13

what are the details of this entire

play51:16

Playbook what is this entire Playbook

play51:17

doing so in such cases you can provide

play51:19

the metadata information and also you

play51:21

can provide the licensing information

play51:23

that is in the future let's say you want

play51:25

to share this uh playbooks or share this

play51:28

entire Playbook plus role uh to the

play51:31

community okay ansible Galaxy community

play51:32

so in such cases what you can do is you

play51:35

can write the entire information like

play51:37

you know this Playbook I have written

play51:39

for uh the open source purpose and I

play51:41

want to share it and if anybody wants to

play51:43

use it for commercial purpose it is not

play51:45

allowed okay so such things you provide

play51:48

in The Meta folder after that you have

play51:50

defaults okay defaults is basically used

play51:52

to store some variable kind of things

play51:54

like for example if you go to defaults

play51:56

today and open the main.yaml so default

play51:59

file default file for kubernetes okay

play52:01

you can store some variables and you can

play52:03

do the same thing with vars folder and

play52:06

also Group ads folder I'll explain you

play52:08

what is the difference between each of

play52:09

them in the interview questions don't

play52:11

worry

play52:12

and then there is test folder so test is

play52:15

just like any other test that you write

play52:16

uh even if you are writing uh shell

play52:18

scripts or even if you're writing Python

play52:19

scripts you basically add some unit

play52:21

tests right so that is similar here and

play52:24

then you have handlers handlers is

play52:26

basically for handling some kind of

play52:28

exception let's say while you are

play52:30

starting uh nginx server in our previous

play52:32

example we ran into some error so using

play52:35

the handlers what you can do is you can

play52:37

either send some mail notifications or

play52:38

you know you can say that whenever This

play52:40

nginx Server is failing to start there

play52:43

is no purpose of installing nginx itself

play52:45

so delete the nginx also okay so this

play52:48

sort of things you can handle it so

play52:49

Handler is basically the situations that

play52:52

you want to handle

play52:54

then task I don't I don't have to

play52:56

explain it I know that you all are

play52:58

familiar with tasks by now and readme.md

play53:01

so basically you can explain uh our

play53:03

viewers or you know who are watching

play53:05

this repositories you can tell them that

play53:06

what is the uh content of this uh uh

play53:09

ansible playbooks what we stored in the

play53:11

sensible playbooks and all then files

play53:13

you can store some files like uh for

play53:15

example you have some uh certificates or

play53:17

you know uh you have some uh basic

play53:20

example is let's say you have some

play53:22

index.html and you want to pass this

play53:24

index.html okay so in such cases you can

play53:27

use uh files as well like uh for example

play53:30

you want to pass the index.html onto the

play53:33

uh task okay you want to copy the

play53:35

index.html from here to another machine

play53:38

so using the uh files folder you can

play53:41

store them and you can pass so template

play53:43

is for basic templating right so ansible

play53:45

uses the ginger to templating and if you

play53:47

want to use any anything related to this

play53:50

templates you can store in the templates

play53:52

folder

play53:53

perfect so this is about your ansible

play53:55

structure and why ansible roles are used

play53:58

so you can take a reference or take a

play54:00

look at these files and folders that I

play54:03

stored in my GitHub repository like I

play54:05

told you there are bunch of examples

play54:07

okay so you can start looking at them

play54:09

but before that what you need to do is

play54:11

you have to be acquainted with ansible

play54:14

advert commands you have to start

play54:16

writing your first ansible playbooks and

play54:19

then you in like you know I also showed

play54:21

you how to establish password

play54:22

authentication and all of these things

play54:24

get familiar with them as well after

play54:26

that go to this roles concept and

play54:28

Implement ansible roles for that you can

play54:31

pick any of the example that I have here

play54:33

best start with the JBoss Standalone

play54:35

okay if you have any questions with this

play54:37

table Standalone uh if you tried and if

play54:39

it doesn't work you can let me know but

play54:41

uh pretty sure this will work because

play54:43

these are all tested playbooks even

play54:45

myself I have tested uh some of these

play54:47

playbooks so they'll definitely work

play54:49

now

play54:51

if you want to learn more about the

play54:53

ansible interview questions uh don't

play54:54

worry I've already done a video on it uh

play54:57

just to not stretch this video and to

play54:59

repeat the same content I'll put the

play55:01

link in the description I have put 18

play55:03

most asked ansible interview questions

play55:05

okay 18 answer will interview questions

play55:08

and their answers so it covers all of

play55:10

these topics it covers roles it covers

play55:12

handlers it covers like you know uh some

play55:15

uh complicated scenario based questions

play55:18

on ansible so watch that video so that

play55:20

you get clear understanding of what are

play55:22

the interview questions that people can

play55:23

ask on ansible apart from this if you

play55:26

have any questions if you want me to do

play55:28

uh more videos on ansible if you fail to

play55:31

understand some topics on ansible do let

play55:33

me know in the comment section and I'll

play55:34

definitely uh do a video for you guys

play55:37

I hope you enjoyed this video if you

play55:39

like the video click on the like button

play55:40

and if you have someone who wants to

play55:43

learn ansible please share with this

play55:45

share this video with them thank you so

play55:47

much I'll I'll see in the next video

play55:48

take care everyone bye

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

5.0 / 5 (0 votes)

Related Tags
DevOps CourseAnsible TutorialConfiguration ManagementPasswordless SSHAd-Hoc CommandsAnsible PlaybooksYAML SyntaxRole-Based AccessInfrastructure AutomationAWS EC2