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...
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 thenamevariable to store theMy namestring value, thedocker_versionvariable to store a string value of20.10.14, and also thepackage_namevariable to store thecurlvalue:
Note: Simple strings without special characters or spaces do not have to be enclosed within quotes.
- 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_portvariable to specify an integer value of8080and theagevariable to store a numeric value of47:
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
trueorfalseconditions. In the following example, we set theis_productionvariable totrueand theenable_loggingvariable tofalse. A common use case for boolean variables is to determine the execution of certain tasks depending on if