| 1 |
# |
| 2 |
# Copyright (c) 2025, Felix Fontein <[email protected]> |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
name: binary_file |
| 10 |
author: Felix Fontein (@felixfontein) |
| 11 |
short_description: Read binary file and return it Base64 encoded |
| 12 |
version_added: 11.2.0 |
| 13 |
description: |
| 14 |
- This lookup returns the contents from a file on the Ansible controller's file system. |
| 15 |
- The file is read as a binary file and its contents are returned Base64 encoded. |
| 16 |
This is similar to using P(ansible.builtin.file#lookup) combined with P(ansible.builtin.b64encode#filter), |
| 17 |
except that P(ansible.builtin.file#lookup) does not support binary files as it interprets the contents as UTF-8, |
| 18 |
which can cause the wrong content being Base64 encoded. |
| 19 |
options: |
| 20 |
_terms: |
| 21 |
description: |
| 22 |
- Paths of the files to read. |
| 23 |
- Relative paths will be searched for in different places. See R(Ansible task paths, playbook_task_paths) for more details. |
| 24 |
required: true |
| 25 |
type: list |
| 26 |
elements: str |
| 27 |
not_exist: |
| 28 |
description: |
| 29 |
- Determine how to react if the specified file cannot be found. |
| 30 |
type: str |
| 31 |
choices: |
| 32 |
error: Raise an error. |
| 33 |
empty: Return an empty string for the file. |
| 34 |
empty_str: |
| 35 |
- Return the string C(empty) for the file. |
| 36 |
- This cannot be confused with Base64 encoding due to the missing padding. |
| 37 |
default: error |
| 38 |
notes: |
| 39 |
- This lookup does not understand 'globbing' - use the P(ansible.builtin.fileglob#lookup) lookup instead. |
| 40 |
seealso: |
| 41 |
- plugin: ansible.builtin.b64decode |
| 42 |
plugin_type: filter |
| 43 |
description: >- |
| 44 |
The b64decode filter can be used to decode Base64 encoded data. |
| 45 |
Note that Ansible cannot handle binary data, the data will be interpreted as UTF-8 text! |
| 46 |
- plugin: ansible.builtin.file |
| 47 |
plugin_type: lookup |
| 48 |
description: You can use this lookup plugin to read text files from the Ansible controller. |
| 49 |
- module: ansible.builtin.slurp |
| 50 |
description: >- |
| 51 |
Also allows to read binary files Base64 encoded, but from remote targets. |
| 52 |
With C(delegate_to: localhost) can be redirected to run on the controller, but you have to know the path to the file to read. |
| 53 |
Both this plugin and P(ansible.builtin.file#lookup) use some search path logic to for example also find files in the C(files) |
| 54 |
directory of a role. |
| 55 |
- ref: playbook_task_paths |
| 56 |
description: Search paths used for relative files. |
| 57 |
""" |
| 58 |
|
| 59 |
EXAMPLES = r""" |
| 60 |
--- |
| 61 |
- name: Output Base64 contents of binary files on screen |
| 62 |
ansible.builtin.debug: |
| 63 |
msg: "Content: {{ lookup('community.general.binary_file', item) }}" |
| 64 |
loop: |
| 65 |
- some-binary-file.bin |
| 66 |
""" |
| 67 |
|
| 68 |
RETURN = r""" |
| 69 |
_raw: |
| 70 |
description: |
| 71 |
- Base64 encoded content of requested files, or an empty string resp. the string C(empty), depending on the O(not_exist) option. |
| 72 |
- This list contains one string per element of O(_terms) in the same order as O(_terms). |
| 73 |
type: list |
| 74 |
elements: str |
| 75 |
returned: success |
| 76 |
""" |
| 77 |
|
| 78 |
import base64 |
| 79 |
|
| 80 |
from ansible.errors import AnsibleLookupError |
| 81 |
from ansible.plugins.lookup import LookupBase |
| 82 |
from ansible.utils.display import Display |
| 83 |
|
| 84 |
from ansible_collections.community.general.plugins.plugin_utils._lookup import check_for_wrong_terms |
| 85 |
|
| 86 |
display = Display() |
| 87 |
|
| 88 |
|
| 89 |
class LookupModule(LookupBase): |
| 90 |
def run(self, terms, variables=None, **kwargs): |
| 91 |
self.set_options(var_options=variables, direct=kwargs) |
| 92 |
check_for_wrong_terms(self, direct=kwargs) |
| 93 |
not_exist = self.get_option("not_exist") |
| 94 |
|
| 95 |
result = [] |
| 96 |
for term in terms: |
| 97 |
display.debug(f"Searching for binary file: {term!r}") |
| 98 |
path = self.find_file_in_search_path(variables, "files", term, ignore_missing=(not_exist != "error")) |
| 99 |
display.vvvv(f"community.general.binary_file lookup using {path} as file") |
| 100 |
|
| 101 |
if not path: |
| 102 |
if not_exist == "empty": |
| 103 |
result.append("") |
| 104 |
continue |
| 105 |
if not_exist == "empty_str": |
| 106 |
result.append("empty") |
| 107 |
continue |
| 108 |
raise AnsibleLookupError(f"Could not locate file in community.general.binary_file lookup: {term}") |
| 109 |
|
| 110 |
try: |
| 111 |
with open(path, "rb") as f: |
| 112 |
result.append(base64.b64encode(f.read()).decode("utf-8")) |
| 113 |
except Exception as exc: |
| 114 |
raise AnsibleLookupError(f"Error while reading {path}: {exc}") from exc |
| 115 |
|
| 116 |
return result |