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.

Short Ansible Tasks Examples


Task Example Comment

Install nginx with yum 

  • enable it for auto-start
  • start it 
Upload multiple files to remote directory

Add line to a multiple files matching a find criteria.

While reading the documentation I found there is “with_fileglob”  condition, but it works only for local files.

Generate random password with Ansible 


Ansible download file from URL

Ansible rsync with synchronize module


Making recursive file/directory chown/chmod



Create system user/group



Execute command with su by different user


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 – Define Execution Host On Command Line / Running ansible-playbook without specifying inventory file /

With ansible-playbook you are able to define the hostname on the fly.

Here is how to do the trick:


In the same manner you could specify multiple hosts:


Ansible – Change the parallelism execution level pf a playbook

By default Ansible is configured to use “5” forks, which means it will spawn 5 parallel executing SSH sessions for your tasks. (unless you have changed your default ansible.cfg file).

Executing playbooks/tasks on big number of hosts could become really slow operation with this low parallelism level.

Assuming you don’t want to change your default settings, you could provide command line setting to change the fork level:

If you want to change the default setting, you could edit your “~/.ansible.cfg” and modify the following setting:

One more thing you can do for speeding up your playbook (in case you don’t rely on ‘facts’) is to disable the facts gathering.

This could be done by adding the following in your playbook file:




Ansible-Playbook Tips And Tricks

Making tests in Ansible playbook

Some time you may need to create playbook specifically to run certain tests.

Here are some examples for different types of testing

Test if multiple files exists

If some of the defined files does not exist, the playbook will fail, because of the “failed_when” condition.


Test if a given process is running

Another simple test is to check if a given process is running.

This will check if a process with name ‘some-proc’ is in the ps fax output and fail if not.


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


Ansible Ad-Hoc Commands Examples

Ansible ad-hoc commands are useful and effective way for executing command or upload files to group of remote hosts, without the need of writing playbooks.

It is very similar to using “pssh , prsync or pscp” ,but is way more powerful.


Uploading directory to a group of hosts

This will upload “directory_for_upload” on all servers defined inside “server_list.txt” to the destination of /root;


Add line to a file

The following ad-hoc command will add the line “some_text” to /root/some_file, for the manually defined host:



Ansible Debugging

Ansible debug print variables to command line 

Ansible debug dump variables to remote file 

Debug playbook by running in verbose mode:


Debugging Ansible play-book execution by setting ANSIBLE_DEBUG=1



Ansible Useful Resources

Examples of using Ansible “when” conditional 

Optimizing Ansible Performance

Using Ansible With Older Hosts