Ansible

Ansible: Step by step Installation and configuration on vagrant machine.

Ansible is an open-source software provisioning, configuration management, and application deployment tool. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows. It includes its own declarative language to describe system configuration. DevOps culture is a recent hot cake trend in IT industry and Ansible is one it’s vital tools. I assume that audience of this tutorial are familiar with different Ansible’s terms, so I briefly discuss them below.

Modules

small programs that do some work on the server, so for example instead of running this command

sudo apt-get install htop

we can use apt module and install htop

#yaml 
- name: Install htop
  apt: name=htop

or

#adhoc command
anisble host -m apt -a "name=htop state=present"

Using module give you the ability to know if it’s installed or not. For more details about module you can check on official documentation.

Plugins

Plugins are pieces of code that augment Ansible’s core functionality. Ansible ships with a number of handy plugins, and you can easily write your own. More about plugins is here.

Host inventories

To provide a list of hosts, we need to provide an inventory list. This is in the form of a hosts file.In its simplest form, our hosts file could contain a single line.

35.178.45.231  ansible_ssh_user=ubuntu

More on working with inventory you can find here.

Playbooks

Ansible playbooks are a way to send commands to remote computers in a scripted way. Instead of using Ansible commands individually to remotely configure computers from the command line, you can configure entire complex environments by passing a script to one or more systems. In this article I am not going to use play-book, however in future tutorial most of the deployment will conduct using it. So, you better learn more about play-book from official documentation.

What I am going to do

I am going to show step by step installation and configuration process on Vagrant hosted machines and some basic command of Anisble.

Prerequisites

  • Familiar with Unix/Linux CLI.
  • Familiar with Vagrant and it’s basic command.
  • Familiar with CentOS and Ubuntu.
  • Familiar with SSH.
  • Basic Networking and Client-Server communication Knowledge.

Vagrant should preinstalled in your machine before start this tutorial.

Step 1:

Make a directory named ~/Anisble and  vagrant init  inside it-

mkdir Ansible
cd Ansible
vagrant init

Step 2:

Open the vagrantfile in your favourite editor. I am using here VSCode

code vagrantfile

Go to line number 15 on vagrantfile and comment it using #. This line is used for search vagrant box, we will manually add Ubuntu and CentOS box so we do not need that line.

config.vm.box = "base"

change it to:

# config.vm.box = "base"

Scroll down to the last line of vagrantfile and add following configuration codes just above   end :-

----

#   apt-get install -y apache2
# SHELL
# ---- new code ---
# Some time it will take long time to download and execute configuration code. Following line is for 
# increase boot timeout value from default 300 mseconds to 12000 msecond
 
config.vm.boot_timeout=12000
# add new hosts, vagrant boxes and give ip to machines
config.vm.define "ubuntu" do |ubuntu|
  ubuntu.vm.hostname="ubuntu"
  ubuntu.vm.box="bento/ubuntu-17.10"
  ubuntu.vm.network "private_network", ip:"192.168.33.10"
end
config.vm.define "centos" do |centos|
  centos.vm.hostname="centos"
  centos.vm.box="bento/centos-7.4"
  centos.vm.network "private_network", ip:"192.168.33.20"
end
config.vm.define "server1" do |server1|
  server1.vm.hostname="server1"
  server1.vm.box="bento/ubuntu-17.10"
  server1.vm.network "private_network", ip:"192.168.33.30"
end
config.vm.define "server2" do |server2|
  server2.vm.hostname="server2"
  server2.vm.box="bento/centos-7.4"
  server2.vm.network "private_network", ip:"192.168.33.40"
end
# end of new code

end

Save the changes into vagrantfile and run the vargrant machines

vagrant up

It may take little long time to download and deploy all four machines. After successfully execute the vagrant up with have to check whether all four machines running or not.

vagrant status

Output:-

Current machine states:

ubuntu                    running (virtualbox)
centos                    running (virtualbox)
server1                   running (virtualbox)
server2                   running (virtualbox)

This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.

We can see that  all four machines are running without error.

Step 3:

In this step we will access the ubuntu and centos hosts using  vasgrant ssh command and install Ansible into them. First ubuntu host:-

vagrant@ubuntu:~$ sudo apt-get update

vagrant@ubuntu:~$ sudo apt-get install software-properties-common

vagrant@ubuntu:~$ sudo apt-add-repository ppa:ansible/ansible

vagrant@ubuntu:~$ sudo apt-get update

vagrant@ubuntu:~$ sudo apt-get install ansible

Now  logout  from ubuntu and access to centos host:-

vagrant@centos:~$ vagrant ssh centos

---------------------

vagrant@centos:~$ sudo yum -y install epel-release
vagrant@centos:~$ sudo yum -y install ansible

Ansible has installed in both ubuntu and centos hosts. this machine are now controller hosts. Using Ansible we will install or configure others machines from the controller hosts.

Step 4:

At this moment we are now in centos host. I this step we will configure default host file and generate a ssh key so that we can securely access to the other machines of the network.

At first configure the Ansible default host file:-

vagrant@centos:~$ sudo vim /etc/ansible/hosts

Go to end line of hosts file and add following hosts ip on it:-

## server1 ip
192.168.33.30
## server2 ip
192.168.33.40

Now generate ssh-keys (public/private) using following command:-

ssh-keygen

While execution of above command they asked for a passphrase, you can give one for leave it empty. I left it empty here:-

Generating public/private rsa key pair.
Enter file in which to save the key (/home/vagrant/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/vagrant/.ssh/id_rsa.
Your public key has been saved in /home/vagrant/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:BpZj9IP/Q0wfKs+8EbZKdCj4nl3Z0iA+3GEZ4MGNZbA vagrant@ubuntu
The key's randomart image is:
+---[RSA 2048]----+
|      ..+=o      |
|     . =o=.      |
|      B E o .    |
|     + + = = .   |
|    . . S % .    |
|     . * & O     |
|      . = & o    |
|     . + + =     |
|      o o .      |
+----[SHA256]-----+

We have to copy the ssh keys to target machines, other wise we are unable to access them.

copy the ssh key to server1:-

vagrant@centos:~$ ssh-copy-id 192.168.33.30

server1 will asked for password. The password is  vagrant . Hit enter and following out put will show if ssh key copied successfully:-

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.33.30'"
and check to make sure that only the key(s) you wanted were added.

copy the ssh key to server2:-

vagrant@centos:~$ ssh-copy-id 192.168.33.40

server2 will asked for password. The password is  vagrant . Hit enter and following out put will show if ssh key copied successfully:-

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.33.40'"
and check to make sure that only the key(s) you wanted were added.

Now both server1 and server2 are ready for access. we can access the servers using following command:-

vagrant@centos:~$ ssh 192.168.33.30
Welcome to Ubuntu 17.10 (GNU/Linux 4.13.0-21-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

 * Ubuntu's Kubernetes 1.14 distributions can bypass Docker and use containerd
   directly, see https://bit.ly/ubuntu-containerd or try it now with

     snap install microk8s --classic

41 packages can be updated.
27 updates are security updates.


Last login: Sun May  5 04:40:54 2019 from 192.168.33.20
vagrant@server1:~$ logout
vagrant@centos:~$ ssh 192.168.33.40
Last login: Sun May  5 04:40:54 2019 from 192.168.33.20
[vagrant@server2 ~]$ logout

Step 5:

Here we will see few Ansible command for software installation and configuration. Earlier of this article I told you that in this tutorial I only show Ansible command line  configuration not play-book implementation.

Command for checking all hosts status:-

Include both server1 and server2 under a host group named servers:-

sudo vim /etc/ansible/hosts
# Group name servers
[servers]
## server1 ip
192.168.33.30
## server2 ip
192.168.33.40
vagrant@centos:~$ ansible servers -m ping
192.168.33.30 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.33.40 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

Here both servers ping using one Ansible command. Boths servers are ping and returned pong. That mean they are running ok.

Using -a argument:-

ansible servers -a "df -h"

From output we can see the disk and file system informations:-

92.168.33.30 | SUCCESS | rc=0 >>
Filesystem                    Size  Used Avail Use% Mounted on
udev                          479M     0  479M   0% /dev
tmpfs                          99M  7.4M   92M   8% /run
/dev/mapper/vagrant--vg-root   62G  1.4G   58G   3% /
tmpfs                         495M     0  495M   0% /dev/shm
tmpfs                         5.0M     0  5.0M   0% /run/lock
tmpfs                         495M     0  495M   0% /sys/fs/cgroup
vagrant                       234G  216G   18G  93% /vagrant
tmpfs                          99M     0   99M   0% /run/user/1000

192.168.33.40 | SUCCESS | rc=0 >>
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   41G  1.1G   40G   3% /
devtmpfs                 486M     0  486M   0% /dev
tmpfs                    497M     0  497M   0% /dev/shm
tmpfs                    497M  6.6M  490M   2% /run
tmpfs                    497M     0  497M   0% /sys/fs/cgroup
/dev/sda1               1014M  153M  862M  16% /boot
/dev/mapper/centos-home   20G   33M   20G   1% /home
vagrant                  234G  216G   18G  93% /vagrant
tmpfs                    100M     0  100M   0% /run/user/1000

seeing servers date time:-

vagrant@centos:~$ ansible servers -a "date"
192.168.33.30 | SUCCESS | rc=0 >>
Mon May  6 10:32:54 UTC 2019

192.168.33.40 | SUCCESS | rc=0 >>
Mon May  6 10:32:55 UTC 2019

Installing software packages on both servers:-

Since Centos and Ubuntu use different package manager, in this case we have to  execute Ansible command individually in each servers.

For server1-UbuntuOS- apt package manager:-

vagrant@centos:~$ ansible 192.168.33.30 -b -m apt -a "name=vim state=present"

output:

192.168.33.30 | SUCCESS => {
    "cache_update_time": 1557031056, 
    "cache_updated": true, 
    "changed": true
}

 

For server2-CentOS- yum package manager:-

 ansible 192.168.33.40 -b -m yum -a "name=vim state=present"

Output:

192.168.33.40 | SUCCESS => {
    "changed": true, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "2:vim-enhanced-7.4.160-5.el7.x86_64 providing vim installed"
    ]
}

So that all for today. I hope this tutorial will help you to understand the Ansible concepts in a practical approach. In future I’ll write on more Ansible concept like yaml, Ansible-playbook, Roles, provision of different architectures using Ansible, collaboration of Ansible with other DevOps tools and so on. Stay Tuned in www.99coding.club. Bye till then.

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2 thoughts on “Ansible: Step by step Installation and configuration on vagrant machine.

  1. Hello this is kind of of off topic but I was wondering if blogs use WYSIWYG editors or if you have
    to manually code with HTML. I’m starting a blog soon but have no coding knowledge so I wanted to get guidance from someone
    with experience. Any help would be enormously appreciated!

    1. This is wordpress powered blog. If you have no coding knowledge better go for hire a developer to develop a blog site for you.

Leave a Reply

Your email address will not be published. Required fields are marked *