Day 58 - Ansible Playbooks ππ€
Hello DevOps enthusiasts! π Welcome to Day 58 of the #90DaysOfDevOps challenge. Today, weβre diving into Ansible Playbooks! π οΈβ¨
Ansible Playbooks are essential tools in DevOps, allowing us to automate complex tasks, manage configurations, and deploy applications seamlessly. Imagine them as instruction manuals π that help you get multiple servers working together efficiently. ππ‘
What We'll Cover Today:
Creating a file on a different server using Ansible Playbooks. π
Creating a new user with an Ansible Playbook. π€
Installing Docker on a group of servers using Ansible Playbooks. π³
Best practices for writing Ansible Playbooks. π
Let's get started! ππ₯
Task 1: Creating a File on a Different Server π
Step 1: Write an Ansible Playbook βοΈ
Create a playbook file named create_file.yml
with the following content to create a file on the server:
---
- name: Create a file on a different server
hosts: all
become: true
tasks:
- name: Create a file
file:
path: /tmp/ansible_file.txt
state: touch
Step 2: Execute the Playbook π
Run the playbook using the ansible-playbook
command:
ansible-playbook create_file.yml -i <inventory-file-path> --private-key=<private-key-path>
Step 3: Verify the File Creation β
Log into the server and check if the file /tmp/ansible_file.txt
has been created:
ssh -i <private-key-path> <user>@<server-ip>
ls /tmp/ansible_file.txt
Task 2: Installing Docker on a Group of Servers π³
Step 1: Write an Ansible Playbook βοΈ
Create a playbook file named install_docker.yml
to install Docker on the servers:
---
- name: Install Docker on servers
hosts: docker_group
become: true
tasks:
- name: Update apt cache and install Docker
apt:
name: docker.io
state: present
update_cache: yes
when: ansible_os_family == 'Debian'
- name: Install Docker
yum:
name: docker
state: present
when: ansible_os_family == 'RedHat'
- name: Start Docker service
service:
name: docker
state: started
enabled: yes
Step 2: Execute the Playbook π
Run the playbook using the ansible-playbook
command:
ansible-playbook install_docker.yml -i <inventory-file-path> --private-key=<private-key-path>
Step 3: Verify Docker Installation β
Log into one of the servers and check the Docker installation:
ssh -i <private-key-path> <user>@<server-ip>
docker --version
Task 3: Best Practices for Writing Ansible Playbooks π
Ansible playbooks are YAML files that define a set of tasks and configurations to be executed on remote systems. Playbooks allow you to automate various infrastructure management tasks, such as provisioning servers, configuring services, deploying applications, and more. πΌ
Examples of Ansible Playbooks π
Installing Packages π¦
---
- name: Install packages
hosts: web_servers
become: true
tasks:
- name: Update package cache
apt:
update_cache: yes
when: ansible_os_family == 'Debian'
- name: Install nginx
apt:
name: nginx
state: present
when: ansible_os_family == 'Debian'
- name: Install httpd
yum:
name: httpd
state: present
when: ansible_os_family == 'RedHat'
Configuring Services π§
---
- name: Configure Nginx
hosts: web_servers
become: true
tasks:
- name: Copy Nginx configuration file
copy:
src: nginx.conf
dest: /etc/nginx/nginx.conf
notify: restart nginx
handlers:
- name: restart nginx
service:
name: nginx
state: restarted
Deploying Applications π
---
- name: Deploy myapp
hosts: app_servers
become: true
tasks:
- name: Clone Git repository
git:
repo: https://github.com/myusername/myapp.git
dest: /var/www/myapp
version: master
- name: Install application dependencies
command: npm install
args:
chdir: /var/www/myapp
- name: Start the application
command: npm start
args:
chdir: /var/www/myapp
User Management π₯
---
- name: Create user accounts
hosts: all
become: true
vars:
users:
- username: user1
password: "{{ 'user1_password' | password_hash('sha512') }}"
- username: user2
password: "{{ 'user2_password' | password_hash('sha512') }}"
tasks:
- name: Create user accounts
user:
name: "{{ item.username }}"
password: "{{ item.password }}"
state: present
with_items: "{{ users }}"
Firewall Configuration π₯
---
- name: Configure firewall
hosts: web_servers
become: true
vars:
allowed_ports:
- 80
- 443
tasks:
- name: Allow incoming HTTP and HTTPS traffic
ufw:
rule: allow
port: "{{ item }}"
with_items: "{{ allowed_ports }}"
Database Backup πΎ
---
- name: Backup database
hosts: db_servers
become: true
tasks:
- name: Stop database service
service:
name: mysql
state: stopped
- name: Backup database
command: mysqldump -u root -p{{ db_password }} my_database > /tmp/my_database_backup.sql
args:
warn: false
- name: Start database service
service:
name: mysql
state: started
Thank you for reading! π
Subscribe to my newsletter
Read articles from Nilkanth Mistry directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Nilkanth Mistry
Nilkanth Mistry
Embark on a 90-day DevOps journey with me as we tackle challenges, unravel complexities, and conquer the world of seamless software delivery. Join my Hashnode blog series where we'll explore hands-on DevOps scenarios, troubleshooting real-world issues, and mastering the art of efficient deployment. Let's embrace the challenges and elevate our DevOps expertise together! #DevOpsChallenges #HandsOnLearning #ContinuousImprovement