Ansible: Default Variables Values

In Ansible it is recommended to set default values for variables to avoid undefined-variable errors:

The task includes an option with an undefined variable.

The default variables for a role can be defined in the defaults/main.yml file that might look something like this:

nginx_version: 1.20.1
nginx_service_name: "nginx.service"

Ansible will use the default values only if those variables are not defined anywhere else.

Cool Tip: Ansible Playbook – Print Variable & List All Variables! Read more →

Set Default Values for Variables in Ansible

Another option is to set the default values for the variables using the Jinja’s default filter:

"{{ some_variable | default('default_value') }}"

This is often a better approach than failing if a variable is not defined.

Passing in a true as the second parameter will return the default value if the value is defined but blank:

"{{ some_variable | default('default_value', true) }}"

Cool Tip: How to check if a file exists in Ansible! Read more →

Ansible: Run Handler Immediately (Force)

Handlers in Ansible are special tasks that only get executed when triggered by another tasks via the notify directive.

By default, handlers run not immediately but only after all the tasks in a particular play have been completed.

This makes sense, as the handler will only run one time, regardless of how many tasks have notified it.

For example, if multiple tasks update a configuration file and notify a handler to restart Nginx, Ansible will run this handler only one time to avoid unnecessary restarts.

Cool Tip: Enable DEBUG mode and increase VERBOSITY in Ansible! Read more →

Force Ansible Handlers To Run Immediately

If you want to force handlers to run immediately, without waiting for the end of the play, add a meta: flush_handlers task:

tasks:
  - name: "Some task"
    command: ...

  - name: "Flush handlers"
    meta: flush_handlers

  - name: "Some other task"
    command: ...

The meta: flush_handlers task immediately triggers any handlers that have been notified at that point in the play.

Ansible – “sudo: a password is required” [SOLVED]

If you run an Ansible task that requires a privilege escalation, i.e. with become: true, you may get an error “sudo: a password is required”.

This happens when Ansible needs to run some command with sudo but it doesn’t know the password.

In this note i will show how to make the ansible-playbook command prompt for a password at a runtime and how to pass the password non-interactively during automated deployment. (more…)

Install Ansible on Ubuntu – APT Repository

Ansible is a popular agent-less automation tool used to configure local or remote systems and deploy applications.

The easiest way to install the latest version of Ansible on Ubuntu is to get it from the official APT repository.

This short note shows how to install Ansible on Ubuntu, check its version, locate the configuration file and test the connection to a remote host from the command line. (more…)

Ansible: Human-Readable Output Format

By default Ansible sends output of the plays, tasks and module arguments to STDOUT in the format that is not suitable for human reading.

Starting from Ansible 2.5, the default output format can be changed to a human-readable using the callback plugin.

This short note shows how to change the default Ansible’s JSON output format to the more human-friendly YAML format. (more…)

Ansible: Enable Debug and Increase Verbosity

During troubleshooting of Ansible issues it is useful to know how to enable debug mode and increase the level of verbosity.

To enable debug and increase verbosity in Ansible you can pass the corresponding environment variables on the command line or define these settings in Ansible configuration file.

Warning: The debug output can also include secret information despite no_log settings being enabled, which means debug mode should not be used in production.

(more…)

Ansible: When Variable Is – Defined | Exists | Empty | True

In Ansible playbooks, it is often a good practice to test if a variable exists and what is its value.

Particularity this helps to avoid different “VARIABLE IS NOT DEFINED” errors in Ansible playbooks.

In this context there are several useful tests that you can apply using Jinja2 filters in Ansible.

In this article, i’ll show the examples of how to test a variable in Ansible: if it exists or not, if it is empty or not and if it is set to True or False. (more…)