Vous êtes sur la page 1sur 4

Ansible Introduction

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In this series, we will explore one of the most popular configuration management tool Ansible. We will
try to cover each aspect of Ansible with a demo.

What is Ansible?

Ansible is an IT automation and configuration management tool. It can configure systems, deploy
software, and orchestrate more advanced IT tasks such as continuous deployments or zero downtime
rolling updates. Ansible manages machines in an agent-less manner using SSH or password which makes
it simple to configure and operate.

Ansible components:
 Control Node: A control node is any machine with Ansible installed. You can run commands and
playbooks, invoking /usr/bin/ansible or /usr/bin/ansible-playbook, from any control node. You
can use any computer that has Python installed on it as a control node - laptops, shared
desktops, and servers can all run Ansible. However, you cannot use a Windows machine as a
control node. You can have multiple control nodes.
 Managed Nodes: Managed nodes are the network devices (and/or servers) you manage with
Ansible. Managed nodes are also sometimes called “hosts”. Ansible is not installed on managed
nodes.
 Inventory: An inventory is a list of managed nodes. An inventory file is also sometimes called a
“hostfile”. Your inventory can specify information like IP address for each managed node. An
inventory can also organize managed nodes, creating and nesting groups for easier scaling.
 Modules: Modules are the units of code Ansible executes. Each module has a particular use,
from administering users on a specific type of database to managing VLAN interfaces on a
specific type of network device. You can invoke a single module with a task, or invoke several
different modules in a playbook.
 Tasks: Task is the units of action in Ansible. You can execute a single task once with an ad-hoc
command or multiple task with the help of playbook.
 Playbooks: Playbooks are ordered lists of tasks, saved so you can run those tasks in that order
repeatedly. Playbooks can include variables as well as tasks. Playbooks are written in YAML and
are easy to read, write, share and understand.

Next, we are going to learn how to install Ansible.

Ansible Installation Demo:


##--------------------------
## Ansible: Installation ##
##--------------------------
## We are going to use VirtualBox VM and AWS EC2 instances for most of the demo.
## Below is the configuration for our VirtualBox VM’s
# hostnames ip os role
# --------- ------------ -------- ------------
# system1 192.168.0.10 Centos 7 Control Node
# system2 192.168.0.20 Centos 7 Managed Node One
# system3 192.168.0.30 Centos 7 Managed Node Two

## Create an user for ansible in all the systems


useradd debjeet
passwd debjeet
passwd -x -l debjeet

# Login using above user


## Install ansible in Control Node (192.168.0.10)
sudo yum install ansible -y

#to install in amazon linux: sudo amazon-linux-extras install ansible2

## Check ansible version


ansible --version

## run your 1st ansible command


ansible localhost -m shell -a "echo hello $USER from Ansible"

## Check package for paths


rpm -qa | grep ansible #copy package name
rpm -ql ansible-2.4.2.0-2.el7.noarch | more #replace package name

#config file: /etc/ansible/ansible.cfg


#inventory: /etc/ansible/hosts
#role: /etc/ansible/roles
#executable: /usr/bin/ansible
#modules: /usr/lib/python2.7/site-packages/ansible/

## View ansible configuration details


ansible-config view

## Get help
ansible --help
ansible-doc --list
ansible-doc <module> #ansible-doc ping will return help for ping module

##############################################
## Configure Ansible for remote execution ##
##############################################
## Update your Ansible inventory
sudo vi /etc/ansible/hosts
--------------
[local]
192.168.0.10
[appserver]
192.168.0.20
[dbserver]
192.168.0.30
[remote]
192.168.0.20
192.168.0.30
-------------
:wq

## Create ssh key in the control node


## Your public key has been saved in /home/debjeet/.ssh/id_rsa.pub
## hit enter three times
ssh-keygen -t rsa -b 4096 -C "debjeet@cloudaffaire.com"

## Copy ssh key in all nodes


## Provide password for the user that has been created in previous step when
prompted
ssh-copy-id debjeet@192.168.0.10
ssh-copy-id debjeet@192.168.0.20
ssh-copy-id debjeet@192.168.0.30

## Check if keys are working


## On control node (192.168.0.10)
## Make sure host file has proper entry /etc/hosts
ssh system2
exit
ssh system3
exit

## Execute ad-hoc commands against managed nodes


ansible remote -m shell -a "hostname"
ansible all -m ping #all command include all systems defined in inventory file
ansible -m command -a "df -h" remote

Hope you have enjoyed this article. In the next blog post, we will discuss Ansible Inventory.

To get more details on Ansible, please refer below Ansible documentation.

https://docs.ansible.com/