In playbooks, the variable is very similar to using the variables in a programming language. It helps you to assign a value to a variable and use it anywhere in the playbook. You can put the conditions around the value of the variables and use them in the playbook accordingly.
Creating Valid Variable Names
Before start using variables, it's important to know what valid variable names are.
Variable names should be letters, numbers, and underscores. The variable should always start with a letter.
foo_port and foo2 both are the correct or valid variable names.
Foo-port, foo port, foo.port, and 10foo all are invalid variable names.
YAML supports dictionaries that map keys to values. For instance:
Then you can reference a specific field in the dictionary using either bracket notation or dot notation:
Both will reference the same value "one". But, if you choose to use dot notation, be aware that some keys can cause problems because they collide with the attributes and methods of python dictionaries. You should use bracket notation instead of dot notation if you use keys which start and end with two underscores or any of the known public attributes:
In the above example, defined a variable name tomcat_port and assigned the value 8080 to the variable and can use it in your playbook wherever required.
The below code is from one of the roles (install-tomcat), such as: