- Session 1 - Introduction & Setting up of Ansible
- Session 2 - Complete overview of Playbook
- Session 3 - Ansible configuration file & Ansible Facts
- Session 4 - Installing & Configuring of Docker with Ansible
- Session 5 - Template & Jinja2
- Session 6 - Setting up load balancer & Ansible Roles
Ansible is an open-source automation tool used for configuration management, application deployment, and task automation. It is agentless and uses SSH to communicate with remote machines.
Before starting, remove any unnecessary software like firefox
, php
, and httpd
.
sudo yum remove -y firefox php httpd
yum remove
→ Removes the specified packages.-y
→ Automatically confirms removal without asking.firefox php httpd
→ List of packages to be removed.
rpm -q firefox php httpd
- This command checks if the packages are still installed.
- If a package is not installed, it will return:
package <name> is not installed
.
Ansible uses an inventory file (/etc/ansible/hosts
) to manage hosts.
gedit /etc/ansible/hosts
[local]
localhost ansible_connection=local
[webservers]
192.168.1.10
192.168.1.11
[local]
→ Defines a local group.localhost ansible_connection=local
→ Configures Ansible to run locally.[webservers]
→ Defines a group for web servers.- IPs
192.168.1.10
&192.168.1.11
are the managed nodes.
To list all hosts in inventory:
ansible all --list-hosts
Ad-hoc commands are one-time Ansible commands that don't require a playbook.
ansible all -m ping
localhost | SUCCESS => {
"ping": "pong"
}
ansible all -m command -a "whoami"
ansible all -m command -a "date"
ansible all
→ Runs the command on all hosts in the inventory.-m command
→ Uses the command module to execute shell commands.-a "whoami"
→ Executes thewhoami
command to check the logged-in user.
ansible all -m package -a "name=httpd state=present"
ansible all -m package -a "name=dialog state=present" \
--become --become-method=sudo --become-user=root --ask-become-pass
State | Description |
---|---|
present |
Ensures the package is installed. |
absent |
Ensures the package is removed. |
latest |
Ensures the package is installed with the latest version. |
installed |
Alias for present . |
removed |
Alias for absent . |
Module | Purpose |
---|---|
ping |
Checks connectivity with hosts. |
command |
Runs shell commands on remote hosts. |
package |
Installs, removes, or manages packages. |
copy |
Copies files from the control node to remote hosts. |
service |
Manages services (start, stop, restart, enable). |
firewalld |
Configures firewall rules. |
Playbooks are YAML files that define a set of tasks to automate complex processes.
- hosts: webservers
become: yes
tasks:
- name: Install Apache
package:
name: httpd
state: present
- name: Start Apache Service
service:
name: httpd
state: started
enabled: yes
hosts: webservers
→ Applies tasks to thewebservers
group.become: yes
→ Uses root privileges.package:
→ Installs Apache (httpd
).service:
→ Starts and enables Apache.
ansible-playbook apache.yml
Command | Description |
---|---|
ansible all -m ping |
Check if hosts are reachable |
ansible all -m command -a "whoami" |
Check the user running Ansible on remote machines |
ansible all --list-hosts |
Show all managed hosts |
ansible all -m command -a "date" |
Get the current date on all hosts |
ansible all -m package -a "name=httpd state=present" |
Install httpd using Ansible |
ansible all -m package -a "name=dialog state=present" --become --ask-become-pass |
Install dialog with sudo privileges |
ansible-playbook apache.yml |
Run an Ansible playbook |