Setup Ansible on a Linux system
Setup Ansible with Homebrew on macOS
Ansible Setup Overview
Ansible is an open-source automation platform designed to simplify the management of IT infrastructure. It helps automate tasks such as configuration management, application deployment, and cloud provisioning across multiple servers, making it essential for DevOps practices. The tool uses a simple, human-readable language called YAML to define tasks in “playbooks,” and communicates with remote machines over SSH.
Current stable version: Ansible-core 2.20.2 (released January 29, 2026)
Before installing Ansible, ensure your system meets the following requirements:
Install Ansible
Ansible is available for most Linux distributions, macOS, and Windows (via WSL). Multiple installation methods are available depending on your needs:
Method 1: Using pip (recommended for latest version)
# Create a virtual environment (recommended)
python3 -m venv ansible-env
source ansible-env/bin/activate # On Windows: ansible-env\Scripts\activate
pip install ansible
# Or install globally (not recommended)
pip install ansible
Method 2: Using package managers
sudo apt update && sudo apt install ansiblesudo dnf install ansible or sudo yum install ansiblebrew install ansibleMethod 3: Using pipx (recommended for global installation)
pip install pipx
pipx install ansible
Verify Installation
Check the installed Ansible version to confirm the installation was successful:
ansible --version
You should see output similar to:
ansible [core 2.20.2]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /path/to/ansible/lib/python3.x/site-packages/ansible
executable location = /path/to/ansible/bin/ansible
python version = 3.12.x
Configure SSH Access
Ansible requires SSH access to manage remote systems. Set up passwordless SSH login between the Ansible control node and remote machines using SSH keys:
# Generate an SSH key pair (skip if you already have one)
ssh-keygen -t rsa -b 4096 -C "ansible@control-node"
# Copy the public key to remote hosts
ssh-copy-id user@remote_host
# Test SSH connection
ssh user@remote_host
Create an Inventory File
Ansible uses an inventory file to define the hosts it will manage. This file can be a simple list of IPs or hostnames organized into groups.
Example inventory file (~/ansible/inventory):
[webservers]
web1.example.com
web2.example.com
192.168.1.10
192.168.1.11
[databases]
db1.example.com
192.168.1.20
[production:children]
webservers
databases
[all:vars]
ansible_user=ansible_user
ansible_ssh_private_key_file=~/.ssh/id_rsa
Configure Ansible (Optional)
Create a custom ansible.cfg file to override default settings:
# Create ansible.cfg in your project directory
cat > ansible.cfg << EOF
[defaults]
inventory = ./inventory
remote_user = ansible_user
host_key_checking = False
gathering = smart
fact_caching = memory
timeout = 30
forks = 20
[privilege_escalation]
become = True
become_method = sudo
become_user = root
EOF
Test Connectivity
After configuring the inventory, test connectivity to all the hosts using the Ansible ping module:
# Test with specified inventory
ansible all -i ~/ansible/inventory -m ping
# Test with default inventory (if configured in ansible.cfg)
ansible all -m ping
This should return a “pong” from each reachable host.
Write Your First Playbook
Playbooks are YAML files that define a series of tasks to be executed on remote machines. For example, a playbook can install software, update configurations, or start services.
Example of a simple playbook to install and start Apache:
---
- name: Install and configure Apache web server
hosts: webservers
become: yes
tasks:
- name: Install Apache
package:
name: apache2 # Use 'httpd' for RHEL/CentOS
state: present
- name: Start and enable Apache service
service:
name: apache2 # Use 'httpd' for RHEL/CentOS
state: started
enabled: yes
- name: Create a simple index page
copy:
content: |
<html><body><h1>Hello from Ansible!</h1></body></html>
dest: /var/www/html/index.html
owner: www-data
group: www-data
mode: '0644'
Run Playbooks
Once the playbook is created, it can be executed to automate tasks across the defined hosts:
# Run playbook with specified inventory
ansible-playbook -i ~/ansible/inventory playbook.yml
# Run playbook with extra variables
ansible-playbook -i ~/ansible/inventory playbook.yml --extra-vars "version=2.4.40"
# Run playbook in check mode (dry run)
ansible-playbook -i ~/ansible/inventory playbook.yml --check
ssh-agent to manage your SSH keysControlMaster and ControlPersist in your SSH config for faster connectionsforks parameter in ansible.cfg to control parallelismansible-vault to encrypt sensitive data in your playbooksAny questions?
Feel free to contact us. Find all contact information on our contact page.