Add beauty to long Jinja2 chains Do you have a long-long Jinja2 expression with chain of filters? Need to scroll screen? Have linter errors about 80+ long lines? Wait no more! Use YAML block scalar syntax: - set_fact:
Define what `diff` means for Ansible task I was inspired to dig into this topic by this ServerFault question. So you have some Ansible task that supports diff output (e.g. copy). But imagine that you copy files that are
Idempotent shell command in Ansible Ever wanted to make your command or shell call in Ansible playbook idempotent and avoid those nasty yellow changed lines in run logs? Wait no more! Here's bash trick for you: - name:
Print to standard output without escaping If you want to print some values during debugging or show some message to user with debug module, you may notice that Ansible outputs JSON-encoded strings. There are some tricks to overcome this.
Process complex variables with set_fact and with_items Need to process complex data? Like rearranging keys in a list of nested dicts. No problem! You can do it with set_fact, with_items and register. Let's take this list of users
SSH with port knocking Does your target server require port knocking to enable SSH access? No problem. You can try this handy ssh_pkn connection plugin. It is based on Ansible stock ssh plugin. It tries to
Force "check mode" for single task Need to run some preparation tasks on your host in case of package update? You can trick Ansible to run single task in check mode to predict the result with check_mode: yes
Idempotent random number Need a random number for your Ansible playbook? But want to be idempotent on subsequent runs? There is an answer! Let's say you want to register cron jobs on a bunch of servers
Avoid Ansible command warning Sometimes you need to execute a shell command on remote host, but Ansible gives you an annoying warning, for example: [WARNING]: Consider using service module rather than running service If it is a
Template as variable If you need to use a template without file generation, there is a handy lookup plugin for you: template. You can use it with lookup function: - debug: msg="{{ lookup('template', 'mytemplate.
Prevent playbook execution without tags If you have a playbook or a role with extensive tags usage (e.g. install/update/uninstall) and want to prevent its execution without tags (in which case every task will be executed)
Offline docs with ansible-doc Did you know that you can get Ansible module's documentation with ansible-doc? $ ansible-doc --help Usage: ansible-doc [options] [module...] Options: -h, --help show this help message and exit -l, --list List available modules -M
Execute detached process with Ansible How to execute long running process on remote host with Ansible? There are several ways to achieve this. Let's look into them: Async task You can use async option for your task with
long-read Troubleshooting Ansible connection issues How to handle this: managed-host | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh.", "unreachable": true } or other connection error when