Lesson 1 of 11 ↓
- 1.Ansible Basic Setup Guide
- 2.Ansible Inventory Guide (hosts)
- 3.Ansible Configuration Guide (ansible.cfg)
- 4.Ansible Ad-hoc Commands Guide
- 5.Ansible Playbook Guide (playbook.yml)
- 6.Ansible Variables & Precedence Guide
- 7.Ansible Loops Guide
- 8.Ansible Conditionals Guide (when)
- 9.Ansible Vault Guide
- 10.Ansible Handlers Guide
- 11.Ansible Custom Roles Guide
4 min read
Ansible Basic Setup Guide
Introduction & Architecture
Ansible Advantages
-
Agentless: No daemons or agents installed on managed nodes (uses SSH).
-
Simple: Easy to learn and read using YAML syntax.
-
Powerful: Handles complex deployments, configuration management, and orchestration.
-
Repeatable: Ensures consistent environments every time you run it.
-
Idempotent: Tasks run only when needed (no changes if the state is already correct).
-
Extensible: Can be extended using custom modules, plugins, and roles.
Lab Architecture
This diagram shows how the Controller connects to the Agents using SSH.
flowchart LR
subgraph AWS["AWS Cloud"]
subgraph Controller["Ansible Controller"]
A["Ansible Software"]
Inv["Inventory File"]
Key["private-key.pem"]
Cfg["ansible.cfg"]
end
Agent1["Agent-01 (Amazon Linux)"]
Agent2["Agent-02 (Amazon Linux)"]
Controller -->|SSH Port 22| Agent1
Controller -->|SSH Port 22| Agent2
endInfrastructure Setup (aws)
Step 1: Launch Ec2 Instances
Before installing anything, we need to create the servers on AWS.
-
Launch 3 Instances:
-
OS: Amazon Linux 2 or 2023.
-
Instance Type: t2.micro or t3.micro (Free tier eligible).
-
Key Pair: Create/Select one key pair (e.g.,
ansible.pem) and download it. Use this same key for all 3 instances. -
Security Group: Ensure Port 22 (SSH) is open. Allow traffic from your IP and from the Security Group itself (for internal communication).
-
-
Assign Roles:
-
Instance 1 -> Controller Node (Where we run commands).
-
Instance 2 -> Managed Node 1.
-
Instance 3 -> Managed Node 2.
-
System Configuration
Step 2: Configure Hostnames
Setting hostnames makes it easier to identify which server you are working on.
A. Configure the Controller Connect to the first instance via SSH and run:
sudo hostnamectl set-hostname ansible-controllerAction: Log out (exit or Ctrl+D) and log back in to see the prompt change to [ec2-user@ansible-controller ~]$.
B. Configure Agent 01 Connect to the second instance:
sudo hostnamectl set-hostname ansible-agent01C. Configure Agent 02 Connect to the third instance:
sudo hostnamectl set-hostname ansible-agent02Ansible Installation & Setup
All following steps are performed ONLY on the Ansible Controller Node.
Step 3: Install Ansible
We need to install the Ansible software engine on the controller.
sudo dnf install ansible -yStep 4: Prepare the Project Directory
It is best practice to keep your Ansible files in a specific folder.
mkdir ~/ansible-project
cd ~/ansible-projectStep 5: Set Up Ssh Key on Controller
Ansible needs the private key (.pem file) to log in to the managed nodes.
-
Upload/Create Key: Copy the content of your
ansible.pem(from your local machine) to a new file namedyour-key.peminside the~/ansible-projectfolder on the controller.- You can use
nano your-key.pem, paste the content, and save.
- You can use
-
Secure the Key: SSH requires private keys to have strict permissions (read-only by owner).
bash chmod 400 ~/ansible-project/your-key.pemExplanation: If you don't do this, SSH will reject the key as "too open".
Connecting Ansible to Nodes
Step 6: Create Inventory File (hosts)
The inventory file tells Ansible which servers to manage and how to connect to them.
Create a file named hosts:
[web]
<node1_public_ip> ansible_user=ec2-user ansible_ssh_private_key_file=~/ansible-project/your-key.pem
<node2_public_ip> ansible_user=ec2-user ansible_ssh_private_key_file=~/ansible-project/your-key.pem-
[web]: This is a group name. You can run commands against all servers in this group. -
<node1_private_ip>public ip for node -
ansible_user: The default user for Amazon Linux isec2-user. -
ansible_ssh_private_key_file: The path to the key we secured in Step 5.
Step 7: Create Configuration File (ansible.cfg)
This file sets defaults so you don't have to type flags manually every time.
Create a file named ansible.cfg in the same directory:
[defaults]
inventory = ./hosts
host_key_checking = False-
inventory: Tells Ansible to look at thehostsfile we just made. -
host_key_checking = False: Prevents the interactive "Are you sure you want to connect?" prompt, which is crucial for automation.
Verification
Step 8: Test Connectivity
Now, let's verify that the Controller can talk to the Agents using the ping module.
ansible web -m pingExpected Output: You should see a green "SUCCESS" message for both nodes.
172.31.x.x | SUCCESS => {
"ping": "pong"
}
172.31.y.y | SUCCESS => {
"ping": "pong"
}Note: This is not a network ping (ICMP); it verifies that Ansible can SSH into the node, find Python, and execute a script.
Comments
Loading the discussion…