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 […]
ansible
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 […]
Ansible Vault: Encrypt | Decrypt a String
Ansible files may often contain different secret strings e.g. passwords, tokens, keys etc., and the good practice is not to store them in a plain text but to encrypt them. This is especially important if your Ansible files are tracked in a source control system. This note shows the examples of how to encrypt and […]
Ansible: `Cat` File – Print/Read File Content
The cat command in Linux is used to print a file’s content to a standard output. Ansible can also be used to connect to a remote Linux or Windows host and print the content of a remote file or save it to a variable. This note shows the examples of how to “cat” a file […]
Ansible: Create File With Content
Ansible has built-in modules that can be used to create an empty file or a file with a content. These common tasks can be easily done using the Ansible’s file module (or win_file for Windows targets) and copy module (or win_copy for Windows targets). In this short note i will show the examples of the […]
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 […]
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 […]
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 […]
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 […]
Ansible: Comment Out & Uncomment Lines in a File
To comment out or uncomment lines in a file in automated deployments we can use the Ansible’s replace module. This module is used to replace all instances of a pattern within a file and perfectly suits our needs. In this note i will show several examples of the Ansible tasks for commenting out and uncommenting […]