| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
author: Felix Fontein (@felixfontein) |
| 14 |
module: load_vars |
| 15 |
short_description: Load SOPS-encrypted variables from files, dynamically within a task |
| 16 |
version_added: '0.1.0' |
| 17 |
description: |
| 18 |
- Loads SOPS-encrypted YAML/JSON variables dynamically from a file during task runtime. |
| 19 |
- To assign included variables to a different host than C(inventory_hostname), use C(delegate_to) and set C(delegate_facts=true). |
| 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 |
name: |
| 27 |
description: |
| 28 |
- The name of a variable into which assign the included vars. |
| 29 |
- If omitted (V(null)) they will be made top level vars. |
| 30 |
type: str |
| 31 |
expressions: |
| 32 |
description: |
| 33 |
- This option controls how Jinja2 expressions in values in the loaded file are handled. |
| 34 |
- If set to V(ignore), expressions will not be evaluated, but treated as regular strings. |
| 35 |
- If set to V(evaluate-on-load), expressions will be evaluated on execution of this module, in other words, when the |
| 36 |
file is loaded. |
| 37 |
- If set to V(lazy-evaluation), expressions will be lazily evaluated. This requires ansible-core 2.19 or newer |
| 38 |
and is the same behavior than M(ansible.builtin.include_vars). V(lazy-evaluation) has been added in community.sops 2.2.0. |
| 39 |
type: str |
| 40 |
default: ignore |
| 41 |
choices: |
| 42 |
- ignore |
| 43 |
- evaluate-on-load |
| 44 |
- lazy-evaluation |
| 45 |
return_method: |
| 46 |
description: |
| 47 |
- Determine how to return the results. |
| 48 |
- Because of limitations of ansible-core < 2.21, the results were returned as Ansible facts. |
| 49 |
Since ansible-core 2.21, it is possible to actually set variables, similar to how M(ansible.builtin.include_vars) |
| 50 |
is working. |
| 51 |
type: str |
| 52 |
default: auto |
| 53 |
choices: |
| 54 |
auto: |
| 55 |
- Return the results as facts for ansible-core < 2.21, and as variables for ansible-core >= 2.21. |
| 56 |
facts-only: |
| 57 |
- Return the results always as facts. |
| 58 |
vars-only: |
| 59 |
- Return the results always as variables. |
| 60 |
- Fails for ansible-core < 2.21. |
| 61 |
version_added: 2.3.0 |
| 62 |
extends_documentation_fragment: |
| 63 |
- community.sops.sops |
| 64 |
- community.sops.attributes |
| 65 |
- community.sops.attributes.facts |
| 66 |
- community.sops.attributes.flow |
| 67 |
attributes: |
| 68 |
action: |
| 69 |
support: full |
| 70 |
async: |
| 71 |
support: none |
| 72 |
details: |
| 73 |
- This action runs completely on the controller. |
| 74 |
check_mode: |
| 75 |
support: full |
| 76 |
diff_mode: |
| 77 |
support: N/A |
| 78 |
details: |
| 79 |
- This action does not modify state. |
| 80 |
facts: |
| 81 |
support: partial |
| 82 |
details: |
| 83 |
- Only returns facts if O(return_method=facts-only), or O(return_method=auto) for ansible-core < 2.21. |
| 84 |
idempotent: |
| 85 |
support: N/A |
| 86 |
details: |
| 87 |
- The action has no C(changed) state. |
| 88 |
seealso: |
| 89 |
- module: ansible.builtin.set_fact |
| 90 |
- module: ansible.builtin.include_vars |
| 91 |
- ref: playbooks_delegation |
| 92 |
description: More information related to task delegation. |
| 93 |
- plugin: community.sops.sops |
| 94 |
plugin_type: lookup |
| 95 |
description: The sops lookup can be used decrypt SOPS-encrypted files. |
| 96 |
- plugin: community.sops.decrypt |
| 97 |
plugin_type: filter |
| 98 |
description: The decrypt filter can be used to descrypt SOPS-encrypted in-memory data. |
| 99 |
- plugin: community.sops.sops |
| 100 |
plugin_type: vars |
| 101 |
description: The sops vars plugin can be used to load SOPS-encrypted host or group variables. |
| 102 |
""" |
| 103 |
|
| 104 |
EXAMPLES = r""" |
| 105 |
--- |
| 106 |
- name: Include variables of stuff.sops.yaml into the 'stuff' variable |
| 107 |
community.sops.load_vars: |
| 108 |
file: stuff.sops.yaml |
| 109 |
name: stuff |
| 110 |
expressions: evaluate-on-load # interpret Jinja2 expressions in stuf.sops.yaml on load-time! |
| 111 |
|
| 112 |
- name: Conditionally decide to load in variables into 'plans' when x is 0, otherwise do not |
| 113 |
community.sops.load_vars: |
| 114 |
file: contingency_plan.sops.yaml |
| 115 |
name: plans |
| 116 |
expressions: ignore # do not interpret possible Jinja2 expressions |
| 117 |
when: x == 0 |
| 118 |
|
| 119 |
- name: Load variables into the global namespace |
| 120 |
community.sops.load_vars: |
| 121 |
file: contingency_plan.sops.yaml |
| 122 |
""" |
| 123 |
|
| 124 |
RETURN = r""" |
| 125 |
ansible_facts: |
| 126 |
description: Variables that were included and their values. |
| 127 |
returned: success, and O(return_method=facts-only), or O(return_method=auto) for ansible-core < 2.21 |
| 128 |
type: dict |
| 129 |
sample: {'variable': 'value'} |
| 130 |
ansible_included_var_files: |
| 131 |
description: A list of files that were successfully included. |
| 132 |
returned: success |
| 133 |
type: list |
| 134 |
elements: str |
| 135 |
sample: [/path/to/file.sops.yaml] |
| 136 |
""" |