Getting started with Ansible playbooks

Vidhita Kher
HackerBay
Published in
3 min readMar 25, 2018

--

I started my journey in Ansible playbook automation with the idea of moving away from traditional script automation tools and deployment models on virtual machines to something more simple, sequential , agent-less and powerful in terms of the configuration management and workflow orchestration required in Cloud environment. And ANSIBLE was the answer.

Below is the step by step guide on how to get started with playbook creation, execution and troubleshooting in Ansible:

Creating a Playbook — Know How:

I began my first playbook creation by converting the existing Network Shell script for product installation on virtual machine in cloud environment to a playbook.

Playbooks are expressed in YAML format and mostly consists of a series of sequential ‘tasks’. At a very basic level, a task is nothing more than a call to an ansible module , which are predefined set of system commands ,resources etc.

To have an overview of playbook creation , here is the official link to the documentation on the same: AnsiblePlaybooks

Steps to execute playbook:

1. The first step in executing a playbook is to check for any syntax errors while writing the playbook

ansible-playbook test.yml — syntax-check

2. Below is the step to execute a playbook

ansible-playbook test.yml

3. For detailed debugging step by step for a playbook use this command:

— ansible-playbook test.yml — step -vv

Troubleshooting a playbook:

Most common errors faced while executing a playbook are:

1. no such file or directory

— Usually for this kind of error, you need to look if the permissions of the file is set to execute for that particular file

— Another thing to check is if you are running the playbook as root user or any other non-root ansible user which has the required privileges to execute the playbook.

— Another basic error could be that the file is not copied properly to the target server before execution

2. Permissions denied

— You may need to check if the id you’re using is having enough permissions to execute the playbook or you need to be root otherwise.

Best practices to be followed:

1. Copy the required sh script,tar, zip files whatever is required for installation of any of the software/products on the virtual machines for which we are writing the ansible playbook in the proper folder structure in your GIT repository

2. It is a best practice to provide the name all the tasks which are performed in the playbook to increase the readability of the same by another user.

3. Always mention the target hosts in your playbook at the top before beginning the actual playbook. Please make sure you may also need to add the same in /etc/ansible.cfg file on the MAIN server.

4. Throughout the development of any playbook if you’re stuck with any command, use the official guide docs.ansible.com to lookup for any command, it’s syntax and various parameters used along with it.

5. For e.g.: for executing a shell script via Ansible playbook , we have two modules available in ansible: Shell module and Script module.

Pro Tip: Using the -step -vv ( verbose) while playbook execution can help playbook developers troubleshoot and optimize if at all required their scripts interactively and — when things go wrong — provide valuable triage information.

Automation frees admins up to focus on efforts that help deliver more value to the business by speeding time to application delivery, and building on a culture of success.

Leave me a comment below if you’re facing any issues while playbook creation,execution, troubleshooting etc in Ansible and I will be happy to help!.
Happy Learning ! :)

--

--