Ansible Playbook Example5 min read

Introduction

Ansible Playbooks are a script-based method of sending commands to remote systems. Ansible playbooks are scripts that are executed on one or more systems to customize complex system environments and improve flexibility. The playbooks are more like a configuration language than a programming language. In this article, we will look at an example of an Ansible Playbook and learn how to create and execute an Ansible Playbook.

Since Ansible playbook commands are written in YAML format, there isn’t much syntax to learn, however, indentation must be followed. A playbook is a list of plays, as the name implies. You may assign specific roles to certain hosts and other roles to other hosts using a playbook. You can orchestrate several servers in a variety of situations with only one playbook.

Pre-requisites

  1. Ansible should be installed in the Master machine
  2. SSH connection should be established between the Master and the Slave machine (you can refer to our How to establish SSH Connection in Ubuntu Tutorial)

Installing Ansible

First, let us see how to install Ansible. Installing Ansible is very easy. You just need to execute 3 commands. Refer below

  • Install Ansible in Ubuntu/Debian
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible -y
  • For CentOS/Redhat distributions
sudo yum install epel-release
sudo yum update
sudo yum install ansible
  • Install Ansible in Amazon Linux2
sudo amazon-linux-extras install epel -y
sudo yum update
sudo yum install -y ansible

Check Ansible version with the below command

ansible --version

Before moving ahead with ansible playbook example, we need to configure Ansible so that we can connect to our slave instance while executing the playbook. Follow the below steps for the same.


Configuring Ansible

Configuring Ansible refers to setting up the ansible inventory file. The hosts and groups of hosts on which commands, modules, and tasks in a playbook run are described in the Ansible inventory file. /etc/ansible/hosts is the default location of the inventory file. According to your project’s needs, you can create the inventory file in any other location.

Individual hosts and user-defined groups of hosts can be listed in the inventory file. Below is an example of how to set up the Ansible Inventory file.

1. Execute the below command in the master machine to edit the default inventory file,

sudo nano /etc/ansible/hosts

2. Add the below lines at the end of the file,

[webserver]
[email protected]_ip_of_slave

OR

[webserver]
private_ip_of_slave

Note: Instead of private_ip_of_slave, you can provide public IP as well.

configuring ansible

Here I have named the group [webserver] but you can provide any name for the group. You can add multiple slaves in this group. If you have a single slave, then you can provide just the IP address of the slave without creating a group.

Change private_ip_of_slave accordingly. Save and exit with ctrl+x -> -> enter.

3. Execute the below command to check the connection,

ansible -m ping webserver

4. If everything is fine, the output will be shown in green color and now you can execute the playbooks in the master machine.


Ansible Playbook Example

1. Now we will create a playbook in the Master machine to install Java 1.8 on the Slave machine. Execute the below command in the Master,

nano ansible-java.yml

2. Paste the below lines in the file,

---
- hosts: webserver
  become: true
  tasks:
  - name: Run update
    apt:
      update_cache: true
  - name: Install jdk
    package:
      name: openjdk-8-jdk
      state: present

Make sure the indentation is as below,

ansible playbook

Save and exit by pressing ctrl+x -> y -> enter key

Note: The above playbook is for Ubuntu. If you are using CentOS, you can use the below playbook,

---
- hosts: webserver
  become: true
  tasks:
  - name: Run update
    yum:
      update_cache: true
  - name: Install jdk
    package:
      name: java-1.8.0-openjdk
      state: present

3. Now we will execute the playbook with the below command,

ansible-playbook ansible-java.yml
ansible playbook execution

4. Let us check if Java is installed in the Slave machine by executing the below command in the Slave machine,

java -version
java version

Done. We have successfully executed the ansible playbook and installed java on the slave machine.


Share:

Leave a Reply