Day 59 Task: Ansible Project

Day 59 Task: Ansible Project

ยท

4 min read

Project Title: Revolutionizing Nginx Deployment through Automated Ansible Playbooks

Project Description: In this project, I leveraged Ansible playbooks to automate the deployment of Nginx web servers across multiple servers. The primary goal was to efficiently manage the installation, configuration, and deployment of Nginx, simplifying the process of hosting a web application.

Key Tasks:

  1. Installing Nginx:

    • The Ansible playbook begins by ensuring that the server's apt package cache is up to date. It then installs Nginx using the apt module.
  2. Starting Nginx Service:

    • After installation, the playbook starts the Nginx service and ensures that it is enabled to run on system boot.
  3. Deploying the Website:

    • The playbook deploys a website by copying the index.html file to the appropriate directory (/var/www/html), making the web content accessible via Nginx.
  4. Verification:

    • The project emphasizes the importance of verification. After running the Ansible playbook, it is crucial to check and ensure that the website is correctly deployed on all target servers.

Benefits and Outcomes: Through the implementation of this Ansible playbook, we achieved several key benefits:

  • Automation: The playbook automates the entire process of Nginx installation, configuration, and website deployment, reducing manual intervention.

  • Consistency: Ansible ensures consistent configurations across multiple servers, eliminating configuration drift.

  • Efficiency: Rapid deployment of Nginx and web content simplifies the hosting of web applications.

  • Scalability: This approach can be scaled to manage a large number of servers efficiently.

Learning and Expertise: This project underscores the power of Ansible as an automation tool for managing infrastructure and application deployment. It showcases the ease and effectiveness of Ansible playbooks in simplifying complex tasks.

Conclusion: In conclusion, this project highlights the efficiency and simplicity of automating Nginx deployment using Ansible playbooks. It not only streamlines the process but also ensures consistent and reliable results across multiple servers. Ansible continues to be a valuable tool in the DevOps arsenal for automating and managing infrastructure.

๐Ÿ”ถ Let's dive into the project.

๐Ÿ”ถ Task: Deploy a webpage using Ansible

  • Create 3 EC2 instances. Make sure all three are created with the same key pair.

  • Install Ansible on a host server.

        sudo apt-add-repository ppa:ansible/ansible 
        sudo apt update -y
        sudo apt install ansible -y
    
    1. Assign the values in the file as shown in the below screenshot and save.

        [servers]
        server1 ansible_host=<server1_ip>
        server2 ansible_host=<server2_ip>
        server3 ansible_host=<server2_ip>
      
        [all:vars]
        ansible_user=ubuntu
        ansible_ssh_private_key_file=~/.ssh/id_rsa
        ansible_python_interpreter=/usr/bin/python3
      
    2. Let's verify the inventory that we have created.

      ansible-inventory --list -y

        ansible-inventory --list -y -i /etc/ansible/hosts
      

We have created the three servers and set up the server to master the above task.

  1. Create a public key on the master server and copy the key using the ssh-keygen command.

      ssh-keygen
    

  2. We can see id_rsa.pub which is the public key of the master server.

  3. Copy the above public key of the master to both the node servers.

  • Try a ping command using Ansible to the Nodes.

    1. Now, use the ping command with the input of inventory file to it so that it will ping both the node servers.

        ansible all -a "free" -u ubuntu
      

  • Create a playbook to install Nginx.

        ---
        - name: Install Nginx
          hosts: servers
          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
    

        #To run nginx playbook
        ansible-playbook nginx.yml
    

  • Deploy a sample webpage using the ansible-playbook.

    Now update the nginx.yml file to include indext.HTML

        ---
        - name: Install Nginx
          hosts: servers
          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
    

    Now run ansible-playbook nginx.yml

      ansible-playbook nginx.yml
    

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

  • for checking the deployed webpage use any server <public-ip>

In conclusion, Ansible playbooks provide an incredibly efficient and powerful way to manage and deploy infrastructure and applications. As we've learned, deploying a simple web app using Ansible is not just a good project; it's a testament to the ease and effectiveness of this automation tool.

Happy Learning :)

If you find my blog valuable, I invite you to like, share, and join the discussion. Your feedback is immensely cherished as it fuels continuous improvement. Let's embark on this transformative DevOps adventure together! ๐Ÿš€ #devops #90daysofdevop #AWS #Ansible #Ansible-playbook

ย