Ansible to Automate Configuration Management on a Cloud Server
AWS Ansible Automation Realtime UseCase - Handson
1. Why We Need This Use Case
Using Ansible for automating configuration management on a cloud server helps ensure that all servers are configured consistently and according to best practices. Ansible simplifies the management of cloud servers by automating repetitive tasks, reducing human error, and enabling infrastructure as code (IaC). This leads to improved efficiency, reliability, and scalability of server configurations.
2. When We Need This Use Case
When managing multiple cloud servers that require consistent configuration.
When automating the installation and configuration of software packages.
When ensuring compliance with security policies and best practices.
When reducing manual intervention in server setup and configuration.
When implementing infrastructure as code (IaC) practices,.
3. Prerequisites for the Lab
An active cloud account (e.g., AWS, Azure, GCP).
Ansible is installed on a control machine (a local machine or another server).
SSH access to the cloud server(s).
Basic knowledge of Ansible playbooks and YAML syntax.
Cloud server(s) running a supported operating system (e.g., Ubuntu, CentOS).
4. Advantages and Disadvantages of This Use Case
Advantages
Consistency: ensures consistent configurations across all servers.
Automation: reduces manual effort and speeds up configuration processes.
Scalability: Easily manage configurations for a large number of servers.
Idempotence: Ansible ensures that applying the same configuration multiple times does not change the system state if it is already compliant.
Disadvantages
Learning Curve: Requires learning Ansible syntax and best practices.
Dependency Management: Some modules may have dependencies that need to be managed.
Execution Time: Large playbooks or numerous tasks can take time to execute.
5. Step-by-Step Implementation Instructions
Step 1: Install Ansible on the Control Machine
Refer - https://github.com/careerbytecode/cloud-devops-engineer-training/blob/main/3-ansible/ansible-automation.MD to install ansible server
Install Ansible:
On Ubuntu/Debian:
sudo apt update
sudo apt install ansible -yOn CentOS/RHEL:
sudo yum install epel-release -y
sudo yum install ansible -yStep 2: Configure SSH Access to the Cloud Server
Generate SSH Key (if not already available):
ssh-keygen -t rsa -b 4096
Copy the SSH key to the cloud server:
ssh-copy-id user@your-cloud-server-ipStep 3: Create an Ansible Inventory File
Create a directory for your Ansible project:
mkdir ansible-cloud-config
cd ansible-cloud-configCreate an inventory file (hosts):
[cloud_servers]
your-cloud-server-ipFollow this and make sure all steps are completed
https://github.com/careerbytecode/cloud-devops-engineer-training/blob/main/3-ansible/ansible-automation.MD to install ansible server
Step 4: Write an Ansible Playbook
Create a playbook file (site.yml):
vim site.yaml---
- name: Configure Cloud Server
hosts: cloud_servers
become: yes
tasks:
- name: Ensure system packages are up to date
apt:
update_cache: yes
upgrade: dist
when: ansible_os_family == "Debian"
- name: Ensure system packages are up to date
yum:
name: '*'
state: latest
when: ansible_os_family == "RedHat"
- name: Install Nginx
apt:
name: nginx
state: present
when: ansible_os_family == "Debian"
- name: Install Nginx
yum:
name: nginx
state: present
when: ansible_os_family == "RedHat"
- name: Start and enable Nginx
service:
name: nginx
state: started
enabled: yes
- name: Copy a custom index.html file
copy:
src: index.html
dest: /var/www/html/index.htmlCreate a custom index.html file:
vim index.html<!DOCTYPE html>
<html>
<head>
<title>Welcome to Ansible Automated Server</title>
</head>
<body>
<h1>Hello from Ansible!</h1>
</body>
</html>Step 5: Run the Ansible Playbook
Execute the playbook:
ansible-playbook -i hosts site.ymlStep 6: Verify the Configuration
Open a web browser and navigate to the IP address of your cloud server.
Ensure that the Nginx server is running and the custom index.html is displayed.
6. Conclusion
Using Ansible to automate configuration management on a cloud server streamlines the setup and maintenance of server environments. This use case demonstrates the power of Ansible in ensuring consistent, repeatable configurations, and reducing manual workload. By following these step-by-step instructions, you can efficiently manage your cloud servers, improve reliability, and embrace infrastructure as code practices.



