Variables in Ansible
Remote Environment in Ansible
From bib. source
[…] if you need to set some environment variables for your remote user account, you can do that by adding lines to the remote user’s
.bash_profile, […]
That is, one way to handle variables in Ansible is to simply set them on the target server as environment variables for the shell session. In such a case, all subsequent Ansible tasks would have access to those variables provided they rely on the shell module (Geerling 2023, 85-86). In addition, and environment can be set for specific tasks or sets of tasks in a playbook by using the environment field or option on the given task or on each task, e.g. (Geerling 2023, 87-88):
- name: Download a file, using example-proxy as proxy
get_url:
url: http://www.example.com/file.tar.gz
dest: ~/Downloads
environment:
http_proxy: http://example-proxy:80/Creating Variables in Ansible
There are four ways to create variables for playbooks in Ansible:
- Through command line, using the option
extra-varsas a flag for the commandansible-playbook, which can take a comma-separated list of key-value pairs conjoined by equal signs, e.g.foo=bar,bar=beer(Geerling 2023, 90). - Inline within a file, which comes in two separate cases:
- The playbook case, wherein one uses a top-level
varssection or field, that includes the variable names as fields or keys, followed by their values (Geerling 2023, 90). Since the playbook is in YAML, this must obey its syntax, wherein field and value are separated by a colon and space (Ibid). - The inventory file case, wherein one uses a key-value pair conjoined with an equal sign, as required by INI syntax, following right after an enlisted hostname, FQDN, or public IP address (Geerling 2023, 92-93).
- The playbook case, wherein one uses a top-level
- As a group variable within an inventory file, wherein one follows a group name in the inventory file with a colon and “vars”, and then enlists one key-value pair conjoined by equal sign per line.
- Externally, via separate YAML files called variable files containing the variables at top-level.
- In the case of command line, using the option
extra-varsas a flag for the commandansible-playbookwhich can take a YAML file path as an argument, e.g.@vars.yaml(Geerling 2023, 90). - In the case of playbooks, this would be done by using a top-level
vars_filessection or field that lists YAML file paths (Geerling 2023, 90-91). - In the case of inventory files, the separate YAML files are simply placed in sub-directory
host_varsof the project root or the sub-directorygroup_varsof the project root for what otherwise would be inline or group inventory file variable definitions (Geerling 2023, 93). Each of these YAML files must have a filename equivalent to the given host or group it is to apply to, without any YAML file extension (Ibid).
- In the case of command line, using the option
Output Registration
The values of remote environment variables can be stored as Ansible playbook variables by registering the remote command output consequent of a given playbook task being played, e.g. (Geerling 2023, 86):
- name: Get the value of an environment variable we just added.
shell: 'source ~/.bash_profile && echo $ENV_VAR'
register: fooThe value of the register field acts as the playbook’s variable name storing the value outputted by the given task. In the above, it would be foo.
The ability to register output also allows using the output of one task to determine the input or execution of another task in an Ansible playbook (Geerling 2023, 94).
Variable Usage
To get or output the value of simple variables in Ansible (i.e., variables gathered by Ansible, defined in inventory files, playbook files or variable files), their variable names are padded with single-space (two spaces total) and wrapped with double curly brackets (Geerling 2023, 86 & 94):
- name: Print the value of the environment variable.
debug:
msg: "The variable is {{ foo.stdout }}"For variables with values of type list or array, you can “access the […] item in that array with either of the following syntax” (Geerling 2023, 95):
foo[0]
foo|firstRegarding these two approaches to grabbing an element from our array variable (Geerling 2023, 95):
From bib. source
[…] the first line uses standard Python array access syntax (‘retrieve the first (0 indexed) element of the array‘), whereas the second line uses a convenient filter provided by Jinja.
Structured Variable Usage
Meanwhile, “for larger and more structured variables” (Ibid):
From bib. source
[…], you can access any part of the array by drilling through the array keys, either using bracket (
[]) or dot (.) syntax.
E.g. (Geerling 2023, 96),
{{ ansible_ath0.ipv4.address }}
{{ ansible_eth0['ipv4']['address'] }}To retrieve inventory file inline or group variables for a given host, the hostvars variable may be used, as it is a larger, structured variables with array keys for the different hosts and their respective variables; for example (Geerling 2023, 98):
{{ hostvars['host1']['admin_user'] }}environment_variable environment_variables variable_name key-value_pair key-value_pairs key Yet_Another_Markup_Language YAML_Aint_Markup_Language filepath file_path configuration_file configuration_management configuration IP_address Internet_Protocol_address Internet_Protocol IP inventory_file inline_variable group_variable file_extension files variable_files simple_variables data_type structured_variable associative_array dictionary
bibliography
- “Ansible Playbooks - Beyond the Basics.” In Ansible for DevOps: Server and Configuration Management for Humans, 2nd ed., 84–122. Leanpub, 2023.