ansible 使用local_action解密Vault加密的模板
示例
您可以使用该local_action模块运行依赖于保管库加密模板的播放。
---
- name: Decrypt template
local_action: "shell {{ view_encrypted_file_cmd }} {{ role_path }}/templates/template.enc > {{ role_path }}/templates/template"
changed_when: False
- name: Deploy template
template:
xx_src=templates/template
dest=/home/user/file
- name: Remove decrypted template
local_action: "file path={{ role_path }}/templates/template state=absent"
changed_when: False请注意changed_when:False。如果您使用角色扮演幂等测试,这一点很重要-否则,每次您运行剧本时,都会发出更改信号。在中,group_vars/all.yml您可以设置一个全局解密命令以供重用,例如,如view_encrypted_file_cmd。
group_vars/all.yml
---
view_encrypted_file_cmd: "ansible-vault --vault-password-file {{ lookup('env', 'ANSIBLE_VAULT_PASSWORD_FILE') }} view"现在,在运行播放时,您需要将ANSIBLE_VAULT_PASSWORD_FILE环境变量设置为指向您的Vault密码文件(理想情况下具有绝对路径)。