Ansible

·

2 min read

What is Ansible?

Ansible is a configuration management tool used to configure multiple machines/servers at the same time without manually input to each one of them

We are having host machine and 2 node machines (Ubuntu and Redhat) Let us try to ping those 2 machines via ansible-ad-hoc. Before that Let us install Ansible in the host machine.[This lab is built using a virtual box]

  • Let us Create a user account with a sudo access
useradd -m -d /home/ansible -s /bin/bash -G sudo ansible
echo "ansible ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/ansible
  • Install Python
sudo apt-get install python*
  • Install Ansible
sudo apt-get install python*
  • Check ansible version
ansible --version

Let us create a Project directory and inside that create a file named ansible.cfg

mkdir ansible-demo
cd ansible-demo
vi ansible.cfg
[defaults]
inventory = ./hosts
remote_user = devops
ask_pass = false

Create an inventory file named hosts in the same directory

ubuntu ansible_host=192.168.x.x 
redhat ansible_host=192.168.x.x

Now create an ssh key using ssh-keygen

ssh-keygen -t rsa -b 4096 -C ansible@your_host_ip_address

Copy ssh public key to ubuntu machine

ssh-copy-id -i ~/.ssh/id_rsa.pub devops@192.168.x.x

Now again copy the ssh public key to the red hat machine

ssh-copy-id -i ~/.ssh/id_rsa.pub redhat@192.168.x.x

Configure ssh key in myinventory

[ubuntu]
devops@devops ansible_host=192.168.x.x ansible_ssh_private_key=/home/ansible/.ssh/id_rsa ansible_user=devops
[redhat]
redhat@redops ansible_host=192.168.x.x ansible_ssh_private_key=/home/ansible/.ssh/id_rsa ansible_user=redops

Check connection

ansible all -m ping -i myinventory 
devops@devops | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}
redhat@redops | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"

Both machines are reachable using the ansible-ad-hoc command.In next post will try to deploy a website using Ansible-playbook