Saturday, April 11, 2020

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


Ansible Installation

                                                                                                                      Next Step

Steps to install Ansible

  • For example, take three servers (1-Ansiblemaster, 2-Nodes)
  • Ansible as a means of managing our various servers, we need to install the Ansible software on at least one machine.

Ansible Installation on a CentOS/RedHat Server
Updating the machine.
#yum update
After updating the server with the above command, install the required packages
# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
Installing Ansible on CentOS.
#yum install -y ansible
Verify Ansible Installation.
# ansible --version

Ansible Installation on Ubuntu Server
Update Server.
#apt update
After updating the server with the above command, install the required packages.
# apt install -y software-properties-common
Create a repository.
#sudo apt-add-repository ppa:ansible/ansible
Install Ansible on Ubuntu Server.
#apt install -y ansible
Verify Ansible installation.
# ansible --version

Ansible Configuration and Host Inventory
Configuration file default location:
#vi /etc/ansible/ansible.cfg

#cp /etc/ansible/ansible.cfg   ~/

#vi ansible.cfg
After this need to enable some configurations in “#vi ansible.cfg”.
   inventory =/root/hosts/

   library =/usr/share/my_modules/

   module_utils =/usr/share/my_module_utils/

   remote_tmp =~/.ansible/tmp

   local tmp = ~/.ansible/tmp

   plugin_filters_cfg =/etc/ansible/plugin_filters.yml

   forks =5

   poll_interval =15

   sudo_user =root

   transport =smart

   remote_port = 22

   module_lang =C

   module_set_locale=False

   deprecation_warnings=False

Host file default location:
#vi /etc/ansible/hosts

#cp /etc/ansible/hosts  ~/

After changing the path of the Host and ansible.cfg files, first add the remote server IPs in the host inventory file and save the file.
#vi hosts
To check the connection is held or not in master. We can check with this command.
#ansible all –list-hosts  --> to see all connected nodes lists
Get all node details.
#ansible all –m ping

#ansible all –m setup
See if the selected node is connected.
#ansible ubnt –m ping

Featured Post

Ansible Tool Introduction

                                                                                                                                    Next ...