...

/

Working with Variables

Working with Variables

Learn the different types of variables and the various ways to define and utilize them in Ansible.

Variable types

Variables can be of different types, including the following:

  • Basic type variables, such as strings, integers, booleans, lists, and dictionaries.
  • Nested variables.
  • Null variables.
  • Magic variables.
  • Connection variables.
  • Facts.

Basic types

  • Strings: These variables are used to store and manipulate text-based data. Strings can be defined using single (' ') or double (" ") quotes. In the following example, we’re using the name variable to store the My name string value, the docker_version variable to store a string value of 20.10.14, and also the package_name variable to store the curl value:

Note: Simple strings without special characters or spaces do not have to be enclosed within quotes.

Press + to interact
name: 'My name'
docker_version: "20.10.14"
package_name: curl
  • Integers: These variables are used to store numerical values. We can use integer variables to define port numbers, specify counts, or represent any other numeric parameter. In the example below, we’re using the server_port variable to specify an integer value of 8080 and the age variable to store a numeric value of 47:
Press + to interact
server_port: 8080
age: 47

Note: It’s important to note that integer values don’t need to be enclosed inside quotes. ...

  • Boolean: These variables are used to store values representing true or false conditions. In the following example, we set the is_production variable to true and the enable_logging variable to false. A common use case for boolean variables is to determine the execution of certain tasks depending on if