Environment Variables in the Local Machine/Ansible Control Machine First, let us see how to work with the environment variables on the server which acts as the ACM. Accessing the environment variables You can access the environment variables available on the local server via the ‘lookup’ plugins, which allow you to access the system […]
Tutorial
Working with Ansible facts – retrieving facts
What are Ansible facts? Ansible facts are pieces of information regarding the remote systems to which you have connected. It contains information like IP addresses, the OS installed, Ethernet devices, mac address, time/date related data, hardware information etc. These are very useful for scenarios where you need to take conditional operations based on […]
Working with Ansible filters
You can use Ansible filters when you need to format data inside template expressions. You are provided with a set of filters that are shipped with Ansible and another set provided by Jinja2 template engine. You can also build your own filters. In the following sections, I will give a list of useful filters and […]
User inputs with Ansible Prompt with examples
Working with Ansible Prompts. Sometimes you need input from the user to decide how the playbook should run. Maybe you need to name a directory. Maybe there are plays written for installing multiple versions. And you can install one version based on the user input. Or there may be some data which is sensitive you […]
How to zip files and folders with Ansible
Working with Ansible archive module Until version 2.3, Ansible didn’t support an Archive module(It was available in the developer edition for some time though). Before that, we had to use the shell module or command module to zip a directory in Ansible. From 2.3 you can use the Ansible archive module to compress the files […]
Working with Ansible variables in conditionals
Variables are necessary when you need to store the output of a task and need to access it in some other tasks. They are mostly used when dealing with a conditional statement where you can opt to run the task based on the output of another task. Here I will show some examples of using […]
Using the Ansible find module to search for files/folder
Ansible find module is used when you need to retrieve a list of files in the remote server which matches some conditions like name, size, etc. You will have to provide the path(s) to the remote server where the search should be done. It also supports searching for directories and links. The module returns the list of […]
Working with date and timestamp in Ansible
Getting the system timestamp is very useful in multiple types of scenarios while scripting. You can use it for just printing out the time for logging purpose, use it for naming a directory or use it in a conditional statement to control the flow of your program. Ansible provides access to the remote system time […]
Working with Ansible loop
Ansible loop provides a lot of methods to repeat certain tasks until a condition is met. A basic example which can be used to install a lot of Linux packages can be written like the below example. – name: Ansible Loop example apt: name: “{{ item }}” state: present with_items: – python3 – ca-certificates – […]
How to delete multiple files / directories in Ansible
There are multiple methods in Ansible by which you can delete a particular file or directory, delete all files in a directory, delete files using regex etc. The safe way is to use the Ansible file module. You can also use the shell module to achieve the task. But it is not idempotent and hence […]