Ansible Playbook Example

In this article we are going to talk about how to write our first Ansible playbook Example.

What is Ansible Playbook?

Ansible modules execute tasks. One or more Ansible tasks can be combined to make a play. Two or more plays can be combined to create an Ansible playbook. Ansible playbooks are lists of tasks that automatically execute against hosts. Groups of hosts form your Ansible inventory.

Each module within an Ansible playbook performs a specific task. Each module contains metadata that determines when and where a task is executed, as well as which user executes it.

To write your ansible playbook you should know what is inventory, modules and tasks.

Ansible Inventory: 

So the inventory is nothing but a list of hosts or group of hosts.

The default location of your hosts inventory file is /etc/ansible/hosts

Ansible Module:

Modules are the programs that perform the actual work of the task of a play.

These modules we use under task sessions in our playbook.

Ansible Tasks:

A tasks nothing but a set of instructions which can be performed by using modules so in the task we usually called modules

Ansible Playbook Example

Now let’s write our first ansible playbook

So we are going to write an Apache playbook.

We need to name it as .yaml or .yml extension here we are going to use install-apache.yml because it is going to install apache2.

Now let me create my first playbook using below command:

sudo nano install-apache.yml

---
- name: Setting up Apache webserver
  hosts: devopshint
  become: true
  tasks:
  - name: Install apache2
    apt: name=apache2 update_cache=yes state=latest

Lets understand ansible playbook 

  • Ansible playbook starts with a (—) three dash
  • Next line starts with (-) single dash. Name is optional here.
  • Then hosts nothing but lists of the server are target nodes. Hosts like all or name of group.
  • If you want to use become a root then you can use become true 
  • Then next is task in task we are going to perform operators. Tasks are a list of the actions which our playbook performs on a remote host.
  • So here we are going to use the apt module. you can go to the official page of ansible where you can check modules and their parameters. And if you scroll down you can see examples of ansible playbooks

And in ansible playbook indentation is very important.

Now if you want to check this playbook successfully work or not then you can run the command:

ansible-playbook apache-install.yml --check

Here i am using by default hosts file that’s why here im not mention my hosts file that means inventory file

If you have created new inventory file then you can run the below command

ansible-playbook -i myinventory apache-install.yml --check

myinventory is your inventory name

When you mention –check in your command it only check this task performable or not.

Run Ansible Playbook

Now run our ansible playbook using below command 

ansible-playbook install-apache.yml

Output:

PLAY [Setting up Apache webserver] ******************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************
ok: [node1]
ok: [node2]

TASK [Install apache2] ******************************************************************************************************************
ok: [node1]
ok: [node2]

PLAY RECAP ******************************************************************************************************************************
node1                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
node2                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Now let’s check in browser apache2 installed or not in our remote hosts

You will get output like this:

Ansible Playbook Example 1

Conclusion:

In this article we have covered What is Ansible Playbook, Ansible Playbook Example, Ansible Inventory, Run Ansible Playbook.

Related Articles:

Reference:

Ansible Playbook official page

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern in Product Company. Likes to share knowledge.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap