我正在hashi_vault模块的帮助下,从HashiCorp的文件库中读取base64文件。代码示例:
- name: Vault get b64.pfx file
set_fact:
b64_pfx: "{{ lookup('hashi_vault',
'secret={{ path_pfx }} token={{ token }} url={{ url }} cacert={{ role_path}}/files/CA.pem')}}"
然后,下一步,我需要将此base64 var解码为二进制格式,并将其存储在文件中。我目前正在使用shell模块来完成工作。代码示例:
- name: Decode Base64 file to binary
shell: "echo {{ b64_pfx }} | base64 --decode > {{ pfxFile }}"
delegate_to: localhost
我在网上寻找可能的解决方案,例如( Copy module with base64-encoded binary file adds extra character和How to upload encrypted file using ansible vault?)。
但是我能找到的唯一可行的解决方案是使用shell模块。由于这是一个老问题,是否有任何解决方法?
至少在ansible 2.9上使用b64decode
filter可以满足您的要求:
b64decode
我确认它仅写入指定的字节(无尾随空格),并且是二进制安全的。
[如果您尝试了该行为,但对您不起作用,请更新您的问题以说出这一点,并包括您正在使用的ansible版本。我还认为您链接到的错误已得到修复,因为我在ansible 2.9上尝试了他们的确切案例,并且这样做是正确的:
- copy:
dest: '{{ pfxFile }}'
content: '{{ b64_pfx | b64decode }}'
delegate_to: localhost
- hosts: localhost
connection: local
gather_facts: no
tasks:
- set_fact:
string_in_base64: 'sxZARwIVokeqOMGPygc1S20CaGPiKDRGRzg0oSVGmCF2oXHua+9fVhriUQRd8vkmvpHoBmSsI6Y='
- copy:
dest: binary_file.ansible
content: '{{ string_in_base64 | b64decode }}'
- shell: |
echo '{{ string_in_base64 }}' | base64 --decode > binary_file
shasum -a 1 binary_file
shasum -a 1 binary_file.ansible