Saturday, April 11, 2020

Ansible: Playbooks

What is YAML:
YAML stands for YAML Ain’t Markup Language, which is human-readable and often used in the configuration file.
Features of YAML:
  • Compared to XML or JSON, YAML is less complex and provides the same features.
  • It provides configuration settings without the need to learn complex code types such as CSS, JavaScript or PHP
Basic Rules for YAML:
  •  You must end the YAML files with .yaml or .yml extension.
  •  YAML must be case sensitive.
  •  YAML doesn’t support the use of tabs. Instead of tabs, it uses spaces which are supported universally.
Basic Data Types of YAML:
YAML supports some basic data types which can be used with programming languages. Such as
1. Scalars: Strings or numbers.
2. Sequences: arrays or lists.
3. Mappings: hashes or dictionaries.
Example of Yaml file:
---
- hosts: dev
  become: true
  tasks:
  - name: to install httpd
    yum: 
    name: httpd 
    state: latest  - name: to install tomcat
    yum: name= tomcat state= latest  - name: to start httpd service
    service: name=httpd state=started  - name: to start tomcat service
    service: name=tomcat state=started
Playbooks:
1. Hosts and Users
2. Tasks lists
3. Handlers
4. Ansible Variables(Vars, Vars_prompt)
5. Ansible Loops
Hosts and Users:
Each play in a playbook, you get to choose which machines in your infrastructure to target and what remote user to complete steps(called tasks).
Ex:
---
- hosts: dev
  remote_user: root
Tasks List:
  • Each playbook contains a list of tasks.
  • Tasks are executed in order, one at a time, against all machines matched by the host pattern.
  • The playbook will run from top to bottom.
Ex:
tasks:
- name: to install apache2
  apt:
  name: apache2
  state: latest
Handlers:
Running operations On Change.
Example:
---
- hosts: dev
  remote_user: true
  tasks:
  - name: install apache2
  apt:
  name: apache2
  state: latest
  notify:
  - start apache
  - name: to install tomcat  apt:
  name: tomcat7 
  state: latest
  notify:
  - start tomcat

handlers:
 - name: start apache
   service:
   name: apache2
   state: started - name: start tomcat
   service:
   name: tomcat7
   state: started
Ansible Variables:
  • A Variable is an element which can hold a specific value.
  • Variable names can be letters, numbers, and underscores.
  • Variables should start with a letter.
  • Here we have two types of variable methods, 1- Vars & 2- Vars_prompt

Vars:
Vars is nothing but a static functionality. Once the functions in playbooks are declared it can not change.
Example:
---
- hosts: all
  remote_user: root
  vars:
  pack1: net-tools
  pack2: wget
  tasks:
  - name: install package {{pack1}}
    yum:
    name:"{{pack1}}"
    state: latest
    when: ansible_distribution=="CentOS"

  - name: install package {{pack2}}
    apt:
    name: "{{Pack2}}"
    state: latest
    when: ansible_distribution=="Ubuntu"
Vars_prompt:
It will take the user input while executing playbook with vars_prompt and store into a variable.
Example: 
---
- hosts: centos
  remote_user: root
  vars_prompt:
  - name: pack_name
  prompt: Give Package name
  private: no
  tasks:
  - name: install package {{pack_name}}
    yum:
    name: "{{pack_name}}
    state: latest
Ansible Loops:
Ansible Loops can do many things in one task, such as create a lot of users, copy a set of files, install a lot of packages.
Example: copy multiple files using ansible loops.
---
- hosts: prod
  remote_user: root
  tasks: 
  - name: Copy{{item}}
  copy:
  src: /root/files/{{item}}
  dest: /tmp/{{item}}
  with_items:
  - abc1.txt
  - abc2.txt
  - abc3.txt
Add multiple users using ansible loops:
Example:
---
- hosts: all
  remote_user: root
  tasks: 
  - name: add group
    group:
    name:"sales"
    state: present

  - name: add user {{item}}
    usr:
    name: {{item}}
    group:"sales"
    state: "present"
    with_items:
    - user1
    - user2
    - user3


<Ansible: Adhoc Commands                                                          Ansible: Ansible Roles >








Ansible: Adhoc Commands

Ad-hoc command modules and how to use it?

Ad-hoc Commands:
Ad-hoc commands will perform quick tasks and not save that certain command for a later stage. In the commands below “-a” indicates that the command is an ad-hoc command.
See the list of nodes from the master server.
#ansible ubnt –m command –a ls/
Create a demo file in node2 from the server.
#ansible  cent –m  raw  –a ‘touch  /tmp/demo.txt’
Install Java on node2 from the server.
# ansible cent –m  raw  -a  ‘yum install –y java’

                        (or)

#ansible cent  -m  yum  -a  ‘name=java  state=latest’
Start/manage services.
#ansible  cent  -m  service  -a  ‘name=httpd   state=started/restarted’
Copy a file from ansible server to nodes.
#ansible all –m  copy  -a  “src= ‘/root/devops.txt’  dest=’/tmp/devops.txt’”
Create a file.
#ansible all –m  file  -a “name=/opt/sample.txt  state=touch”
Remove a file.
#ansible all –m  file  -a “name=/opt/sample.txt  state=absent”
Create a directory.
#ansible all –m  file  -a “name=/opt/devops  state=directory”


<Ansible: SSH configuration                                                 Ansible: Ansible Playbooks>


Ansible Tool Introduction

                                                                                                                                   Next

What is Configuration Management? (Applicable to any CM tools)

Configuration Management (CM) is a systems engineering process for establishing and maintaining the following activities
 
  • Package installation (we have to install packages for multiple servers)
  • Configuration of servers
  • Application deployment
  • Continuous testing of already installed application
  • Provisioning (Provisioning means providing or making something available in Servers based on requirement)
  • Orchestration (Controlling multiple nodes from central location)
  • Automation of tasks

What is Ansible ?

Ansible is an Open Source tool, used as a “Configuration Management System” designed to control a large number of servers. It is used to deploy applications and provision software. Easy to use for systems administrators and DevOps teams. It allows DevOps teams to control many different nodes or systems from a central ansible server.

  • Ansible is radically simple IT automation platform that makes your applications and systems easier to deploy.
  • Ansible have the core component named Master, no need to install agent because ansible is agentless.
  • Ansible Master will controls the server infrastructure (For multiple nodes).
  • Ansible uses ssh connection between Master and Client Nodes.
  • Ansible uses push model(puppet and chef uses pull model).
  • If it is push model the configurations what we prepared at master it can directly pushed to multiple nodes.
  • Large number of ready to use modules for system management     Ex: ping, setup, apt, yum, and many more module service are ready to use modules)
  • Custom modules can be added if need (we can use existing modules or else we create our own modules)
  • Last but not least Simple and Human readable.

Ansible Architecture

Ansible Architecture



Ansible Terminologies

  • Ansible (Ansible Master)
  • Host Inventory (It contains all node IP addresses)
  • Playbooks (The scripts we develop for small tasks are playbooks)
  • Core Modules (Predefined Modules)
  • Custom Modules (We can design our own custom modules-user defined )
  • Plugins
  • Connection Plugins (Through connection plugins we can connect to multiple nodes)
Controller machine Or Ansible Master

           The controller machine is known as the Ansible Master, where Ansible is installed and configured is referred to as an Ansible Master or Controller Machine. The controller server will do the orchestration work for the respective client nodes.

Host Inventory
          
           Inventory is nothing more than a list of the server IP addresses that contains all node IP’s. By adding a node IP in the inventory file we can deploy the applications to the nodes through an SSH Connection.

Module

            A module is a Fundamental unit in Ansible. It is also referred to as “Task Plugins” or ” Library Plugins”.  Modules can be used in two ways:
                           i.   Ad-hoc Commands
                           ii.  Playbooks

Tasks

            A task is a single procedure or work to be executed, installed or configured in the process.

Playbooks

            Playbooks are a set of tasks written in python/YAML scripting languages. There are different sections in the ansible playbook:
                   i. Ansible Tasks
                   ii. Ansible Variables
                   iii. Ansible Handlers
                   iv. Ansible Templates
                   v. Ansible Files



Ansible SSH configuration

<Ansible Installation                                                      Ansible: Adhoc Commands>

SSH Configuration

Configure ssh connection, on both Ansible server and Node servers.
Ansible Machine:
Generate a key, see the steps below.
$sudo su -

#ls –l

#cd  .ssh 

#ssh-keygen


Node-1
Create passwords, configure sshd file and start services.
To create the password,
#passwd root  ----->provide password
Edit the sshd_config file, In this Configuration file enable the Permit root login and Password authentification.
#vi /etc/ssh/sshd_config   ,
Permit root login  yes

password authentification yes
Restart the sshd service.
#systemctl restart sshd
Get the Ip address
#hostname –i   or   ip  a
Node-2
Follow the same steps on Node 2 used on Node 1.
Create the password,
#passwd root  ----->provide password
Edit the sshd_config file, In this Configuration file enable the Permit root login and Password authentification.
#vi /etc/ssh/sshd_config   ,
Permit root login  yes

password authentification yes
Restart the sshd service.
#systemctl restart sshd
Get the Ip address
#hostname –i   or   ip  a

From Master Side

#ssh-copy-id  root@node-1-ip 
#ssh-copy-id root@node-2-ip #ssh root@node-1-ip #ssh root@node-2-ip


Featured Post

Ansible Tool Introduction

                                                                                                                                    Next ...