🌟 Introduction
Automation is at the heart of modern DevOps practices, and Ansible has emerged as a powerful tool for streamlining server configuration, deployment, and management. In this blog, we'll walk you through the process of creating your first Ansible playbook step by step. By the end, you'll be well on your way to automating tasks and hosting website or infrastructure management more efficiently.
🔱Prerequisites
Before we dive into creating your first Ansible playbook, make sure you have the following:
- Servers setup: If you do not set up the Ansible master server with different node servers and you haven't installed Ansible yet, refer to our CLICK_ON_THIS_LINK for installation instructions.
Target Server: You should have a remote server (physical or virtual) where you want to apply your playbook. Make sure you have SSH access to this server.
🔱Ansible Playbook
Playbooks are Ansible's configuration, deployment, and orchestration language. Written in YAML format, playbooks define a series of tasks to be executed on specific hosts or groups. Each task is associated with a module and contains the desired state that Ansible will ensure on the target systems. Playbooks enable IT teams to automate complex workflows, making them highly valuable for repetitive or multi-step processes.
🌟Follow these steps to perform this project
If you want to know how to set up the Ansible master server and node servers follow this link:
🔱TASK 1: Ansible Host files
Ansible uses this file to map target hosts to managed nodes. The host's file is usually located in
/etc/ansible/hosts
.So, open the host's file and add the IP addresses of the Nodes:
sudo vim /etc/ansible/hosts
✔Step 1: Add Node Server IP Addresses
Let's add the Node IP addresses in the host's file:
✔Step2: Ansible Inventory
Now, let's check the Ansible inventory using the command:
ansible-inventory --list
✔Step3: Try a ping command using Ansible to the Nodes
We use the following commands to check connectivity between the master server and node servers.
ansible servers -m ping
# ansible is the command line utility used to interact with remote servers
# servers is a group name we created for node servers, shown above
# -m is the module
# ping is the name of module
ansible prod -m "ping"
✔Step4: Installing docker and checking the version
ansible prod -m apt -a "name=docker.io state=present update_cache=yes"
ansible prod -a "docker --version"
ansible prod -a "free -h"
🔱TASK 2: Ansible Playbook
✔Step 1: Setting Up Your Directory Structure
Organizing your Ansible project is essential for maintainability. Let's start by creating a directory for your playbook:
mkdir playbooks
cd playbooks
✔Step 2: Create Your First Playbook
Now, let's create your very first playbook. In the same project directory, create a file named date_play.yml
:
ansible-playbook date_play.yml
ansible-playbook -v date_play.yml
The -v
flag indicates that you want to run the playbook in verbose mode, which will provide more detailed output during execution.
✔Step 3: Create Your Second Playbook (install-nginx)
Now, let's create your second playbook. In the same project directory, create a file named install_nginx.yml
:
In this playbook, we have to only install nginx.
ansible-playbook install_nginx.yml
- name: Install nginx and start it
hosts: servers
become: yes
tasks:
- name: install nginx
apt:
name: nginx
state: latest
✔Step 4: Create Your Second Playbook (start-nginx)
Now let's modify this playbook, we have to start nginx.
- name: Install nginx and start it
hosts: servers
become: yes
tasks:
- name: install nginx
apt:
name: nginx
state: latest
- name: Start Nginx
service:
name: nginx
state: started
enable: yes
✔Step 5: Now verify nginx working
Now verify server 1 with an IP address.
Now verify server 2 with an IP address.
🔱TASK 3: Static Website hosting using Playbook
✔Step 1: Create Ansible Playbook
Create a new playbook in the directory known as a "playbooks".
Here you create a YAML file "deploy_static_page.yml" and create a new index.html page to serve in the prod group servers.
- name: Install nginx and serve static website
hosts: prod
become: yes
tasks:
- name: Install Nginx
apt:
name: nginx
state: latest
- name: Start Nginx
service:
name: nginx
state: started
enable: yes
- name: Deploy web page
copy:
src: index.html
dest: /var/www/html
ansible-playbook deploy_static_page_play.yml
✔Step 2: Check your Web page
Now, it's time to check your webpage is visible and working fine.
Modify your index.html file and reapply the playbook "deploy_static_page.yml" in the directory.
🌟Conclusion
Congratulations! We have just created and executed the Ansible playbook. This simple example demonstrates the power of Ansible in automating tasks on remote servers.
I always recommend visiting the official documentation while learning a new skill/tool. Please check this official doc: docs.ansible.com/ansible/latest/playbook_gu..
As you continue your Ansible journey, you can explore more advanced playbooks, modules, and features to orchestrate complex infrastructure configurations and deployments.
\...................................................................................................................................................
The above information is up to my understanding. Suggestions are always welcome. Thanks for reading this article.😊
#aws #cloudcomputing #ansible #configurationmanagement #ansibleplaybook #Devops #TrainWithShubham #90daysofdevops #happylearning
Follow for many such contents: