Working with Variables
Learn the different types of variables and the various ways to define and utilize them in Ansible.
We'll cover the following...
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 thename
variable to store theMy name
string value, thedocker_version
variable to store a string value of20.10.14
, and also thepackage_name
variable to store thecurl
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 of8080
and theage
variable to store a numeric value of47
:
Press + to interact
server_port: 8080age: 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
orfalse
conditions. In the following example, we set theis_production
variable totrue
and theenable_logging
variable tofalse
. A common use case for boolean variables is to determine the execution of certain tasks depending on if