Ansible – variable data type

Ansible variables support various data types, similar to many programming languages.

We have covered the Ansible variables in our earlier post, Ansible Variables

Ansible does offer us a way to check the type of variables used in our playbooks. To check the variables data types in your playbook, use the “type_debug” keyword.

Example:

- name: Playbook - to get the data types
   hosts: localhost
   vars: 
    y: 15
    server_name: "db_server"
    boolean_value: false
    float_value: 10.10

   tasks:
   - name: Lets get the data type
     debug:
      msg:
       - "Variable : {{y}}, is of type {{y | type_debug}}"
       - "Variable : {{server_name}}, is of type {{server_name | type_debug}}"
       - "Variable : {{boolean_value}}, is of type {{boolean_value | type_debug}}"
       - "Variable : {{float_value}}, is of type {{float_value | type_debug}}"

As you can see in the above code, we need to pass pipe operator | and then type_debug.

ansible variable data type

Ansible – Debug Module

Debug module is used to display the message. The debug module works only on the host nodes and not the remote ones.

Syntax, for debug module using msg attribute.

ansible localhost –m debug –a "msg='this is a test message'"

Syntax, for debug module using var attribute. We are using the ansible default variableinventory_hostname” in the below example.

ansible localhost -m debug -a "var=inventory_hostname"

Ansible allows us to join the variable and string in attribute “msg“. We will need to pass the variable name in double braces {{}}.

The same double braces {{}} will be used to display variables under the message “msg” attribute.

Syntax

ansible localhost -m debug -a "msg='you are using hostname '{{inventory_hostname}}"

Ansible – Facts

Ansible Facts are used to fetch information about the managed nodes. The fact can provide a wide range of information about nodes/servers/systems etc.,

  • Processor details
  • Memory
  • Python version
  • OS release, distribution, etc.

Ansible’s setup module is used to gather these facts/information.

Facts are gathered in both Ansible Ad-Hoc commands and in the Ansible playbooks. In Ad-hoc commands, we need to explicitly run the setup module command, whereas in playbooks facts are by default gathered.

Syntax

ansible localhost -m setup

The setup module default will display all information about your node/system.

The facts information will be displayed in JSON/python dictionary format.

We may not be interested in such large Node/system information. We may be interested only in specific information about the node. Ansible does provide us the option to fetch only the information which is required from the facts.

We can retrieve specific required data from facts by passing the argument filter in your setup module ad-hoc command.

Syntax

ansible localhost -m setup -a "filter=ansible_python_version"
ansible localhost -m setup -a "filter=ansible_distribution"

Types of Ansible Facts

  • Default Facts, the default system information fetched using the setup module are called default facts. These facts do not provide details of any third-party application installed on the node, like MySQL, Splunk, etc.
  • Custom Facts are created to gather information about the third-party applications/services installed on your node/system, like Splunk, Mysql server, etc.

Custom Facts are required when we need information about third-party applications installed on the host or nodes. This information is like version details of third-party applications Splunk, Apache, MySQL server, etc.

Steps to create Custom facts

  1. Create your custom facts file at /etc/ansible/facts.d directory
  2. Create custom facts file with exention .fact under your /etc/ansible/fact.d directory.
  3. Custom facts files can be created using python, and shell script, but the files should be saved with .fact as a file extension.
  4. Set executable permission to the fact files.

Syntax to execute custom facts

ansible localhost -m setup -a "filter=ansible_local"

Ansible – Command vs Shell Module

The Shell Module as well as command modules are used to execute Binary Commands.

The command module is the default module to execute the binary commands, if we do not specify the module name in our ad-hoc command, ansible will by default use the command module.

The command module will execute the commands without proceeding to through the shell.

The command module will not recognize certain variables like $HOME

Stream operators like <,>, &, and | will not work with command modules.

The command module is more secured, compared to the shell module

For example, both shell and command module will have the same outputs,

ansible localhost -m shell -a "uptime"
ansible localhost -m command -a "uptime"

The stream operator, when passed through the shell and command module, will show different results, as the command module doesn’t recognize the stream operators and will throw errors, whereas the same will work fine with the shell module.

Command Module, will throw an error as it will not recognize > stream operator

ansible localhost -m command -a "uptime > uptime.txt"

Shell Module

ansible localhost -m shell -a "uptime > uptime.txt"

Ansible – Default Command module

The Ansible command module is the default module that ansible. It is used to execute Binary Commands.

The command module will execute if we do not specific module name in Ansible Ad-Hoc commands.

ansible localhost -m command -a "uptime"

In the above example, we are passing the module as a command, if we skip the module name, ansible will still execute the command and will pick the command as the default module, the output will be the same for both ad-hoc commands.

ansible localhost -a "uptime"

Ansible – Ad-hoc Command Structure

Ansible provides us the option to either run playbooks or ad-hoc commands on the local and remote nodes through ansible engines.

Let us look at an Ansible ad-hoc command example

> ansible localhost -m shell -a "uptime"

The above command can be divided into the following parts,

  • localhost – You can specify the target host/groups here where you want the command to be executed.
  • -m shell – You define the Ansible module, in our example shell.
  • -a “uptime” – Pass the command arguments in double quotes string.

To run the commands on custom inventory host files,

> ansible localhost -m shell -a "uptime" -i custom_inventory_file

On a similar line, we can create our own ad-hoc commands.