Skip to content

Installing Ansible

Requirements

Python Requirements

  • Python 3.9 or later
  • pip (Python package manager)
  • virtualenv (recommended)

Operating System Support

  • Control Node: Linux, macOS, WSL2
  • Managed Nodes: Linux, Windows, macOS

Installation Methods

Using pip

# Create virtual environment
python -m venv ansible-env
source ansible-env/bin/activate

# Install Ansible
pip install ansible

# Verify installation
ansible --version

Using Package Managers

Ubuntu/Debian

# Add Ansible repository
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible

# Install Ansible
sudo apt install ansible

RHEL/CentOS

# Enable EPEL repository
sudo dnf install epel-release

# Install Ansible
sudo dnf install ansible

macOS

# Using Homebrew
brew install ansible

Post-Installation Setup

Configuration File

Create or edit /etc/ansible/ansible.cfg or ~/.ansible.cfg:

[defaults]
inventory = ./inventory
remote_user = ansible
host_key_checking = False
timeout = 30

[privilege_escalation]
become = True
become_method = sudo
become_user = root
become_ask_pass = False

SSH Key Setup

# Generate SSH key
ssh-keygen -t ed25519 -C "ansible"

# Copy key to remote hosts
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@remote-host

Testing Installation

Verify Connectivity

# Create inventory file
echo "localhost ansible_connection=local" > inventory

# Test connection
ansible all -i inventory -m ping

Basic Command Test

# Run a simple command
ansible all -i inventory -m command -a "uptime"

Next Steps

  1. Configure your inventory
  2. Write your first playbook
  3. Learn about modules
  4. Understand variables and facts

Troubleshooting

Common Issues

Python Not Found

# Install Python on remote hosts
sudo apt install python3
sudo ln -s /usr/bin/python3 /usr/bin/python

SSH Connection Issues

# Enable SSH debugging
ANSIBLE_DEBUG=1 ansible all -i inventory -m ping -vvv

Permission Problems

# Configure sudoers
echo "ansible ALL=(ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/ansible