include_vars¶
Collection Note
This module is part of the ansible.builtin collection. To install the collection, use:
Added in version1.4.
Synopsis¶
- Loads YAML/JSON variables dynamically from a file or directory, recursively, during task runtime.
- If loading a directory, the files are sorted alphabetically before being loaded.
- This module is also supported for Windows targets.
- To assign included variables to a different host than C(inventory_hostname), use C(delegate_to) and set C(delegate_facts=yes).
Parameters¶
| Parameter | Defaults / Choices | Comments |
|---|---|---|
| depth int |
When using O(dir), this module will, by default, recursively go through each sub directory and load up the variables. By explicitly setting the depth, this module will only go as deep as the depth. Version Added: 2.2 |
|
| dir path |
The directory name from which the variables should be loaded. If the path is relative and the task is inside a role, it will look inside the role's C(vars/) subdirectory. If the path is relative and not inside a role, it will be parsed relative to the playbook. Version Added: 2.2 |
|
| extensions list / elements=str |
Default: ['json', 'yaml', 'yml'] |
List of file extensions to read when using O(dir). Version Added: 2.3 |
| file path |
The file name from which variables should be loaded. If the path is relative, it will look for the file in C(vars/) subdirectory of a role or relative to playbook. Version Added: 2.2 |
|
| files_matching str |
Limit the files that are loaded within any directory to this regular expression. Version Added: 2.2 |
|
| free-form | This module allows you to specify the O(file) option directly without any other options. There is no O(ignore:free-form) option, this is just an indicator, see example below. |
|
| hash_behaviour str |
Choices: replace, merge | If set to V(merge), merges existing hash variables instead of overwriting them. If omitted (V(null)), the behavior falls back to the global C(hash_behaviour) configuration. This option is self-contained and does not apply to individual files in O(dir). You can use a loop to apply O(hash_behaviour) per file. Version Added: 2.12 |
| ignore_files list / elements=str |
List of file names to ignore. Version Added: 2.2 |
|
| ignore_unknown_extensions bool |
Ignore unknown file extensions within the directory. This allows users to specify a directory containing vars files that are intermingled with non-vars files extension types (e.g. a directory with a README in it and vars files). Version Added: 2.7 |
|
| name str |
The name of a variable into which assign the included vars. If omitted (V(null)) they will be made top level vars. Version Added: 2.2 |
Examples¶
- name: Include vars of stuff.yaml into the 'stuff' variable (2.2).
ansible.builtin.include_vars:
file: stuff.yaml
name: stuff
- name: Conditionally decide to load in variables into 'plans' when x is 0, otherwise do not. (2.2)
ansible.builtin.include_vars:
file: contingency_plan.yaml
name: plans
when: x == 0
- name: Load a variable file based on the OS type, or a default if not found. Using free-form to specify the file.
ansible.builtin.include_vars: "{{ lookup('ansible.builtin.first_found', params) }}"
vars:
params:
files:
- '{{ansible_distribution}}.yaml'
- '{{ansible_os_family}}.yaml'
- default.yaml
paths:
- 'vars'
- name: Bare include (free-form)
ansible.builtin.include_vars: myvars.yaml
- name: Include all .json and .jsn files in vars/all and all nested directories (2.3)
ansible.builtin.include_vars:
dir: vars/all
extensions:
- 'json'
- 'jsn'
- name: Include all default extension files in vars/all and all nested directories and save the output in test. (2.2)
ansible.builtin.include_vars:
dir: vars/all
name: test
- name: Include default extension files in vars/services (2.2)
ansible.builtin.include_vars:
dir: vars/services
depth: 1
- name: Include only files matching bastion.yaml (2.2)
ansible.builtin.include_vars:
dir: vars
files_matching: bastion.yaml
- name: Include all .yaml files except bastion.yaml (2.3)
ansible.builtin.include_vars:
dir: vars
ignore_files:
- 'bastion.yaml'
extensions:
- 'yaml'
- name: Ignore warnings raised for files with unknown extensions while loading (2.7)
ansible.builtin.include_vars:
dir: vars
ignore_unknown_extensions: True
extensions:
- ''
- 'yaml'
- 'yml'
- 'json'
Return Values¶
| Key | Data Type | Description | Returned |
|---|---|---|---|
| ansible_facts | dict | Variables that were included and their values | success |
| ansible_included_var_files | list | A list of files that were successfully included | success |
Authors¶
- Allen Sanabria (@linuxdynasty)