Day01 ‐ Introduction , Installation & Basic Commands - harishgorla5/Ansible GitHub Wiki
Day 1: Introduction, Installation & Ad-Hoc Commands
📘 1. What is Ansible?
Ansible is a simple, agentless IT automation tool used for configuration management, application deployment, and orchestration.
🔑 Key Features:
- Agentless: Uses SSH to communicate, no agent needed on nodes
- Push-based: You control servers from a central control node
- Idempotent: Running the same play multiple times has the same result
🏗️ Architecture:
- Control Node: Where Ansible is installed and commands are run
- Managed Nodes: Target systems Ansible connects to
- Inventory File: List of hosts/groups that Ansible manages
- Modules: Units of work like
yum
,apt
,copy
,user
, etc.
🛠️ 2. Installing Ansible (Amazon Linux 2)
Option 1: Using amazon-linux-extras
sudo amazon-linux-extras enable ansible2
sudo yum install ansible -y
Option 2: Using pip (alternative)
sudo yum install python3-pip -y
pip3 install ansible
Check version:
ansible --version
🗂️ 3. Inventory File Setup
inventory
)
Sample inventory ([web]
172.31.9.115 ansible_user=ec2-user ansible_ssh_private_key_file=~/.ssh/your-key.pem
[db]
172.31.15.215 ansible_user=ec2-user ansible_ssh_private_key_file=~/.ssh/your-key.pem
Test connectivity:
ansible all -i inventory -m ping
🔧 4. Ad-Hoc Commands
✅ Uptime
ansible all -i inventory -m shell -a "uptime"
✅ Install Apache
ansible web -i inventory -m yum -a "name=httpd state=present" --become
✅ Start Apache
ansible web -i inventory -m service -a "name=httpd state=started enabled=yes" --become
✅ Copy a file
ansible web -i inventory -m copy -a "src=/etc/hosts dest=/tmp/hosts" --become
✅ Create user
ansible all -i inventory -m user -a "name=demouser state=present" --become
🧪 Real World Example: Apache + MariaDB Setup on EC2
🧩 Scenario: You’re a DevOps engineer managing two EC2 instances. Your task is to:
- Install Apache (
httpd
) on the web server - Install MariaDB on the db server
- Start and enable the services
🖥️ Inventory File:
[web]
172.31.9.115 ansible_user=ec2-user ansible_ssh_private_key_file=~/.ssh/your-key.pem
[db]
172.31.15.215 ansible_user=ec2-user ansible_ssh_private_key_file=~/.ssh/your-key.pem
✅ Step-by-Step Tasks
1. Verify Connection to All Nodes
ansible all -i inventory -m ping
2. Install Apache on Web Server
ansible web -i inventory -m yum -a "name=httpd state=present" --become
3. Start and Enable Apache
ansible web -i inventory -m service -a "name=httpd state=started enabled=yes" --become
4. Deploy a Simple Homepage
echo "Welcome to the Web Server - Managed by Ansible" > index.html
ansible web -i inventory -m copy -a "src=index.html dest=/var/www/html/index.html" --become
5. Install MariaDB on DB Server
ansible db -i inventory -m yum -a "name=mariadb-server state=present" --become
6. Start and Enable MariaDB
ansible db -i inventory -m service -a "name=mariadb state=started enabled=yes" --become
7. Test Apache
Visit:
http://<public-ip-of-web-server>
🧠 6. Key Questions
- What is agentless architecture?
- What is the difference between shell and command modules?
- What does idempotent mean?
- How does Ansible determine which SSH user to use?
🎓 7. Homework Assignment
- Add a
[db]
group in your inventory. - Install
mariadb-server
on the DB host using ad-hoc command. - Start and enable the service:
ansible db -i inventory -m yum -a "name=mariadb-server state=present" --become
ansible db -i inventory -m service -a "name=mariadb state=started enabled=yes" --become
📎 8. Notes
- Always check SSH access manually before running Ansible.
- Use
--ask-become-pass
if password is needed for sudo. - Use
ansible.cfg
to define default inventory and SSH key for convenience.