Ansible is an open-source IT automation tool that makes it easy to manage servers. This guide will walk you through installing Ansible, setting up SSH access using both SSH key and password, and running basic ad-hoc commands and playbooks.
๐ฆ 1. Install Ansible
You can install Ansible via APT or PIP depending on your environment.
๐ง APT (Debian/Ubuntu)
sudo apt update
sudo apt install ansible -y
๐ PIP (Python)
sudo apt install python3-pip -y
pip3 install --user ansible
Make sure ~/.local/bin
is in your PATH
.
๐ ๏ธ 2. Verify Installation
ansible --version
Output example:
ansible [core 2.15.0]
๐ 3. Configure SSH Access
Ansible uses SSH to communicate with remote systems. You can use either:
- SSH Key (default & secure)
- SSH Password (fallback method)
โ Option A: SSH Key Authentication
Generate SSH Key
ssh-keygen -t rsa -b 4096
Press enter through all prompts.
Copy Key to Remote Server
ssh-copy-id [email protected]
Test Connection
ssh [email protected]
Create Inventory File
# host.ini
[web]
192.168.1.10 ansible_user=ubuntu
Ping Test
ansible -i host.ini web -m ping
๐ Option B: SSH Password Authentication
Used when SSH key is not available yet.
Create Inventory File
# inventory.ini
[linux]
192.168.1.20 ansible_user=ubuntu
Ping Test with Password Prompt
ansible -i inventory.ini web -m ping --ask-pass
Youโll be prompted for the SSH password.
You can add password directly on the inventory like this:
# inventory.ini
[linux]
192.168.1.20 ansible_user=ubuntu ansible_password=pass ansible_port=2222
Run Playbook with Password
ansible-playbook -i inventory.ini playbook.yml --ask-pass --become --ask-become-pass
โ๏ธ 4. Default Policies and Sudo Access
To run tasks requiring sudo, use:
--become --ask-become-pass
๐งช 5. Ad-Hoc Commands
Run one-off commands without writing a playbook.
Install Package (APT)
ansible -i inventory.ini web -m apt -a "name=htop state=present" --become
Install Python Package (PIP)
ansible -i inventory.ini web -m pip -a "name=requests" --become
๐ 6. Create Your First Playbook
install_tools.yml
- name: Install basic tools
hosts: all
become: yes
tasks:
- name: Install htop
apt:
name: htop
state: present
update_cache: yes
- name: Install requests via pip
pip:
name: requests
Run the Playbook
With SSH Key:
ansible-playbook -i inventory.ini install_tools.yml --become
With Password:
ansible-playbook -i inventory.ini install_tools.yml --ask-pass --become --ask-become-pass
โ Summary Table
Purpose | Command Example |
---|---|
Install via APT | sudo apt install ansible |
Install via PIP | pip3 install --user ansible |
Ping Test (SSH Key) | ansible -i inventory.ini all -m ping |
Ping Test (Password) | ansible -i inventory.ini all -m ping --ask-pass |
Sudo Ad-Hoc Command | --become |
Sudo Password Prompt | --ask-become-pass |
Run Playbook (SSH Key) | ansible-playbook -i inventory.ini playbook.yml --become |
Run Playbook (Pass) | ansible-playbook -i inventory.ini playbook.yml --ask-pass --ask-become-pass |
๐ก Tips
- Keep separate inventory files for key and password-based hosts.
- Use
group_vars
andansible.cfg
for more automation. - Use
--limit
to target specific hosts or groups.
Source:
https://docs.ansible.com/ansible/latest/getting_started/index.html
https://github.com/ansible/ansible
https://medium.com/@shivam2003/ansible-a-complete-guide-from-basics-to-advanced-ffd1bf74322f