Introduction to Configuration Management with Ansible
Unveiling Use Cases, Setup on Master and Agent Machines, Ad-hoc Commands, and Playbooks for Automation
What is Ansible ? ๐ง
Ansible is an open-source automation tool used for configuration management, application deployment, and task automation. It is designed to be simple, agentless, and easy to use.
Ansible uses a declarative language for system configurations and automation tasks, allowing you to define the desired system state instead of specifying individual steps.
Use Cases of Ansible:
Exploring practical applications, such as configuration management, application deployment, and task automation.
Emphasizing the role of Ansible in enabling efficient and scalable DevOps practices.
Setting Up Master and Agent :
Install Ansible on the master server.
Setting Up the Inventory File.
There are two ways to set up the Inventory file:
One way is to add the agents' IP addresses in the hosts file
sudo vim /etc/ansible/hosts
And the other way is to create an inventory file in the current location.
Create an SSH key pair on the Ansible master by using the
ssh-keygen
command. This will generate a public key (id_
rsa.pub
) and a private key (id_rsa
).ssh-keygen
Copy Public Key to Agent Nodes:
Open the public key file (
id_
rsa.pub
) and copy its contents.cat ~/.ssh/id_rsa.pub
Create an SSH key pair on the Ansible agents using the
ssh-keygen
command. Then, log in to each agents and add the copied public key to theauthorized_keys
file.ssh-keygen
Ad-hoc Commands:
Ad-hoc commands in Ansible are concise, one-line tasks used for quick checks or tasks on remote hosts without the need for a full playbook. They are run from the command line and offer a convenient way to engage with remote systems.
Examples:
Ping Test
ansible -i inventory all -m ping
Check Free Disk Space
ansible -i inventory all -m "shell" -a "df -h"
Ansible Playbook:
An Ansible playbook is a YAML file that outlines tasks to run on remote hosts. Playbooks automate tasks, configurations, and deployments in a reusable way. Each playbook comprises one or more plays, and each play includes a list of tasks.
Examples:
Nginx Playbook
--- - name: Nginx Installation and Configuration hosts: all become: true tasks: - name: Install Nginx apt: name: nginx state: present - name: Start Nginx service service: name: nginx state: started enabled: yes
Run the playbook:
ansible-playbook -i inventory nginx_playbook.yml
Docker Playbook
--- - name: Docker Installation hosts: all become: true tasks: - name: Install required packages apt: name: - apt-transport-https - ca-certificates - curl - gnupg-agent - software-properties-common state: present - name: Add Docker GPG key apt_key: url: https://download.docker.com/linux/ubuntu/gpg state: present - name: Add Docker APT repository apt_repository: repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable" state: present - name: Update apt package cache again apt: update_cache: yes - name: Install Docker apt: name: - docker-ce - docker-ce-cli - containerd.io state: present - name: Start Docker service service: name: docker state: started enabled: yes
Happy Learning ^_^