Ansible Project ๐Ÿ”ฅ

ยท

3 min read

Ex1: Deploying a simple web app using ansible

Step1:

  • Create 3 EC2 instances, make sure all three are created with the same key pair

Step2:

  • Install Ansible on one of the servers to make it control node

    1. We shall create a bash script named install_ansible.sh and use it to install ansible
    #!/bin/bash

    sudo apt-add-repository ppa:ansible/ansible
    sudo apt update
    sudo apt install ansible

    echo "ansible installed"

  1. Let us check for the host file to confirm if Ansible is installed

  1. copy the private key from local to the Host server (Ansible_host) at (/home/ubuntu/.ssh)

  2. Let us first add the host servers to Ansible on which the Ansible will install nginx

Ansible is successfully installed and configured

Step3:

  • Now, let us create playbook to install nginx
-
 name: Playbook to install nginx
 hosts: servers
 become: true
 tasks:
   - name: install nginx
     apt:
      name: nginx
      state: latest
   - name: start nginx
     service:
      name: nginx
      state: started
      enabled: yes

Step4:

  • Once, the playbook is created run the command ansible-playbook install_nginx.yml in the folder where the playbook is present

Step5:

  • Playbook has completed the task, now let us ssh and check in any of the servers if nginx is installed

Hence, we have successfully installed nginx through ansible-playbook

Ex2:Deploy a sample webpage using the ansible-playbook

Step1:

  • Let us do this in a specific server called the prod server instead of all the servers. To do this we shall add the server to an inventory file.

      [servers]
    
      prod_server ansible_host=54.191.83.157
    
      [all:vars]
    
      ansible_python_interpreter=/usr/bin/python3
      ansible user=ubuntu
      ansible_ssh_private_key_file=/home/ubuntu/.ssh/1_new_key_pair_for_ansible.pem
    

Step2:

  • Let us check if this server is added to the prod inventory list by using the command ansible -i prod_inventory servers -m ping which pings only servers in prod_ineventory

    Thus, the prod inventory was successfully created.

Step3:

  • Let us create a simple html webpage with the following contents in index.html

      <!DOCTYPE html>
      <html>
      <body>
    
      <h1>My First Heading</h1>
      <p>My first paragraph.</p>
    
      </body>
      </html>
    

Step4:

  • Let us create the playbook with the below contents to deploy above webpage

      -
       name: Playbook to install webpage
       hosts: servers
       become: true
       tasks:
         - name: deploy webpage
           copy:
             src: index.html
             dest: /var/www/html
    

Step5:

  • Run the playbook with the command ansible-playbook -i /home/ubuntu/ansible/inventory/prod_inventory install_webpage.yml and check for the output

Step6:

  • The playbook is completed. Let us check for the webpage in the prod server's IP. The command ansible-inventory -i /home/ubuntu/ansible/inventory/prod_inventory --list -y can be used to check the IP address of prod servers

Thus, webpage successfully deployed through ansible-playbook

ย