我需要使用
ansible-vault
加密文件。我只想在文件尚未被 ansible-vault
加密时才执行加密。我正在尝试在我的 Ansible 剧本中使用此任务:
- local_action: command
ansible-vault encrypt path/to/file
when: <when file is not already encrypted by ansible-vault>
条件语句中是否有可使用的逻辑来检查文件是否已被
ansible-vault
加密?
可能有无数种方法可以做到这一点,所有这些都与 Ansible 和 Ansible Vault 本身关系不大。这是一个:
- local_action: shell
head -1 {{ file }} | grep -v -q \$ANSIBLE_VAULT && ansible-vault encrypt {{ file }}
您还需要
--vault-password-file
,否则 Ansible 将停止处理并等待提示。
问:“如何检查文件是否被
ansible-vault
加密?”
由于 Linux file
命令的
类型数据库,Ansible
stat
模块使用的不提供开箱即用的信息
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- stat:
path: testKey.pem
get_attributes: false
get_checksum: false
get_mime: true
register: result
- debug:
var: result.stat.mimetype
即使对于已经加密的文件也会产生的输出
TASK [stat] **********************
ok: [localhost]
TASK [debug] *********************
ok: [localhost] =>
result.stat.mimetype: text/plain
有必要解决这个问题并实施自己的检查,如
- name: Get file header | Check if file is ANSIBLE_VAULT encrypted
shell:
cmd: "! grep '{{ header }}' <(head -n 1 {{ path }})"
vars:
path: testKey.pem
header: $ANSIBLE_VAULT;1.1;AES256
register: encrypted
failed_when:
- encrypted.rc != 0
- encrypted.rc != 1
changed_when: false
check_mode: false
- debug:
var: encrypted
- assert:
that:
- encrypted.rc
success_msg: "ANSIBLE_VAULT encrypted."
fail_msg: "Not ANSIBLE_VAULT encrypted or other!"
产生的输出
TASK [Get file header | Check if file is ANSIBLE_VAULT encrypted] ******
ok: [localhost]
TASK [debug] ***********************************************************
ok: [localhost] =>
encrypted:
changed: false
cmd: '! grep ''$ANSIBLE_VAULT;1.1;AES256'' <(head -n 1 testKey.pem)'
delta: '0:00:00.008187'
end: '2024-10-13 10:00:00.222791'
failed: false
failed_when_result: false
msg: non-zero return code
rc: 1
start: '2024-10-13 10:00:00.214604'
stderr: ''
stderr_lines: []
stdout: $ANSIBLE_VAULT;1.1;AES256
stdout_lines:
- $ANSIBLE_VAULT;1.1;AES256
TASK [assert] *********************************************************
ok: [localhost] => changed=false
msg: ANSIBLE_VAULT encrypted.
因此,如果文件未ansible-vault
加密,则可以有条件地运行任务。
- debug:
msg: "TASK to do"
when: not encrypted.rc
stat
模块,使其具有必要的功能,就像这个快速而懒惰的原型一样
# 458a
get_encryption=dict(type='bool', default=False),
# 470a
get_encryption = module.params.get('get_encryption')
# 529a
# try to get encyption status if requested
if get_encryption:
output['ansible_vault'] = False
enccmd = module.get_bin_path('grep')
if enccmd:
enccmd = [enccmd, '\$ANSIBLE_VAULT;', b_path]
try:
rc, out, err = module.run_command(enccmd)
if rc == 0:
output['ansible_vault'] = True
except Exception:
pass
导致剧本任务
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- custom_stat:
path: testKey.pem
get_attributes: false
get_checksum: false
get_mime: false
get_encryption: true
register: result
- debug:
var: result.stat.ansible_vault
输出为
TASK [debug] ********************
ok: [localhost] =>
result.stat.ansible_vault: true
TASK [debug] ************************************
ok: [localhost] =>
result.stat.mimetype: application/ansible-vault
人们可以通过研究进一步推进这一点,从开始
所有这些都与 Ansible 和 Ansible Vault 本身关系不大。