Configuration Management With Ansible

Hello! I'm a student passionate about becoming a Cloud and DevOps Engineer. I have a solid foundation in AWS Cloud, Linux, Docker, and Kubernetes.
What is Configuration Management?
Configuration management is a way how DevOps engineer manage the configuration of the Server.
Problem statement
If you have 100 servers in your data center or instances in the cloud running different operating systems (25 Ubuntu, 25 Windows, 50 CentOS), you need to update or install new software. You could write scripts, but the shell or PowerShell commands will differ for each server. Configuration management tools solve this problem.
Why Ansible?
There are many tools for configuration management, such as Puppet, Chef, and Ansible. However, Ansible is the most widely used.
Ansible is free and open-source, and it uses simple declarative YAML for writing configurations.
Ansible operates with a pull-based mechanism and is agentless, using SSH.
However, when using Windows, it can be slightly difficult to work with Ansible.
Working with Ansible
launch two ec2 instances, one Ansible Server and another the target server.
setting password less Authentication:To start with Ansible, we need to establish passwordless authentication between the servers. Go to the Ansible server and run the command: ssh-keygen. Copy the public key and paste it into the target server.
Run cat .ssh/id_ed25519.pub to copy the output, then go to the target server and paste the key into the .ssh/authorized_keys file. Now, you can log into that server without a password.
Configuring hosts: The inventory file contains the IP addresses of the nodes that need to be configured. By default, the inventory file is located at /etc/ansible/hosts, but you can customize it. Create a file with touch inventory and add the private IP of the target server.
Running Ansible commands: For configuration management using Ansible, you can use ad-hoc commands or playbooks.
Run the command: ansible -i inventory all -m "shell" -a "touch devOps"
This will create a file named devOps on your target server.
You can create groups of servers in the inventory file and execute commands for specific groups only. (In the command, replace "all" with the group name.)
Writing a playbook to install and start Nginx: vi first-playbook.yml
- name: Install and start Nginx
hosts: all
become: true
tasks:
- name: Install nginx
apt:
name: nginx
state: present # apt install nginx
- name: Start nginx
service:
name: nginx
state: started
Command: ansible-playbook -i inventory ansible-playbook.yml
This is the playbook for installing and starting the Nginx web server.


