使用Python库解密Ansible Vault加密文件时遇解密失败求助
解密Ansible Vault加密文件失败求助
尝试用Python库解密Vault加密的YAML文件时,报错:AnsibleVaultError: Decryption failed (no vault secrets were found that could decrypt)
我的代码
#!/usr/bin/env python3 from ansible.constants import DEFAULT_VAULT_ID_MATCH from ansible.parsing.vault import VaultLib, VaultSecret vault = VaultLib([(DEFAULT_VAULT_ID_MATCH, VaultSecret('toto'.encode()))]) print(vault.decrypt(open('toto.yml').read()))
加密文件toto.yml内容
$ANSIBLE_VAULT;1.1;AES256 32353335396261646339343466646261306239633333366365663862353062653632653633656162 6464663731366136633131646531303063366332633734370a653464373830666666343930393564 39393162396564356336336339666666383432323738376566626434393461376334656664396339 6461363530383238340a376333306631383762323134303931393938653837633263386664633830 61383664303562386131306234333238396434316636343230633231386138613264383566363432 6165626266626437653735383835663564313162633265643964
文件生成命令
echo -n "toto" > password ansible-vault encrypt toto.yml --vault-password-file password
报错信息
Traceback (most recent call last): File "/home/adm_user/selenium-test/./vault-stuff.py", line 7, in <module> print(vault.decrypt(open('toto.yml').read())) File "/home/adm_user/.local/lib/python3.10/site-packages/ansible/parsing/vault/__init__.py", line 644, in decrypt plaintext, vault_id, vault_secret = self.decrypt_and_get_vault_id(vaulttext, filename=filename, obj=obj) File "/home/adm_user/.local/lib/python3.10/site-packages/ansible/parsing/vault/__init__.py", line 751, in decrypt_and_get_vault_id raise AnsibleVaultError(msg) ansible.parsing.vault.AnsibleVaultError: Decryption failed (no vault secrets were found that could decrypt)
尝试过的环境
- Python 3.10.12 / Ansible 9.1.0 / core 2.16.2
- Python 3.9.2 / Ansible base 2.10.8
内容的提问来源于stack exchange,提问作者freeflyer
相关产品推荐
相关产品推荐

