What Is Ansible ?
Ansible Is Configration Management Tool .
Application Deployment, And Task Automation .
We Using Ansile Update And Install Software And Change The Files .
Ansible Architecture
Ansible Is Master And Slave Architecture
Ansible Connect With Multiple Slave Server And Do The Changes Using SSH
Ansible Use The SSH Protocol For The Connect With Slave Server
We Create PlayBook File In Our Master Server And That File Mention Our Changes
We Also Create Inventory File For Connection With Our Other Salve
We Mention That Inventory File Our Slave Server Ip
In Our Master Node All Ready Present on Inventory File PATH:- /etc/hosts
Ansible Installation On Ubuntu
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible
ansible --version
How To Create Our Own Inventory File ?
nano myhost.ini
- Content For Inventory File
[localhost]
127.0.0.1
Create A PlayBook For Install Nginx And Start The Nginx..!!
- Create A Directory For Playbook
mkdir Ansible-Playbook
Create Yaml File For Install Nginx And Start
nano nginx-install.yml
Add The Code
- name: Install Nginx Locally
hosts: localhost
become: yes # Run tasks as sudo
tasks:
# This Task For Install Nginx...!!
- name: Install Nginx
apt:
name: nginx
state: present
# This For Start Our Nginx Service..!!
- name: Start and Enable Nginx Service
systemd:
name: nginx
state: started
enabled: yes
- Run This PlayBook
ansible-playbook nginx-install.yml
- OutPut :-
Create PlayBook For Stop Nginx And Install Apache And Start Apache
nano Apache-Install.yml
- Write Code For Appache
- name: Install Nginx Locally
hosts: localhost
become: yes # Run tasks as sudo
tasks:
# Here Install Our Apache..!!
- name: Install Apache2
apt:
name: apache2
state: present
# Here Stop Our Running Nginx..!!
- name: stop nginx
service:
name: nginx
state: stopped
# Here Start Apache..!!
- name: Start Apache2
service:
name: apache2
state: started
- Run This playBook Using Our Own Inventory File
ansible-playbook -i myhost.ini Nginx-Install.yml
- Output :-
Create A PlayBook For Lemp Stack
nano lemp-install.yml
- code For Lamp Installation
- name: install LEMP on localhost
hosts: localhost
become: true
connection: local
tasks:
- name: stop apache service
service:
name: apache2
state: stopped
- name: install nginx
apt:
name: nginx
state: present
- name: start nginx service
service:
name: nginx
state: started
- name: install php
apt:
name: php
state: present
- name: install php8.3-fpm
apt:
name: php8.3-fpm
state: present
- name: start php-fpm service
service:
name: php8.3-fpm
state: started
- name: install mysql
apt:
name: mariadb-server
state: present
- name: start mysql service
service:
name: mariadb
state: started
- name: restart nginx service
service:
name: nginx
state: restarted
- name: restart php-fpm service
service:
name: php8.3-fpm
state: restarted
- name: restart mysql service
service:
name: mariadb
state: restarted
- Run The PlayBook List
ansible-playbook -i myhost.ini lemp-install.yml
OutPut :-
Create A Playbook For Lemp Stack Install And Restart Using With_Item..!!
- Create A PlayBook
nano Lemp-With-Item.yml
- Code For This PlayBook
- name: myplay1
hosts: localhost
become: true
connection: local
tasks:
- name: install LEMP
apt:
name: "{{ item }}"
state: present
with_items:
- nginx
- php
- php8.3-fpm
- mariadb-server
- name: start all service
service:
name: "{{ item }}"
state: started
with_items:
- nginx
- php8.3-fpm
- mariadb
- name: restart all service
service:
name: "{{ item }}"
state: restarted
with_items:
- nginx
- php8.3-fpm
- mariadb
- Run This PlayBook
ansible-playbook -i myhost.ini Lemp-With-Item.yml
- output :-
Create A One File And Directory And Move This Created File In We Created Directory Using PlayBook
- Create PlayBook
nano File-Dir-Create.yml
- Code For This PlayBook
- name: myplay1
hosts: localhost
become: true
connection: local
tasks:
- name: Create A File..!!
file:
name: Ansible.txt
state: touch
- name: Create A Directory..!!
file:
name: AnsibleDir
state: directory
- name: Move Created File In Created Directory..!!
copy:
src: Ansible.txt # Here Mention Which File Move..!!
dest: AnsibleDir/ # Here Mention Where Move Our File..!!
- Run This PlayBook
ansible-playbook -i myhost.ini File-Dir-Create.yml
- OutPut :-