Ansible is an open-source automation tool that can help you with configuration management, application deployment, task automation, and even IT orchestration.
- A Linux-based system (e.g., Ubuntu, CentOS)
- Python installed (Ansible is written in Python)
sudo apt update
sudo apt install software-properties-common
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt install ansible
sudo yum install epel-release
sudo yum install ansible
- Inventory File: Before you can use Ansible, you need to have an inventory file. This file lists all the hosts you want to manage with Ansible. Create a file named hosts.ini and add your servers:
[server]
192.168.1.10
192.168.1.11
[db]
192.168.1.20
- Ping All Hosts: To check if Ansible can communicate with all the servers listed in the inventory file, use the ansible command:
ansible -i hosts.ini all -m ping
- Ad-hoc Commands: You can run commands on your servers without writing a playbook. For example, to check the disk space on all web servers:
ansible -i hosts.ini web -m shell -a 'df -h'
- Playbooks: Playbooks are the heart of Ansible. They are written in YAML and describe the tasks you want Ansible to execute. Here's a simple playbook to ensure the latest version of Apache is installed:
---
- hosts: web
tasks:
- name: Ensure Apache is installed
apt:
name: apache2
state: latest
To run the playbook:
ansible-playbook -i hosts.ini playbook.yaml