具有output_encoding: base64的可见任务模板抛出未知编码



下面是可见任务

- name: copy file
template:
src: app.properties.j2
dest: "{{ installDir }}/app.properties"
output_encoding: base64

在执行上述任务时,ansible抛出以下错误An exception occurred during task execution. To see the full traceback, use -vvv. The error was: LookupError: unknown encoding: base64,详细的堆栈跟踪为


The full traceback is:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/ansible/executor/task_executor.py", line 140, in run
res = self._execute()
File "/usr/lib/python3/dist-packages/ansible/executor/task_executor.py", line 612, in _execute
result = self._handler.run(task_vars=variables)
File "/usr/lib/python3/dist-packages/ansible/plugins/action/template.py", line 187, in run
f.write(to_bytes(resultant, encoding=output_encoding, errors='surrogate_or_strict'))
File "/usr/lib/python3/dist-packages/ansible/module_utils/_text.py", line 133, in to_bytes
return obj.encode(encoding, errors)
LookupError: unknown encoding: base64

这是ansible version命令

的输出
ansible 2.7.7
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.7.3 (default, Jul 25 2020, 13:03:44) [GCC 8.3.0]

所有这些库都打包在dockerfile中。请帮忙/指点。

您在哪里找到文档表明base64output_encoding的有效值?

Ansible文档说"默认为utf-8,但python支持的任何编码都可以使用。"

在Python中,输出编码由codecs模块提供,该模块用于控制字节字符串和语言特定字符之间的转换。它支持utf-8latin1等,但不支持base64

如果你想在Ansible中使用base64编码,请使用b64encode过滤器。

你可以这样做:

- name: render template to variable
set_fact:
template_value: "{{ lookup('template', 'app.properties.j2') }}"
- name: write base64 encoded file
copy:
dest: "{{ installDir }}/app.properties"
content: "{{ template_value | b64encode }}"

相关内容

最新更新