Ansible: Skip Parameter If Variable Not Defined

In Ansible, if you run a task that has some module’s parameter with a variable that is not set you will get “The task includes an option with an undefined variable” error.

If for some reason you can’t or don’t want to define this variable, you can make it optional.

In this case Ansible will ignore the parameter with the optional variable if it is not defined.

This note shows how to skip a module’s parameter in Ansible if a variable is not defined. (more…)

Ansible Playbook “Dry Run” – Check Mode

An Ansible’s “dry run” or check mode feature is just a simulation of the execution of the ansible-playbook command.

When the ansible-playbook command is executed in the check mode, it will not make any changes on a target system.

With the Ansible’s “dry run” feature you can see if the target system is getting changed or not before actually applying the playbook and making any changes on the target host. (more…)

Ansible: Run Shell Command on Remote Host

To run a command on a remote host we can use the Ansible’s shell module (or win_shell for Windows targets).

By default, the shell module uses the /bin/sh shell to execute commands, but it is possible to use other shells such as /bin/bash by passing the executable argument.

In this note i will show the examples of Ansible tasks running shell commands on remote hosts and also i will show how to execute ad hoc commands to perform quick tasks without having to write a playbook. (more…)

Ansible: `lsb_release` Variable

The lsb_release -sc command displays the release codename of a Linux distribution in a short format.

You can often meet this command while adding APT repositories, e.g. sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main".

To get the output of the lsb_release -sc command in Ansible, there is no actually a need to execute this command, as the ansible_distribution_release fact in Ansible already stores this information in the required format.

This note shows how to refer to the output of the lsb_release -sc command in Ansible using the ansible_distribution_release fact. (more…)

Ansible: Set Variable In Task

Variables in Ansible can be defined in many different places, such as in inventory, in playbooks, in reusable files, in roles, and at the command-line.

Ansible also allows to set variables directly in a task by using the set_fact module.

Variables defined using the set_fact become associated with the host that the task is running against, that makes them available across subsequent tasks for the entire duration of the play. (more…)

Ansible: Compare Version Numbers – Examples

As Ansible is widely used as an automated provisioning and deployment tool, it is quite often required to compare version numbers of different software components.

For example, you may need to check the current version of an application and compare it with the latest one to take a decision if update is required.

To compare versions in Ansible we can use the version test, and in this note i will show the examples of how to use it. (more…)