🔹Table of Contents :
Introduction
Ansible Installation and Configuration
Secure Connections Setup
Executing Server Updates with Ansible
Real-World Scenarios for Ansible Automation
Conclusion and Key Takeaways
Today, I dove into a powerful application of Ansible by setting up an Ansible master node to update three remote servers automatically. Here’s the breakdown of the process and a guide on the commands used, complete with explanations and real-world scenarios.
✅Step 1: Setting Up Ansible on the Master Server
To begin, I needed to install Ansible on the master server that would control the updates across all other servers.
Add the Ansible repository:
sudo add-apt-repository ppa:ansible/ansible
- This command adds Ansible’s PPA (Personal Package Archive) to the server, enabling access to the latest Ansible version.
Update system packages:
sudo apt update
- This command fetches the latest information about packages, ensuring that when we install Ansible, we get the latest stable version.
Install Ansible:
sudo apt install ansible
- Installs Ansible, readying the server to manage and communicate with other nodes.
Verify Installation:
ansible --version
- This command verifies the installation and shows the current Ansible version.
✅Step 2: Configuring Ansible to Communicate with Remote Servers
For Ansible to manage the remote servers, we need to configure the hosts
file on the master server to specify target nodes.
Open and edit the hosts file:
sudo vim /etc/ansible/hosts
- In Ansible, this
hosts
file lists all the servers that Ansible will control. Here, I added the three remote servers.
- In Ansible, this
Add Remote Servers to Hosts File: Inside the hosts file:
[webservers] server1 ansible_host=<public_ip_of_server1> server2 ansible_host=<public_ip_of_server2> server3 ansible_host=<public_ip_of_server3>
Here, we group the servers under
[webservers]
. Each entry contains the alias of the server (e.g., server1) and its public IP.Using this, Ansible will know the IP address for each alias, making it easier to run commands across multiple servers simultaneously.
✅Step 3: Setting Up Secure Access with SSH Keys
To securely connect to remote servers without a password, I used SSH keys. This step ensures secure and automated access.
Create a Directory for Keys:
mkdir keys && cd keys
- This command creates a dedicated directory for our SSH key files, organizing and securing access credentials.
Copy the PEM file to the Remote Server:
scp -i <pem_file> <pem_file> user@<server_ip>:/home/user/keys/
- Here, I used the
scp
command to securely copy the PEM file (SSH private key) from the local machine to each remote server.
- Here, I used the
Establish Trust for Passwordless Access: By placing the PEM file in each remote server’s key directory, I could set up Ansible to access each server without needing manual password entry each time.
✅Step 4: Updating Remote Servers Using Ansible
With the setup complete, I was ready to automate updates across all three servers.
Updating Remote Servers with Ansible:
ansible webservers -m apt -a "update_cache=yes" --become
This command tells Ansible to use the
apt
module to update the package cache on all servers listed in the[webservers]
group.--become
elevates privileges as root, ensuring all packages can be updated.
Verifying Changes: To confirm that updates were successful, I used the following:
ansible webservers -m command -a "ls /var/log/apt/history.log"
- This command checks the log files on each remote server, verifying the recent update history.
✅Real-World Scenarios and Benefits of Using Ansible for Server Management
Efficient Management Across Environments: Ansible is ideal for teams with multiple environments, like staging and production, that need to be consistently updated. Instead of accessing each server individually, we can manage them all in parallel from a single command on the master node.
Centralized Security Updates: With automated update capabilities, Ansible makes it easy to ensure security patches are applied across all servers in seconds, helping maintain a robust, secure infrastructure.
Version Control and Consistency: By using Ansible playbooks (scripts that define a series of tasks), we can ensure that each server is consistently configured, helping avoid “configuration drift” when server configurations start to differ over time.
Stay tuned for more hands-on tasks and in-depth learning!
🚀Thanks for joining me on Day 57! Let’s keep learning and growing together!
Happy Learning! 😊
#90DaysOfDevOps