Ansible playbooks run multiple tasks, assign roles, and define configurations, deployment steps, and variables. If you’re using multiple servers, Ansible playbooks organize the steps between the assembled machines or servers and get them organized and running in the way the users need them to. Consider playbooks as the equivalent of instruction manuals.
Task 1
Write an Ansible playbook to create a file on a different server
Create a playbook file for creating a file on the server.
Execute the playbook using the
ansible-playbook
command followed by the playbook filename.ansible-playbook file-name.yml -i <inventory-file-path> --private-key=<private-key-path>
Check the file if it is created on the server.
Write an Ansible playbook to create a new user.
Create a playbook file to create a user on the server.
Execute the playbook using the
ansible-playbook
command followed by the playbook filename.ansible-playbook file-name.yml -i <inventory-file-path> --private-key=<private-key-path>
Check the user on the server if it is created.
Write an Ansible playbook to install docker on a group of servers
Create a playbook file to install the docker on the server.
Execute the playbook using the
ansible-playbook
command followed by the playbook filename.ansible-playbook file-name.yml -i <inventory-file-path> --private-key=<private-key-path>
Verify that docker is installed on the server.
Task 2
Write a blog about writing Ansible playbooks with the best practices.
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.
Here are a few examples of Ansible playbooks to demonstrate their usage:
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'
In this example, the playbook installs the Nginx package on Debian-based systems and the Apache HTTP server package on Red Hat-based systems.
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
This playbook copies a customized Nginx configuration file to the remote servers and triggers a restart of the Nginx service by notifying the handler.
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
This playbook clones a Git repository, installs application dependencies using npm, and starts the application.
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 }}"
This playbook creates user accounts on all hosts specified in the inventory file, with defined usernames and hashed passwords.
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 }}"
This playbook uses the ufw
module to allow incoming HTTP (port 80) and HTTPS (port 443) traffic on web servers.
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
This playbook stops the MySQL service and takes a database backup using mysqldump
, and then starts the service again on database servers.
Thank you for reading!!
~Shreya Gupta
Great initiative by the #trainwithshubham community. Thank you Shubham Londhe
#devops #90daysofdevops #ansible #ansibleplaybooks