DAY16 of #60DaysDevOps Challenge|Ansible

Today i am learning about Ansible what is ansible why we use ansible and then i use ansible to remote update server

installation of ansible

Go to the instance and make three instance one is master and 2 is node

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

Installation done

Configuration

Login to master node

sudo vim /etc/ansible/hosts

Update this File Add Servers and Configuration in it and add key for access node instance

[servers]
server1 ansible_host=<public ip address node 1>
server1 ansible_host=<public ip addr node n>
[all:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_user=ubuntu
ansible_ssh_private_key_file=/home/ubuntu/key/ansible_node.pem

Now Just copy the pem file from local to instance

In the Local pc where pem file downloaded

change in ssh cmd 
scp <key name >keyname<ubuntu name >:/home/ubuntu/keys
give permission 
chmod 400 ansible.pem

Let's Ping the node by master

ansible servers -m ping

Check Free Space in node

ansible servers -a "free -h"

Let's Update all nodes without Touching

ansible servers -a "sudo apt update"

Playbook in ansible

create a playbook

mkdir playbook
cd playbook
vim nginx.yml

Paste the yml file there

---
- name: install nginx
  hosts: servers
  become: yes
  tasks:
    - name: install nginx
      apt:
        name: nginx
        state: latest
    - name: start nginx
      service:
        name: nginx
        state: started
        enabled: yes
    - name: deploy
      copy:
        src: /path/to/your/index.html   # Specify the correct path to your index.html file
        dest: /var/www/html

Create a index.html here

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Simple HTML Page</title>
</head>
<body>
    <h1>Hello, World!</h1>
    <p>This is a simple HTML page.</p>
</body>
</html>
ansible-playbook nginx.yml