installation-and-setup
⚙️ Ansible Installation and Setup
This guide will walk you through installing Ansible, creating an Ansible-specific user, and setting up SSH key-based authentication for seamless automation.
🖥️ Overview: Control Node vs Target Nodes
-
✅ Ansible Server (Control Node):
- Install Ansible.
- Generate SSH keys.
- Configure inventory.
- Run playbooks.
-
🎯 Target Machines (Managed Nodes):
- Create an
ansible
user. - Allow SSH access.
- Provide
sudo
privileges (optional but recommended). - Ensure Python is installed (required by Ansible modules).
- Create an
🖥️ Step 1: Install Ansible on Control Node
🔹 On Ubuntu / Debian
sudo apt update
sudo apt install -y ansible
🔹 On CentOS / RHEL
sudo dnf install -y epel-release
sudo dnf install -y ansible
🔹 On MacOS (via Homebrew)
brew install ansible
You can verify installation with:
ansible --version
👤 Step 2: Create Ansible User on Target Nodes
Run these commands on each target machine:
sudo adduser ansible
sudo usermod -aG sudo ansible
⚠️ Ensure that the
sudo
group is correctly set for privilege escalation.
🔐 Step 3: Set Up SSH Key-Based Authentication
🔹 On the Control Node:
Generate an SSH key:
ssh-keygen -t rsa -b 4096 -C "ansible@control"
Accept the default path (~/.ssh/id_rsa
).
🔹 Copy the public key to each Target Node:
ssh-copy-id ansible@<managed-node-ip>
🔑 This adds your public key to the
~/.ssh/authorized_keys
file of theansible
user on the managed host.
🔹 Test the connection:
ssh ansible@<managed-node-ip>
You should be logged in without needing a password.
📁 Step 4: Create Inventory File on Control Node
Create a simple hosts.ini
file:
[webservers]
192.168.1.101
192.168.1.102
Test a ping:
ansible -i hosts.ini all -m ping -u ansible
Expected output:
192.168.1.101 | SUCCESS => {
"changed": false,
"ping": "pong"
}
✅ Summary
🔵 On the Control Node:
- Install Ansible.
- Generate and manage SSH keys.
- Set up and manage inventory.
- Run and test Ansible commands.
🟢 On Target Nodes:
- Create
ansible
user. - Enable SSH access.
- Grant
sudo
privileges. - Make sure Python is installed.
With this setup, you are now ready to write and execute Ansible playbooks!