top of page

How To Write Your First Ansible Playbook



What is Playbook ?


Ansible playbook is an organized unit of scripts that defines work for a server configuration managed by the automation tool Ansible. Ansilble Playbook is a file which is written in YAML (Yet Another Markup Language). Playbook is one of core feature of Ansible which instruct Ansible what to execute.


In this tutorial we'll use Ansible COPY module to see how we can copy one file from Ansible Tower to client. This will give you basic hands on its working.


Make sure you have copy module install on Tower :


[root@tecgeek ~]# ansible-doc copy |head -n 8

> COPY (/usr/lib/python2.7/site-packages/ansible/modules/files/copy.py)

The `copy' module copies a file from the local or remote machine to a location on the remote machine. Use the [fetch] module to copy files from remote locations to the local box. If you need variable interpolation in copied files, use the [template] module. Using a variable in the `content' field will result in unpredictable output. For Windows targets, use the [win_copy] module instead.

* This module is maintained by The Ansible Core Team

[root@tecgeek ~]#


Ansible Playbook Format :


All Ansible playbook file begin with - - - and ends with . . .

This format shows starting & ending point of palybook.

All members of a list are lines beginning at the same indentation level starting with a dash and a space ie "- "

Every playbook file must have the file extension .yml


Sample Playbook Of Copy Action :


[root@tecgeek ~]# cat /home/siddhesh/ansible/first_copy.yml

---

- hosts: chatbot

tasks:

- name: copy practice.txt to remote server

copy:

src: /opt/siddhesh/practice.txt

dest: /usr/local

[root@tecgeek ~]#


Let's create /opt/siddhesh/practice.txt file below content.


[root@tecgeek ~]# cat /opt/siddhesh/practice.txt

This is test content file of Ansilbe Tower.

Test config 1

Test config 2

Test config 3

[root@tecgeek ~]#


[root@tecgeek ~]# cat /etc/ansible/hosts

[chatbot]


[root@tecgeek ~]# cat /etc/hosts

127.0.0.1 localhost

[root@tecgeek ~]#


Play Your First Playbook using Ansible-playbook :


Ansilbe-playbook is the tool used to run playbook.


[root@tecgeek ~]# ansible-playbook /home/siddhesh/ansible/first_copy.yml

PLAY [chatbot]

****************************************************************************

TASK [Gathering Facts]

****************************************************************************

ok: [ansible1.tecgeek.info]

TASK [Ansible copy file to remote server]

****************************************************************************

changed: [ansible1.tecgeek.info]

PLAY RECAP

*****************************************************************************

ansible1.tecgeek.info : ok=2 changed=1 unreachable=0 failed=0

[root@tecgeek ~]#


Verify File On remote client.


[root@tecgeek ~]# ssh root@automation1.tecgeek.info

[root@automation1 ~]# hostname

[root@automation1 ~]# find /usr/local -type file -name practice.txt

/usr/local/practice.txt

[root@automation1 ~]#









bottom of page