Ansible Project ๐Ÿ”ฅ

This is Day 59 of #90daysofdevops challenge

ยท

3 min read

Ansible Project ๐Ÿ”ฅ

Ansible playbooks are amazing, as you learned yesterday. What if you deploy a simple web app using Ansible, sounds like a good project, right?

Task-01:

-> Create 3 EC2 instances.

-> Install Ansible in a host server.

  • Now, login to the Master server and run this command sudo apt-add-repository ppa:ansible/ansible

  • Now install Ansible using the commands given below:
sudo apt update 
sudo apt install ansible

  • You can verify the installation by checking the Ansible version:
ansible --version

-> Copy the public key of master server where Ansible is setup

  • Simply use ssh-keygen in the Master as well as the Node server and copy the Master server id_rsa.pub which is a public key and paste it on authorized_keys in both the Node-1 and Node-2 servers.

  • Use vim authorized_keys to edit the file and paste the Master id_rsa.pub in it. Do it in both the Nodes.

  • After this check whether you can access Node-1 or Node-2 from the Master node. Simply use ssh (Node Private Ip address)

  • We successfully accessed the Node-1 and Node-2

-> Now make an inventory file in the Master server and add the Private Ip address of both Node-1 and Node-2.

-> Try ping command using ansible to the Nodes.

  • Now use the command given below to ping the nodes:
ansible -i inventory all -m ping

We can see both pings are successful which indicates servers are in active states.

-> Create a playbook to install Nginx

  ---
  - name: Install Nginx
    hosts: all
    become: yes

    tasks:
      - name: Update apt package cache
        apt:
          update_cache: yes

      - name: Install Nginx
        apt:
          name: nginx
          state: present

      - name: Start Nginx
        service:
          name: nginx
          state: started
  • Now use this command to run the playbook.
  ansible-playbook install_nginx.yml -i (Path-of-inventory)

-> Deploy a sample webpage using the ansible-playbook.

  • Create an index.html file.
<!DOCTYPE html>
<html>

<head>
  <title>Our Company</title>
</head>

<body>

  <h1>Welcome to Our Company</h1>
  <h2>Web Site Main Ingredients:</h2>

  <p>Pages (HTML)</p>
  <p>Style Sheets (CSS)</p>
  <p>Computer Code (JavaScript)</p>
  <p>Live Data (Files and Databases)</p>

</body>
</html>
  • Now, update the install_nginx.yml file.
  ---
  - name: Install Nginx
    hosts: all
    become: yes

    tasks:
      - name: Update apt package cache
        apt:
          update_cache: yes

      - name: Install Nginx
        apt:
          name: nginx
          state: present

      - name: Start Nginx
        service:
          name: nginx
          state: started
          enabled: yes

      - name: Deploy website
        copy:
          src: index.html
          dest: /var/www/html

  • Let's run this playbook.

  • Check and verify the website deployed on all the servers.

"Thank you for enjoying my DevOps blog! Your positive response fuels my passion to dive deeper into technology and innovation.

Stay tuned for more captivating DevOps articles, where we'll explore this dynamic field together. Follow me on Hashnode and connect on LinkedIn (https://www.linkedin.com/in/som-shanker-pandey/) for the latest updates and discussions.

Did you find this article valuable?

Support Som Pandey's blog by becoming a sponsor. Any amount is appreciated!

ย