| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
--- |
| 10 |
author: Allen Sanabria (@linuxdynasty) |
| 11 |
module: include_vars |
| 12 |
short_description: Load variables from files, dynamically within a task |
| 13 |
description: |
| 14 |
- Loads YAML/JSON variables dynamically from a file or directory, recursively, during task runtime. |
| 15 |
- If loading a directory, the files are sorted alphabetically before being loaded. |
| 16 |
- This module is also supported for Windows targets. |
| 17 |
- To assign included variables to a different host than C(inventory_hostname), |
| 18 |
use C(delegate_to) and set C(delegate_facts=yes). |
| 19 |
version_added: "1.4" |
| 20 |
options: |
| 21 |
file: |
| 22 |
description: |
| 23 |
- The file name from which variables should be loaded. |
| 24 |
- If the path is relative, it will look for the file in C(vars/) subdirectory of a role or relative to playbook. |
| 25 |
type: path |
| 26 |
version_added: "2.2" |
| 27 |
dir: |
| 28 |
description: |
| 29 |
- The directory name from which the variables should be loaded. |
| 30 |
- If the path is relative and the task is inside a role, it will look inside the role's C(vars/) subdirectory. |
| 31 |
- If the path is relative and not inside a role, it will be parsed relative to the playbook. |
| 32 |
type: path |
| 33 |
version_added: "2.2" |
| 34 |
name: |
| 35 |
description: |
| 36 |
- The name of a variable into which assign the included vars. |
| 37 |
- If omitted (V(null)) they will be made top level vars. |
| 38 |
type: str |
| 39 |
version_added: "2.2" |
| 40 |
depth: |
| 41 |
description: |
| 42 |
- When using O(dir), this module will, by default, recursively go through each sub directory and load up the |
| 43 |
variables. By explicitly setting the depth, this module will only go as deep as the depth. |
| 44 |
type: int |
| 45 |
default: 0 |
| 46 |
version_added: "2.2" |
| 47 |
files_matching: |
| 48 |
description: |
| 49 |
- Limit the files that are loaded within any directory to this regular expression. |
| 50 |
type: str |
| 51 |
version_added: "2.2" |
| 52 |
ignore_files: |
| 53 |
description: |
| 54 |
- List of file names to ignore. |
| 55 |
type: list |
| 56 |
elements: str |
| 57 |
version_added: "2.2" |
| 58 |
extensions: |
| 59 |
description: |
| 60 |
- List of file extensions to read when using O(dir). |
| 61 |
type: list |
| 62 |
elements: str |
| 63 |
default: [ json, yaml, yml ] |
| 64 |
version_added: "2.3" |
| 65 |
ignore_unknown_extensions: |
| 66 |
description: |
| 67 |
- Ignore unknown file extensions within the directory. |
| 68 |
- This allows users to specify a directory containing vars files that are intermingled with non-vars files extension types |
| 69 |
(for example, a directory with a README in it and vars files). |
| 70 |
type: bool |
| 71 |
default: no |
| 72 |
version_added: "2.7" |
| 73 |
hash_behaviour: |
| 74 |
description: |
| 75 |
- If set to V(merge), merges existing hash variables instead of overwriting them. |
| 76 |
- If omitted (V(null)), the behavior falls back to the global C(hash_behaviour) configuration. |
| 77 |
- 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. |
| 78 |
default: null |
| 79 |
type: str |
| 80 |
choices: ["replace", "merge"] |
| 81 |
version_added: "2.12" |
| 82 |
free-form: |
| 83 |
description: |
| 84 |
- This module allows you to specify the O(file) option directly without any other options. |
| 85 |
- There is no O(ignore:free-form) option, this is just an indicator, see example below. |
| 86 |
extends_documentation_fragment: |
| 87 |
- action_common_attributes |
| 88 |
- action_common_attributes.conn |
| 89 |
- action_common_attributes.flow |
| 90 |
- action_core |
| 91 |
attributes: |
| 92 |
action: |
| 93 |
details: While the action plugin does do some of the work it relies on the core engine to actually create the variables; that part cannot be overridden |
| 94 |
support: partial |
| 95 |
bypass_host_loop: |
| 96 |
support: none |
| 97 |
bypass_task_loop: |
| 98 |
support: none |
| 99 |
check_mode: |
| 100 |
support: full |
| 101 |
delegation: |
| 102 |
details: |
| 103 |
- while variable assignment can be delegated to a different host the execution context is always the current inventory_hostname |
| 104 |
- connection variables, if set at all, would reflect the host it would target, even if we are not connecting at all in this case |
| 105 |
support: partial |
| 106 |
diff_mode: |
| 107 |
support: none |
| 108 |
core: |
| 109 |
details: While parts of this action are implemented in core, other parts are still available as normal plugins and can be partially overridden |
| 110 |
support: partial |
| 111 |
seealso: |
| 112 |
- module: ansible.builtin.set_fact |
| 113 |
- ref: playbooks_delegation |
| 114 |
description: More information related to task delegation. |
| 115 |
""" |
| 116 |
|
| 117 |
EXAMPLES = r""" |
| 118 |
- name: Include vars of stuff.yaml into the 'stuff' variable (2.2). |
| 119 |
ansible.builtin.include_vars: |
| 120 |
file: stuff.yaml |
| 121 |
name: stuff |
| 122 |
|
| 123 |
- name: Conditionally decide to load in variables into 'plans' when x is 0, otherwise do not. (2.2) |
| 124 |
ansible.builtin.include_vars: |
| 125 |
file: contingency_plan.yaml |
| 126 |
name: plans |
| 127 |
when: x == 0 |
| 128 |
|
| 129 |
- name: Load a variable file based on the OS type, or a default if not found. Using free-form to specify the file. |
| 130 |
ansible.builtin.include_vars: "{{ lookup('ansible.builtin.first_found', params) }}" |
| 131 |
vars: |
| 132 |
params: |
| 133 |
files: |
| 134 |
- '{{ansible_distribution}}.yaml' |
| 135 |
- '{{ansible_os_family}}.yaml' |
| 136 |
- default.yaml |
| 137 |
paths: |
| 138 |
- 'vars' |
| 139 |
|
| 140 |
- name: Bare include (free-form) |
| 141 |
ansible.builtin.include_vars: myvars.yaml |
| 142 |
|
| 143 |
- name: Include all .json and .jsn files in vars/all and all nested directories (2.3) |
| 144 |
ansible.builtin.include_vars: |
| 145 |
dir: vars/all |
| 146 |
extensions: |
| 147 |
- 'json' |
| 148 |
- 'jsn' |
| 149 |
|
| 150 |
- name: Include all default extension files in vars/all and all nested directories and save the output in test. (2.2) |
| 151 |
ansible.builtin.include_vars: |
| 152 |
dir: vars/all |
| 153 |
name: test |
| 154 |
|
| 155 |
- name: Include default extension files in vars/services (2.2) |
| 156 |
ansible.builtin.include_vars: |
| 157 |
dir: vars/services |
| 158 |
depth: 1 |
| 159 |
|
| 160 |
- name: Include only files matching bastion.yaml (2.2) |
| 161 |
ansible.builtin.include_vars: |
| 162 |
dir: vars |
| 163 |
files_matching: bastion.yaml |
| 164 |
|
| 165 |
- name: Include all .yaml files except bastion.yaml (2.3) |
| 166 |
ansible.builtin.include_vars: |
| 167 |
dir: vars |
| 168 |
ignore_files: |
| 169 |
- 'bastion.yaml' |
| 170 |
extensions: |
| 171 |
- 'yaml' |
| 172 |
|
| 173 |
- name: Ignore warnings raised for files with unknown extensions while loading (2.7) |
| 174 |
ansible.builtin.include_vars: |
| 175 |
dir: vars |
| 176 |
ignore_unknown_extensions: True |
| 177 |
extensions: |
| 178 |
- '' |
| 179 |
- 'yaml' |
| 180 |
- 'yml' |
| 181 |
- 'json' |
| 182 |
""" |
| 183 |
|
| 184 |
RETURN = r""" |
| 185 |
ansible_facts: |
| 186 |
description: Variables that were included and their values |
| 187 |
returned: success |
| 188 |
type: dict |
| 189 |
sample: {'variable': 'value'} |
| 190 |
ansible_included_var_files: |
| 191 |
description: A list of files that were successfully included |
| 192 |
returned: success |
| 193 |
type: list |
| 194 |
sample: [ /path/to/file.json, /path/to/file.yaml ] |
| 195 |
version_added: '2.4' |
| 196 |
""" |