String Variables
String variables in Ansible represent sequences of characters. They can be defined in a playbook, an inventory file, or even passed via command-line arguments. For example, the snippet below defines a string variable calledusername with the value "admin":
Number Variables
Number variables store integer or floating point values and are often used in mathematical operations. In the example below,max_connections is a number variable set to 100:
Boolean Variables
Boolean variables in Ansible hold truthy or falsy values and are primarily used in conditional statements. For instance, the following example defines a Boolean variabledebug_mode set to true. For a comprehensive list of acceptable truthy and falsy values, refer to the Ansible Documentation.
List Variables
List variables store an ordered collection of values. Lists in Ansible can contain items of any type and enable referencing the entire list or individual elements using their index with double curly brackets. Below is an example playbook that demonstrates how to use list variables effectively:Dictionary Variables
Dictionary variables store collections of key-value pairs. Both keys and values can be of any type. In the example below, the variableuser is a dictionary containing two key-value pairs. You can reference the entire dictionary or access a specific value via its key using double curly brackets.
Understanding these variable types is fundamental to building efficient and dynamic automation tasks with Ansible. Use the appropriate type based on your data requirements to enhance the clarity and maintainability of your playbooks.