| 1 |
# (c) 2022, Brian Scholer (@briantist) |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import (absolute_import, division, print_function) |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
DOCUMENTATION = r''' |
| 9 |
name: vault_ansible_settings |
| 10 |
version_added: 2.5.0 |
| 11 |
author: |
| 12 |
- Brian Scholer (@briantist) |
| 13 |
short_description: Returns plugin settings (options) |
| 14 |
description: |
| 15 |
- Returns a dictionary of options and their values for a given plugin. |
| 16 |
- This is most useful for using plugin settings in modules and C(module_defaults), |
| 17 |
especially when common settings are set in C(ansible.cfg), in Ansible vars, or via environment variables on the controller. |
| 18 |
- Options can be filtered by name, and can include or exclude defaults, unset options, and private options. |
| 19 |
seealso: |
| 20 |
- ref: Module defaults <module_defaults> |
| 21 |
description: Using the C(module_defaults) keyword. |
| 22 |
notes: |
| 23 |
- This collection supports some "low precedence" environment variables that get loaded after all other sources, such as C(VAULT_ADDR). |
| 24 |
- These environment variables B(are not supported) with this plugin. |
| 25 |
- If you wish to use them, use the R(ansible.builtin.env lookup,ansible_collections.ansible.builtin.env_lookup) to |
| 26 |
load them directly when calling a module or setting C(module_defaults). |
| 27 |
- Similarly, any options that rely on additional processing to fill in their values will not have that done. |
| 28 |
- For example, tokens will not be loaded from the token sink file, auth methods will not have their C(validate) methods called. |
| 29 |
- See the examples for workarounds, but consider using Ansible-specific ways of setting these values instead. |
| 30 |
options: |
| 31 |
_terms: |
| 32 |
description: |
| 33 |
- The names of the options to load. |
| 34 |
- Supports C(fnmatch) L(style wildcards,https://docs.python.org/3/library/fnmatch.html). |
| 35 |
- Prepend any name or pattern with C(!) to invert the match. |
| 36 |
type: list |
| 37 |
elements: str |
| 38 |
required: false |
| 39 |
default: ['*'] |
| 40 |
plugin: |
| 41 |
description: |
| 42 |
- The name of the plugin whose options will be returned. |
| 43 |
- Only lookups are supported. |
| 44 |
- Short names (without a dot C(.)) will be fully qualified with C(community.hashi_vault). |
| 45 |
type: str |
| 46 |
default: community.hashi_vault.vault_login |
| 47 |
include_private: |
| 48 |
description: Include options that begin with underscore C(_). |
| 49 |
type: bool |
| 50 |
default: false |
| 51 |
include_none: |
| 52 |
description: Include options whose value is C(None) (this usually means they are unset). |
| 53 |
type: bool |
| 54 |
default: false |
| 55 |
include_default: |
| 56 |
description: Include options whose value comes from a default. |
| 57 |
type: bool |
| 58 |
default: false |
| 59 |
''' |
| 60 |
|
| 61 |
EXAMPLES = r''' |
| 62 |
### In these examples, we assume an ansible.cfg like this: |
| 63 |
# [hashi_vault_collection] |
| 64 |
# url = https://config-based-vault.example.com |
| 65 |
# retries = 5 |
| 66 |
### end ansible.cfg |
| 67 |
|
| 68 |
### We assume some environment variables set as well |
| 69 |
# ANSIBLE_HASHI_VAULT_URL: https://env-based-vault.example.com |
| 70 |
# ANSIBLE_HASHI_VAULT_TOKEN: s.123456789 |
| 71 |
### end environment variables |
| 72 |
|
| 73 |
# playbook - ansible-core 2.12 and higher |
| 74 |
## set defaults for the collection group |
| 75 |
- hosts: all |
| 76 |
vars: |
| 77 |
ansible_hashi_vault_auth_method: token |
| 78 |
module_defaults: |
| 79 |
group/community.hashi_vault.vault: "{{ lookup('community.hashi_vault.vault_ansible_settings') }}" |
| 80 |
tasks: |
| 81 |
- name: Get a secret from the remote host with settings from the controller |
| 82 |
community.hashi_vault.vault_kv2_get: |
| 83 |
path: app/some/secret |
| 84 |
###### |
| 85 |
|
| 86 |
# playbook - ansible any version |
| 87 |
## set defaults for a specific module |
| 88 |
- hosts: all |
| 89 |
vars: |
| 90 |
ansible_hashi_vault_auth_method: token |
| 91 |
module_defaults: |
| 92 |
community.hashi_vault.vault_kv2_get: "{{ lookup('community.hashi_vault.vault_ansible_settings') }}" |
| 93 |
tasks: |
| 94 |
- name: Get a secret from the remote host with settings from the controller |
| 95 |
community.hashi_vault.vault_kv2_get: |
| 96 |
path: app/some/secret |
| 97 |
###### |
| 98 |
|
| 99 |
# playbook - ansible any version |
| 100 |
## set defaults for several modules |
| 101 |
## do not use controller's auth |
| 102 |
- hosts: all |
| 103 |
vars: |
| 104 |
ansible_hashi_vault_auth_method: aws_iam |
| 105 |
settings: "{{ lookup('community.hashi_vault.vault_ansible_settings', '*', '!*token*') }}" |
| 106 |
module_defaults: |
| 107 |
community.hashi_vault.vault_kv2_get: '{{ settings }}' |
| 108 |
community.hashi_vault.vault_kv1_get: '{{ settings }}' |
| 109 |
tasks: |
| 110 |
- name: Get a secret from the remote host with some settings from the controller, auth from remote |
| 111 |
community.hashi_vault.vault_kv2_get: |
| 112 |
path: app/some/secret |
| 113 |
|
| 114 |
- name: Same with kv1 |
| 115 |
community.hashi_vault.vault_kv1_get: |
| 116 |
path: app/some/secret |
| 117 |
###### |
| 118 |
|
| 119 |
# playbook - ansible any version |
| 120 |
## set defaults for several modules |
| 121 |
## do not use controller's auth |
| 122 |
## override returned settings |
| 123 |
- hosts: all |
| 124 |
vars: |
| 125 |
ansible_hashi_vault_auth_method: userpass |
| 126 |
plugin_settings: "{{ lookup('community.hashi_vault.vault_ansible_settings', '*', '!*token*') }}" |
| 127 |
overrides: |
| 128 |
auth_method: aws_iam |
| 129 |
retries: '{{ (plugin_settings.retries | int) + 2 }}' |
| 130 |
settings: >- |
| 131 |
{{ |
| 132 |
plugin_settings |
| 133 |
| combine(overrides) |
| 134 |
}} |
| 135 |
module_defaults: |
| 136 |
community.hashi_vault.vault_kv2_get: '{{ settings }}' |
| 137 |
community.hashi_vault.vault_kv1_get: '{{ settings }}' |
| 138 |
tasks: |
| 139 |
- name: Get a secret from the remote host with some settings from the controller, auth from remote |
| 140 |
community.hashi_vault.vault_kv2_get: |
| 141 |
path: app/some/secret |
| 142 |
|
| 143 |
- name: Same with kv1 |
| 144 |
community.hashi_vault.vault_kv1_get: |
| 145 |
path: app/some/secret |
| 146 |
###### |
| 147 |
|
| 148 |
# using a block is similar |
| 149 |
- name: Settings |
| 150 |
vars: |
| 151 |
ansible_hashi_vault_auth_method: aws_iam |
| 152 |
settings: "{{ lookup('community.hashi_vault.vault_ansible_settings', '*', '!*token*') }}" |
| 153 |
module_defaults: |
| 154 |
community.hashi_vault.vault_kv2_get: '{{ settings }}' |
| 155 |
community.hashi_vault.vault_kv1_get: '{{ settings }}' |
| 156 |
block: |
| 157 |
- name: Get a secret from the remote host with some settings from the controller, auth from remote |
| 158 |
community.hashi_vault.vault_kv2_get: |
| 159 |
path: app/some/secret |
| 160 |
|
| 161 |
- name: Same with kv1 |
| 162 |
community.hashi_vault.vault_kv1_get: |
| 163 |
path: app/some/secret |
| 164 |
##### |
| 165 |
|
| 166 |
# use settings from a different plugin |
| 167 |
## when you need settings that are not in the default plugin (vault_login) |
| 168 |
- name: Settings |
| 169 |
vars: |
| 170 |
ansible_hashi_vault_engine_mount_point: dept-secrets |
| 171 |
settings: "{{ lookup('community.hashi_vault.vault_ansible_settings', plugin='community.hashi_vault.vault_kv2_get') }}" |
| 172 |
module_defaults: |
| 173 |
community.hashi_vault.vault_kv2_get: '{{ settings }}' |
| 174 |
block: |
| 175 |
- name: Get a secret from the remote host with some settings from the controller, auth from remote |
| 176 |
community.hashi_vault.vault_kv2_get: |
| 177 |
path: app/some/secret |
| 178 |
##### |
| 179 |
|
| 180 |
# use settings from a different plugin (on an indivdual call) |
| 181 |
## short names assume community.hashi_vault |
| 182 |
- name: Settings |
| 183 |
vars: |
| 184 |
ansible_hashi_vault_engine_mount_point: dept-secrets |
| 185 |
settings: "{{ lookup('community.hashi_vault.vault_ansible_settings') }}" |
| 186 |
module_defaults: |
| 187 |
community.hashi_vault.vault_kv2_get: '{{ settings }}' |
| 188 |
block: |
| 189 |
- name: Get a secret from the remote host with some settings from the controller, auth from remote |
| 190 |
community.hashi_vault.vault_kv2_get: |
| 191 |
engine_mount_point: "{{ lookup('community.hashi_vault.vault_ansible_settings', plugin='vault_kv2_get') }}" |
| 192 |
path: app/some/secret |
| 193 |
##### |
| 194 |
|
| 195 |
# normally, options with default values are not returned, but can be |
| 196 |
- name: Settings |
| 197 |
vars: |
| 198 |
settings: "{{ lookup('community.hashi_vault.vault_ansible_settings') }}" |
| 199 |
module_defaults: |
| 200 |
# we usually want to use the remote host's IAM auth |
| 201 |
community.hashi_vault.vault_kv2_get: >- |
| 202 |
{{ |
| 203 |
settings |
| 204 |
| combine({'auth_method': aws_iam}) |
| 205 |
}} |
| 206 |
block: |
| 207 |
- name: Use the plugin auth method instead, even if it is the default method |
| 208 |
community.hashi_vault.vault_kv2_get: |
| 209 |
auth_method: "{{ lookup('community.hashi_vault.vault_ansible_settings', 'auth_method', include_default=True) }}" |
| 210 |
path: app/some/secret |
| 211 |
##### |
| 212 |
|
| 213 |
# normally, options with None/null values are not returned, |
| 214 |
# nor are private options (names begin with underscore _), |
| 215 |
# but they can be returned too if desired |
| 216 |
- name: Show all plugin settings |
| 217 |
ansible.builtin.debug: |
| 218 |
msg: "{{ lookup('community.hashi_vault.vault_ansible_settings', include_none=True, include_private=True, include_default=True) }}" |
| 219 |
##### |
| 220 |
|
| 221 |
# dealing with low-precedence env vars and token sink loading |
| 222 |
## here, VAULT_ADDR is usually used with plugins, but that will not work with vault_ansible_settings. |
| 223 |
## additionally, the CLI `vault login` is used before running Ansible, so the token sink is usually used, which also will not work. |
| 224 |
- hosts: all |
| 225 |
vars: |
| 226 |
plugin_settings: "{{ lookup('community.hashi_vault.vault_ansible_settings', 'url', 'token*', include_default=True) }}" |
| 227 |
overrides: |
| 228 |
url: "{{ plugin_settings.url | default(lookup('ansible.builtin.env', 'VAULT_ADDR')) }}" |
| 229 |
token: >- |
| 230 |
{{ |
| 231 |
plugin_settings.token |
| 232 |
| default( |
| 233 |
lookup( |
| 234 |
'ansible.builtin.file', |
| 235 |
( |
| 236 |
plugin_settings.token_path | default(lookup('ansible.builtin.env', 'HOME')), |
| 237 |
plugin_settings.token_file |
| 238 |
) | path_join |
| 239 |
) |
| 240 |
) |
| 241 |
}} |
| 242 |
auth_method: token |
| 243 |
settings: >- |
| 244 |
{{ |
| 245 |
plugin_settings |
| 246 |
| combine(overrides) |
| 247 |
}} |
| 248 |
module_defaults: |
| 249 |
community.hashi_vault.vault_kv2_get: "{{ lookup('community.hashi_vault.vault_ansible_settings') }}" |
| 250 |
tasks: |
| 251 |
- name: Get a secret from the remote host with settings from the controller |
| 252 |
community.hashi_vault.vault_kv2_get: |
| 253 |
path: app/some/secret |
| 254 |
##### |
| 255 |
''' |
| 256 |
|
| 257 |
RETURN = r''' |
| 258 |
_raw: |
| 259 |
description: |
| 260 |
- A dictionary of the options and their values. |
| 261 |
- Only a single dictionary will be returned, even with multiple terms. |
| 262 |
type: dict |
| 263 |
sample: |
| 264 |
retries: 5 |
| 265 |
timeout: 20 |
| 266 |
token: s.jRHAoqElnJDx6J5ExYelCDYR |
| 267 |
url: https://vault.example.com |
| 268 |
''' |
| 269 |
|
| 270 |
from fnmatch import fnmatchcase |
| 271 |
|
| 272 |
from ansible.errors import AnsibleError |
| 273 |
from ansible.plugins.lookup import LookupBase |
| 274 |
from ansible import constants as C |
| 275 |
from ansible.plugins.loader import lookup_loader |
| 276 |
from ansible.utils.display import Display |
| 277 |
|
| 278 |
|
| 279 |
display = Display() |
| 280 |
|
| 281 |
|
| 282 |
class LookupModule(LookupBase): |
| 283 |
def run(self, terms, variables=None, **kwargs): |
| 284 |
self.set_options(direct=kwargs, var_options=variables) |
| 285 |
|
| 286 |
include_private = self.get_option('include_private') |
| 287 |
include_none = self.get_option('include_none') |
| 288 |
include_default = self.get_option('include_default') |
| 289 |
|
| 290 |
plugin = self.get_option('plugin') |
| 291 |
if '.' not in plugin: |
| 292 |
plugin = 'community.hashi_vault.' + plugin |
| 293 |
|
| 294 |
if not terms: |
| 295 |
terms = ['*'] |
| 296 |
|
| 297 |
opts = {} |
| 298 |
|
| 299 |
try: |
| 300 |
# ansible-core 2.10 or later |
| 301 |
p = lookup_loader.find_plugin_with_context(plugin) |
| 302 |
loadname = p.plugin_resolved_name |
| 303 |
resolved = p.resolved |
| 304 |
except AttributeError: |
| 305 |
# ansible 2.9 |
| 306 |
p = lookup_loader.find_plugin_with_name(plugin) |
| 307 |
loadname = p[0] |
| 308 |
resolved = loadname is not None |
| 309 |
|
| 310 |
if not resolved: |
| 311 |
raise AnsibleError("'%s' plugin not found." % plugin) |
| 312 |
|
| 313 |
# Loading ensures that the options are initialized in ConfigManager |
| 314 |
lookup_loader.get(plugin, class_only=True) |
| 315 |
|
| 316 |
pluginget = C.config.get_configuration_definitions('lookup', loadname) |
| 317 |
|
| 318 |
for option in pluginget.keys(): |
| 319 |
if not include_private and option.startswith('_'): |
| 320 |
continue |
| 321 |
|
| 322 |
keep = False |
| 323 |
for pattern in terms: |
| 324 |
if pattern.startswith('!'): |
| 325 |
if keep and fnmatchcase(option, pattern[1:]): |
| 326 |
keep = False |
| 327 |
else: |
| 328 |
keep = keep or fnmatchcase(option, pattern) |
| 329 |
|
| 330 |
if not keep: |
| 331 |
continue |
| 332 |
|
| 333 |
value, origin = C.config.get_config_value_and_origin(option, None, 'lookup', loadname, None, variables=variables) |
| 334 |
if (include_none or value is not None) and (include_default or origin != 'default'): |
| 335 |
opts[option] = value |
| 336 |
|
| 337 |
return [opts] |