How to Install / Setup Ansible in Linux ?

ANSIBLE Installation / Setup :       

Follow the below steps to setup / configure ansible on Linux box and test the setup.   

1. Install and configure EPEL Repo                         
2. Install Ansible Package
3. Create ansbile user and
4. Enable ssh password less login
5  Configure inventry file for ansible
6. Create an Ansible Playbook
7. Run the Playbook and test

STEP 1:  Install EPEL Repo on a CentOS and RHEL 7.x

Method 1 : 
           #yum update
           #yum install epel-release
           #yum repolist

Method 2 : 
          #yum update
           #yum install epel-release-latest-7.noarch.rpm
           #yum repolist
Install EPEL Repo on RHEL8.X Linux Box:

      # yum install
To check the epel repo by using below command
    #yum --disablerepo="*" --enablerepo="epel" list available | grep 'package'
STEP 2: Install Ansible Packages

A. Install ansible on a RHEL/CentOS Linux :

   #yum update
   #yum install ansible  
   #ansible --version

B. Install ansible on a Debian/Ubuntu Linux :

   #apt-get install software-properties-common
   #apt-add-repository ppa:ansible/ansible
   #apt-get update
   #apt-get install ansible
   # ansible --version

C. Install Ansible Using Python (PIP)

   #pip install ansible
D. Install the latest version of ansible using source code

  #git clone git://
  #cd ./ansible
  #source ./hacking/env-setup  
  When running ansible from a git checkout, one thing to remember is that you will need to setup your environment everytime you want to use it, or you can add it to your bash rc file:
  # echo "export ANSIBLE_HOSTS=~/ansible_hosts" >> ~/.bashrc
  # echo "source ~/ansible/hacking/env-setup" >> ~/.bashrc
STEP 3: Configure User for Ansible

  #useradd admin
  #passwd admin

  Type 'i' to enter input mode and add the following to the end of the file. Type '[ESC]:wq' to save your changes.

 STEP 4: Configure our Admin User for SSH Access
   Login as "admin" user and run below commands.
   #ssh-keygen -t rsa
   #ssh-copy-id ( copy the key file to target nodes)
   To Test the connection
   #ansible -m ping
 STEP 5: Create an Ansible Inventory  
 Ansible requires an inventory list so it can identify your managed nodes. To add our managed node to the inventory, we need to login to our Control node as the admin user. Next, we will add a new inventory file. Make sure you are logged onto the Control node as the admin user.
 Note : By default ansible use the host file at -  /etc/ansible/hosts.

  #vi /home/admin/inventory

  Type 'i' to enter insert mode and add the managed node hostname to the inventory file.

  Next, type '[ESC]:wq' to save the file.
 STEP 6: Create an Ansible Playbook
  To test our configuration, we will create a simple playbook to install Nginx on our managed node. First, we will create and open a new file.
  #vi /home/admin/install-nginx.yml
  add below lines and save the file
  ###  Install nginx

  - hosts :
    become: yes
  name: Install epel package
    name: epel-release
    state: installed
  name: Install nginx package
    name: nginx
    state: intalled
  STEP  7: Run the Playbook and test
   Running a playbook is rather easy. We use the "ansible-playbook" command and then specify the inventory file with the "-i" option followed by the path to the playbook. 
   Make sure you are logged onto the control node as the admin user.

  #ansible-playbook -i /home/admin/inventory /home/admin/install-nginx.yml


Post a Comment