Command Palette

Search for a command to run...

Ansible Installation Guide

Complete guide for installing and setting up Ansible, an agentless automation tool that enables infrastructure as code, configuration management, and application deployment using simple YAML syntax.

Ansible is an open-source automation tool that enables infrastructure as code, configuration management, application deployment, and task automation. It uses a simple, human-readable language (YAML) and requires no agents on remote systems.

Prerequisites

  • Python 2.7 or Python 3.5+ (Python 3.9+ recommended)
  • Control node: Linux/Unix/macOS (Windows is not supported as a control node)
  • Managed nodes: Windows or Unix/Linux systems
  • SSH access to managed nodes
  • Administrative/sudo privileges

Installation Instructions

Ubuntu/Debian Installation

# Update package index
sudo apt update
 
# Install dependencies
sudo apt install -y python3 python3-pip
 
# Install Ansible
sudo apt install -y ansible
 
# Alternative: Install via pip
pip3 install --user ansible

Basic Configuration

1. Create Configuration Directory

# Create Ansible directory structure
mkdir -p ~/ansible/playbooks
cd ~/ansible

2. Configure Inventory

Create inventory.ini:

[webservers]
web1 ansible_host=192.168.1.10
web2 ansible_host=192.168.1.11
 
[dbservers]
db1 ansible_host=192.168.1.20
 
[all:vars]
ansible_user=your_ssh_user
ansible_ssh_private_key_file=~/.ssh/id_rsa

3. Configure Ansible

Create ansible.cfg:

[defaults]
inventory = ./inventory.ini
remote_user = your_ssh_user
private_key_file = ~/.ssh/id_rsa
host_key_checking = False

Basic Usage

1. Test Connection

# Ping all hosts
ansible all -m ping
 
# Check specific group
ansible webservers -m ping

2. Create First Playbook

Create playbooks/update_servers.yml:

---
- name: Update all servers
  hosts: all
  become: yes
  tasks:
    - name: Update apt cache (Debian/Ubuntu)
      apt:
        update_cache: yes
      when: ansible_os_family == "Debian"
 
    - name: Update dnf cache (RHEL/CentOS)
      dnf:
        update_cache: yes
      when: ansible_os_family == "RedHat"

3. Run Playbook

# Run playbook
ansible-playbook playbooks/update_servers.yml
 
# Run with extra variables
ansible-playbook playbooks/update_servers.yml -e "variable=value"

Advanced Configuration

1. SSH Key Setup

# Generate SSH key
ssh-keygen -t rsa -b 4096
 
# Copy key to remote hosts
ssh-copy-id username@remote_host

2. Vault Configuration

# Create encrypted file
ansible-vault create secrets.yml
 
# Edit encrypted file
ansible-vault edit secrets.yml
 
# Run playbook with vault
ansible-playbook playbook.yml --ask-vault-pass

3. Role Structure

# Create role structure
ansible-galaxy init my_role
 
# Role directory structure
my_role/
├── defaults/
├── files/
├── handlers/
├── meta/
├── tasks/
├── templates/
└── vars/

Best Practices

  1. Project Organization

    • Use roles for reusable components
    • Keep sensitive data in Ansible Vault
    • Use version control for playbooks
  2. Security

    • Use SSH keys instead of passwords
    • Implement proper inventory grouping
    • Secure sensitive variables
  3. Performance

    • Use dynamic inventory when possible
    • Implement proper error handling
    • Use async tasks for long-running operations

Troubleshooting

Common Issues

  1. SSH Connection Problems

    # Check SSH connection
    ssh -v username@remote_host
     
    # Test Ansible connection
    ansible host -m ping -vvv
  2. Permission Issues

    # Check sudo access
    ansible host -m shell -a "sudo -l"
     
    # Run with become
    ansible-playbook playbook.yml --become
  3. Python Issues

    # Check Python version
    ansible host -m setup -a "filter=ansible_python_version"
     
    # Install Python if missing
    ansible host -m raw -a "apt-get install -y python3"

Error Resolution

  1. Module Errors

    • Check module documentation
    • Verify module dependencies
    • Use correct module parameters
  2. Playbook Syntax

    # Check playbook syntax
    ansible-playbook --syntax-check playbook.yml
     
    # Run in check mode
    ansible-playbook --check playbook.yml

Additional Resources