Day 59: Ansible Project 🔥

Day 59: Ansible Project 🔥

·

4 min read

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 and Master node. make sure all three are created with same key pair

Install Ansible in host server

  • In your Ansible-Master Install Ansible using this comands
sudo apt-add-repository ppa:ansible/ansible
sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get install ansible -y

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

  • To connect to the host file, you need the private key on your 'Ansible-Master'.

  • To connect using your private key, you need to create a folder where you will store the private key to connect to the other servers.

mkdir keys && cd keys
  • Open your local machine, like your terminal, go to the 'Downloads' folder, find your 'ansible-key,' and then you'll want to transfer that key to your 'Ansible-Master' server using the SSH command.
ssh -i "ansibile-key.pem" ubuntu@ec2-3-82-116-205.compute-1.amazonaws.com
  • To do this, copy the 'Ansible-Master' SSH command and paste it into your local machine's terminal. Then, modify the command: replace 'SSH' with 'scp,' specify your key name after '.pem,' and at the end of the command, paste the present working directory of your 'Ansible-Master' using colon :
scp -i "ansibile-key.pem" ansibile-key.pem ubuntu@ec2-3-82-116-205.compute-1.amazonaws.com:/home/ubuntu/keys

  • If you run 'ls' on your 'Ansible-Master' server, you'll see your private key listed.

Access the inventory file using sudo vim /etc/ansible/hosts

  • Open the host file using the command provided and read your host file carefully, navigate to a specific section, for instance, 'ex:2', there you'll define your groups such as 'dev group' and 'prd group' for your WebPage. You can define them using square brackets '[]' and the corresponding variables like 'ansible_host=your server's IP address'.
sudo vim /etc/ansible/hosts

  • I think you now understand how to configure your servers in the Ansible host file. You can create and organize them into groups, similar to the image above. This enables you to list multiple servers using their respective IP addresses, and now you know how to set this up.

  • Now your private key's location in your 'Ansible-Master' server should be known to the Ansible variable. Navigate to your Ansible host file using the command and update it accordingly.

  • Next you have written your server's IP address and variables. At the bottom, write the private key file like this: [all:vars],ansible_python_interpreter=/usr/bin/python3,ansible_user=ubuntu, ansible_ssh_private_key_file=/<file>, then come back to your host file.

Create a playbook to install Nginx

  • Create a playbook yaml file to install nginx
# vim install-nginx-playbook.yml
-------------------------------------------------------------------------------
-
  name: Install nginx and creating a static cool page
  hosts: all
  become: yes
  tasks:
    - name: Install nginx
      apt:
        name: nginx
        state: latest

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

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

Deploy a sample webpage using the ansible playbook

  • Create a index HTML file to deploy a sample webpage.

  • In my case iam creating a webpage of my resume.

  • Here is main point that we are a devops engineer.To create a index HTML file we need a developer so for that will use ChatGPT😉.

  • As for ChatGPT like this Act as a web developer create a html file .

# vim index.html
-------------------------------------------------------------------------------
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>My Resume</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      background: linear-gradient(135deg, #3498db, #9b59b6);
      color: white;
    }

    .container {
      max-width: 800px;
      text-align: left;
      padding: 20px;
      border-radius: 10px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
      background-color: rgba(255, 255, 255, 0.1);
    }

    h1,
    h2 {
      border-bottom: 2px solid #fff;
      padding-bottom: 5px;
      margin-bottom: 15px;
    }

    ul {
      list-style-type: none;
      padding: 0;
    }

    .button {
      display: inline-block;
      padding: 10px 20px;
      background-color: #fff;
      color: #3498db;
      text-decoration: none;
      border-radius: 5px;
      transition: background-color 0.3s ease, color 0.3s ease;
    }

    .button:hover {
      background-color: #eee;
      color: #2471a3;
    }
  </style>
</head>

<body>
  <div class="container">
    <h1>Your name</h1>
    <p>DevOps Engineer</p>

    <h2>About Me</h2>
    <p>I recently graduated bachelor's degree. Eager to contribute my skills and learn in DevOps.</p>

    <h2>Education</h2>
    <ul>
      <li>Your collage name-passed year</li>
      <li>Train With Shubham - Online Platform - 2023</li>
      <!-- Add more education details here -->
    </ul>

    <h2>Skills</h2>
    <ul>
      <li>LINUX</li>
      <li>GIT-GITHUB</li>
      <li>DOCKER</li>
      <li>JENKINS</li>
      <li>KUBERNETES</li>
      <li>ANSIBLE</li>
      <li>TERRAFORM</li>
      <li>MONITORING</li>
    </ul>

    <h2>Projects</h2>
    <ul>
      <li>Your project-1.</li>
      <li>Your project-2</li>
      <li>Your project-3</li>
    </ul>

    <h2>Certifications</h2>
    <ul>
      <li>DevOps Certification - Issuing Body - 2023</li>
      <li>Python Course - Issuing Body - 2023</li>
      <!-- Add more certifications here -->
    </ul>

    <!-- Call-to-Action Button -->
    <a href="mailto:your mail-id" class="button">Contact Me</a>
  </div>
</body>

</html>
  • Now we should deploy this Web Page using this command
ansible-playbook -v install-nginx-playbook.yml
  • Copy your any one of your servers IP address and past it on your newtab and see Result


Happy Learning

Thanks For Reading! :)

-SriParthu💝💥