In this guide, we will look at different Ansible playbook examples with different modules to boost your automation skills using Ansible.
Ansible is a configuration management tool that allows you to automate application configuration on a server.
What is a Ansible Playbook?
A playbook is a set of tasks and configurations that you define in YAML format. Playbooks are designed to be reusable and shareable across teams and organizations, and they allow you to perform a wide range of tasks, such as installing packages, managing services, and configuring configuration files in an idempotent way.
Note: For project implementations, you should be use roles based on the requirements. So that it can be re-used in diffrent playbooks.
Ansible Playbook Example
Following is an example of an Ansible playbook.
---
- name: Install and configure Nginx web server
hosts: webservers
become: true
gather_facts: yes
tasks:
- name: Display OS information
debug:
var: ansible_distribution
- name: Update apt cache
apt:
update_cache: yes
- name: Install Nginx web server
apt:
name: nginx
state: latest
Here is the explanation of the playbook.
- The playbook starts by specifying a
name
- hosts: webservers specified the
webservers
label added to the inventory file. - become: true specifies that the playbook should run as sudo.
- gather_facts: yes gathers all the node information during the playbook run.
- The tasks section contains the tasks to update the apt cache and then install Nginx.
If you don’t have Ansible installed, check out the Ansible installation blog where all the Ansible configurations and inventory configurations are explained.
Now, let’s look at different examples that will help you gain knowledge on different module use cases. You can also use these examples in your day-to-day ansible playbook implementation.
Ansible Playbook To Check OS Using Fact Variable
Checking OS in a playbook with a when clause is necessary to ensure that the playbook is compatible with the operating system and uses a module that is relevant to the operating system. We can do this using Ansible’s facts which gathers all information about the OS when you run the playbook.
You can check out all the available fact variables from here.
Here is an example playbook that checks if the OS is Ubuntu or Centos and installs apache based on the platform. We specify gather_facts: yes
to gather the system facts. Replace hosts
parameter based on your needs.
---
- name: Check operating system
hosts: all
gather_facts: yes
tasks:
- name: Display OS information
debug:
var: ansible_distribution
- name: Install a package based on OS
when: ansible_distribution == 'Ubuntu'
apt:
name: apache2
state: present
when: ansible_distribution == 'CentOS'
yum:
name: httpd
state: present
Ansible Playbook To Install Applications on Ubuntu
The following playbook example installs multiple applications on Ubuntu. It installs Nginx, MySQL Client, and PHP extensions.
Here it is worth noting with_items:
parameter where it loops through multiple packages and applies one by one in the name: "{{ item }}"
parameter.
---
- name: Install Applications on Ubuntu
hosts: webservers
become: true
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
- name: Install MySQL client
apt:
name: mysql-client
state: present
- name: Install PHP and required extensions
apt:
name: "{{ item }}"
state: present
with_items:
- php-fpm
- php-mysql
- php-curl
- php-gd