DEV Community

SyedAsadRazaDevops
SyedAsadRazaDevops

Posted on

💻ANSIBLE Auto Configure your server for Nginx|Reactjs and Laravel|php env on ONE click👆

Let setup the ansible:

you need one "MASTER" and multiple "SLAVE" node to control the slave configuration.

Important

  • its an configuration management tool, also open-source.
  • we used "YAML" for scripting language.
  • it implement worker on "push management" model.
  • one "Master" configure multiple servers "nodes".
  • it communicate agent/nodes throw "SSH" method.
  • script are called "PLAY BOOKS".
  • the machain where ansible is installed called "ANSIBLE SERVER".
  • the set of commands to be executed in nodes/client called " MODULE"
  • metadata of servers or agent/nodes/clients called "Inventory"
  • inventory default location will be --> /etc/ansible/host

How to connect master and slave

create an SSH-KEY and paste the same in all slave servers.
So, you connect with all different nodes through IP with same private-key.

How To Install

apt install ansible
Enter fullscreen mode Exit fullscreen mode

How To Create Inventory

[servers]
server_1  ansible_hist= 1.112.113.114

[all=vars]
ansible_python_interpreter = /usr/bin/python3
Enter fullscreen mode Exit fullscreen mode

How To Check/Verify Inventory

ansible-inventory --list -y 
ansible-inventory --list -y -i [(inventory)--> /etc/ansible/host]
Enter fullscreen mode Exit fullscreen mode

How To Ping Agent/Nodes

ping the all nodes

ansible all -m ping -i [(inventory)--> /etc/ansible/host] --private-key = ~/.ssh/ansiblekey.pem

Enter fullscreen mode Exit fullscreen mode

OR

ansible all -m ping -u root
Enter fullscreen mode Exit fullscreen mode

####some use full command to get nodes info####

ansible all -a "free -h" -i [(inventory)--> /etc/ansible/host] --private-key = ~/.ssh/ansiblekey.pem

ansible all -a "uptime" -i [(inventory)--> /etc/ansible/host] --private-key = ~/.ssh/ansiblekey.pem

ansible all -a "df -h" -i [(inventory)--> /etc/ansible/host] --private-key = ~/.ssh/ansiblekey.pem
Enter fullscreen mode Exit fullscreen mode

How To Create Play-book.yml

https://www.trainwithshubham.com/blog/ansible-playbooks

    - name: This playbook will create a file
      hosts: all
      become: true
      tasks:
       - name: creating a file
         file:
         path: /home/ubuntu/testdemo2.txt
         state: touch
Enter fullscreen mode Exit fullscreen mode
    - name: This Playbook will create a user
      hosts: all
      become: true
      tasks:
       - name: Create a user Shubham
         user: name=shubham
Enter fullscreen mode Exit fullscreen mode
    - name: This playbook will install Docker
      hosts: all
      become: true
      tasks:
       - name: Add Docker GPG apt Key
         apt_key:
         url: https://download.docker.com/linux/ubuntu/gpg
         state: present

       - name: Add Docker Repository
         apt_repository:
         repo: deb https://download.docker.com/linux/ubuntu focal stable
         state: present
       - name: Install Docker
         apt:
         name: docker-ce
         state: latest
Enter fullscreen mode Exit fullscreen mode

How To Upgrade-all Server by(command)

ansible all -m apt -a "upgrade=yes update_cache=yes cache_valid_time=86400" --become  -i [(inventory)--> /etc/ansible/host] --private-key = ~/.ssh/ansiblekey.pem
Enter fullscreen mode Exit fullscreen mode

How To Run Playbook.yml

ansible-playbook playbook.yml -i [(inventory)--> /etc/ansible/host] --private-key = ~/.ssh/ansiblekey.pem 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)