Boolean Values in YAML
YAML allows for various representations of Boolean values. In Ansible, options can be set using yes/true/TRUE/True as well as no/false/FALSE/False. All these forms are interpreted the same way. For instance, the following playbook tasks are functionally equivalent:Ansible does not differentiate between the different representations of Boolean values. Choose the style that best fits your project conventions.
The YAML Document Separator (---)
The three dashes (---) at the beginning of a YAML file mark the start of a document. While this marker is optional for single-document files, it is especially useful when merging multiple YAML files into one document. Consider the following example:Using Double Curly Braces and Variable Interpolation
Ansible playbooks process variables using Jinja2 templates, and variable interpolation is typically done with double curly braces (e.g.,{{ variable_name }}). However, there are certain nuances to keep in mind:
- Options that explicitly expect a variable name (such as the
varparameter of the debug module or within thewhencondition) should be used without double curly braces. - When combining text with variables or when a variable is the sole content of a field, enclose it in quotes with double curly braces.
- The
msgfield outputs the value ofdns_server_ipusing double curly braces. - The
varparameter and thewhencondition expect the variable names directly without interpolation. - The
with_itemsdirective uses double curly braces within quotes to indicate a list variable.
When constructing playbooks, always assess the context in which a variable is used. Using or omitting double curly braces incorrectly may lead to unexpected behavior.
Ansible SSH Connection Variables
A common source of confusion is whether to useansible_ssh_pass or ansible_password for SSH passwords. Although both work, ansible_ssh_pass is considered a legacy option. The updated and recommended variable is ansible_password. Here are two inventory file examples:
Legacy approach:
For new playbooks and configurations, always use
ansible_password to ensure compatibility with current best practices.