author avatar

soniya.rayabagi

Tue Jan 23 2024

  • Ansible Provisioning :* Example : Setting up Ansible Provisioning for Nginx Installation on Vagrant:

Step 1: Install Ansible on Your Machine :

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

Step 2: Go to your server's /etc/hosts file and add the hostname and the IP address of the host:

hostname    : vagrant 
hostname -I : 10.0.2.15.

Step 3: As Ansible works on the agentless architecture of using SSH to communicate with its hosts, set-up the ssh keys:

ssh-keygen 
cat ~/.ssh/id_rsa.pub
ssh-copy-id root@10.0.2.15.

Step 4: Add the Vagrant SSH Key inside authorized_keys:

sudo nano ~/.ssh/authorized_keys

Step 5: Configure Ansible Hosts , Edit the /etc/ansible/hosts file and add the hostname:

sudo nano /etc/ansible/hosts
vagrant 

Step 6: Verify Connectivity:

ansible -m ping all

output ---> 
vagrant | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"

Step 7: Write Ansible Playbook for Nginx Installation:

vi nginx-playbook.yml

Step 8: Run the Ansible Playbook:

ansible-playbook nginx-playbook.yml

output :

PLAY [Install and configure Nginx] *************************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************
ok: [vagrant]

TASK [Update apt package cache] ****************************************************************************************************************************************************************************
changed: [vagrant]

TASK [Install Nginx] ***************************************************************************************************************************************************************************************
ok: [vagrant]

TASK [Start Nginx service] *********************************************************************************************************************************************************************************
ok: [vagrant]

PLAY RECAP *************************************************************************************************************************************************************************************************
vagrant                    : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0