Table of Contents
In this tutorial I will take you through the steps to write your first ansible playbook. Ansible is a great tool for managing a large number of servers. With it, you can create incredibly flexible, automated tasks that run from a centralized server to act on remote hosts. With these tasks, called playbooks, you can undertake admin duties that would otherwise require you to log onto each remote machine manually and run those commands individually.
First Ansible Playbook
What are Ansible Playbooks
Playbooks are Ansible’s configuration, deployment, and orchestration language. They can describe a policy you want your remote systems to enforce, or a set of steps in a general IT process.
Prerequisites
You need to have one Ubuntu Server with Ansible installed and atleast one node added in the hosts. You can go to my How to install Ansible on Ubuntu Post and follow the steps to install the Ansible if not already installed.
Check the Ansible Version
root@test-VirtualBox:~# ansible --version
ansible 2.9.2
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.15+ (default, Oct 7 2019, 17:39:04) [GCC 7.4.0]
Ansible Playbook
Here you to need understand a few things before you run your first ansible playbook. A Playbook is a readable format instructions to tell the modules to perform a task. You must understand the following to run your Playbook successfully:
1. The target
Playbooks are responsible for interacting with the modules. This is why Playbooks formats are in readable format through which you’re telling the automated parts the way you want to configure your tasks. You need to know the target you’re running the Playbook against.
2. The tasks
Tasks are executed in order, one at a time, against all machines matched by the host pattern, before moving on to the next task. It is important to understand that, within a play, all hosts are going to get the same task directives. It is the purpose of a play to map a selection of hosts to tasks.
Playbooks start with the YAML three dashes (—) followed by:
Name: good for keeping the Playbooks readable
Hosts: identifies the target for Ansible to run against
Here we will use a simple example playbook to install Nginx Server using test.yml.
root@test-VirtualBox:~# cat test.yml
---
- name: Install nginx
hosts: testservers
tasks:
- name: Add epel-release repo
yum:
name: epel-release
state: present
- name: Install nginx
yum:
name: nginx
state: present
- name: Insert Index Page
template:
src: index.html
dest: /usr/share/nginx/html/index.html
- name: Start NGiNX
service:
name: nginx
state: started
root@test-VirtualBox:~# cat index.html <html> <body> <h1>Hello, CyberITHub!</h1> </body> </html>
Run Ansible Playbook
To run your first ansible playbook you need to use below command to run the test.yml file.
root@test-VirtualBox:~# ansible-playbook test.yml -c camico --user=root --extra-vars ansible_sudo_pass="test@123"
PLAY [Install nginx] ***************************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [192.168.0.106]
TASK [Add epel-release repo] *******************************************************************************************************************************************
ok: [192.168.0.106]
TASK [Install nginx] ***************************************************************************************************************************************************
ok: [192.168.0.106]
TASK [Insert Index Page] ***********************************************************************************************************************************************
changed: [192.168.0.106]
TASK [Start NGiNX] *****************************************************************************************************************************************************
changed: [192.168.0.106]
PLAY RECAP *************************************************************************************************************************************************************
192.168.0.106 : ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Also Read: How to Install Ansible on Ubuntu 18.04
Reference: Ansible Documentation