Ansible Cheatsheet

Published on Author gryzli

The idea of this post is to make something like a cheatsheet, which I can refer in a future (after I forget almost everything I have learned so far).

So I will try to add the most useful things I’ve found about Ansible.


Ansible Command Line(CLI) Options

There are short list of options, which are very handy to be used as a command line arguments, so I’m going to describe them here.

Ansible – Define playbook variable on command line

You can define (or re-define) playbook variables by using command line options , and the syntax is as follows :

The good think is that all variables defined by the command line, have the highest priority. So whatever you define by this way is going to be the one seen inside your playbook tasks.


Ansible – Doing dry run of a playbook without actual executing it

Doing dry-run executions of a playbook, could be done by using the "--check" option.

Keep in mind that if you have tasks, which depends on previous tasks successfull execution, then most probably¬† “–check” will fail there ( because no tasks is getting actually executed ) .


Ansible – Executing playbook upon limited number of hosts

I have already written about –limit option and limiting task execution on a certain hosts.

Pay attention to the "--list-hosts" command line option, which gives you the ability to just list the hosts upon which the playbook is going to execute, without actually executing anything.

By using "--list-hosts" you could learn the ‘nice way’ if your –limit option does not include the hosts you wanted to.


Ansible – Executing only specific tasks in a playbook

This is another topic I have already written about, and could be found here

Pay attention on the examples given for using '--tags' and '--skip-tags' which really really useful command line options.


Ansible-Playbook Tips And Tricks


Ansible execute task locally

Executing task locally is pretty easy, you need to do the following

1) Add localhost to your inventory file

vim hosts.inventory

2) Define your task with delegate_to


Ansible conditional execution of multiple tasks

You could make conditional execution of a task by using the “when” statement.

Sometimes you may want to execute multiple task conditionally. Adding when to each and every task is not the most optimal way of doing this (even that it will work).

For the purpose you could use block {} , which has the following syntax

Using block: , if your “some_variable” is true, all the defined tasks will get executed.


Ansible decode json output and use it as a variable later

Let say you have a script, which returns JSON output and you want to parse it. Not only to parse it , but use the result of the parse as a variable for later use.

This could be done with something like following code:

1) First lets create some simple php script, which returns JSON output

vim json.php


2) Now create the playbook, that will read the output and parse it

vim tester.yml


Here is the output of executing:

ansible-playbook tester.yml