Ansible Gotchas

Because I keep forgetting theseā€¦

Patterns for hosts with dynamic inventories:

- hosts: tag_Name_name_here:&tag_Whatever_whatevers

Find more info here.


Get an instance-id from a list returned by ec2_remote_facts:

{{ item.1.id }} # if using with_indexed_items
{{ item.0.id }} # if using with_items.

Parse command result in json format:

First, register the result of a command in a variable:

command: whatever_here
  register: command_result

Then you can parse the result with:

(command_result.stdout | from_json).prop1.prop2.xxxx

Some ansible.cfg useful options:

[defaults]
# remove the cow from the log
nocows = 1
# prevent .retry file creation
retry_files_enabled = False
# for custom python modules
library = ./modules/

Minimal filter:

Because sometimes, it is simpler!

#!/usr/bin/python

class FilterModule(object):
    def filters(self):
        return {
            'my_filter1': self.my_filter1,
            'my_filter2': self.my_filter2
        }

    def my_filter1(self, param, another):
        # do stuff here, store in result
        return result


    def my_filter2(self, param, another):
        # same
        return result

Put the code in a filter_plugins directory at the root of your ansible project files.