这样讲解Ansible(ansible-vault)的加密与解密真666
这期分享就不废话了,来直接上主题。(往期回顾:https://blog.csdn.net/qq_41765918/category_11512932.html,后续整个体系的文章都会在这专栏上,若感兴趣可关注本专栏。若对IT民工金鱼哥感兴趣,可互关~)
Ansible是很火的一个自动化部署工具,在ansible控制节点内,存放着当前环境服务的所有服务的配置信息,其中自然也包括一些敏感的信息,例如明文密码、IP地址等等。 从安全角度来讲,这些敏感数据的文件不应该以明文的形式存在。
Ansible官方已经考虑到了这种情况,当我们的playbook中含有不能明文展示的文本时,ansible通过命令行「ansible-vault」给你目标文件/字符串进行加密。在执行playbook时,通过指定相应参数来给目标文件解密,从而实现ansible vault的功能。
ansible可以加密任何部署相关的文件数据,例如:
- 主机/组变量等所有的变量文件
- tasks、hanlders等所有的playbook文件
- 命令行导入的文件(eg : -e @file.yaml ,-e @file.json)
- copy,template的模块里src参数所使用的文件,甚至是二进制文件。
- playbook里用到的某个字符串参数也可以加密(Ansible>=2.3)
那究竟应该如何操作呢?先来看看命令上的常规操作:
1. 创建加密文件
ansible-vault create foo.yml
执行该命令后,交互式输入两次相同的密码,则创建加密文件成功
2. 使用密码文件创建
ansible-vault create --vault-password-file=vault-pass secret.yml
3. 给现有文件加密
ansible-vault encrypt foo.yml bar.yml baz.yml
4. 编辑加密文件
对加密过的文件进行编辑也是我们常遇到的,命令为:
ansible-vault edit foo.yml
5. 查看加密文件
有时我们不是想编辑文件,而是简单查看下文件内容,命令为:
ansible-vault view foo.yml bar.yml baz.yml
6. 更改密码
给加密文件更改密码,命令为:
ansible-vault rekey foo.yml bar.yml baz.yml
7. 取消加密(解密)
取消加密的命令为:
ansible-vault decrypt foo.yml bar.yml baz.yml
8. playbook与ansible vault
ansible执行playbook时,可以通过交互式或指定密码文件的方式来解密文件。
另外一种使用方式,是将密码放在某个文件内,执行playbook时,通过指定该密码文件进行解密。
可以使用「–vault-password-flie」参数:
ansible-playbook --vault-password-file=password site.yaml
从ansible2.4版本开始,官方不再推荐使用”–vault-password-file”选项,官方开始推荐使用”–vault-id”选项代替”–vault-password-file”选项指定密码文件,也就是说,如下两条命令的效果是一样的。(推荐使用,不代表不能使用。_)
ansible-vault decrypt --vault-id passwordfile site.yaml
ansible-vault decrypt --vault-password-file passwordfile site.yaml
官网地址:https://docs.ansible.com/ansible/2.9/user_guide/vault.html
9. 练习演示
命令选项
[student@servera example]$ ansible-vault -h
usage: ansible-vault [-h] [--version] [-v]
{create,decrypt,edit,view,encrypt,encrypt_string,rekey}
...
encryption/decryption utility for Ansible data files
positional arguments:
{create,decrypt,edit,view,encrypt,encrypt_string,rekey}
create Create new vault encrypted file
decrypt Decrypt vault encrypted file
edit Edit vault encrypted file
view View vault encrypted file
encrypt Encrypt YAML file
encrypt_string Encrypt a string
rekey Re-key a vault encrypted file
optional arguments:
--version show program's version number, config file location,
configured module search path, module location,
executable location and exit
-h, --help show this help message and exit
-v, --verbose verbose mode (-vvv for more, -vvvv to enable
connection debugging)
See 'ansible-vault <command> --help' for more information on a specific
command.
创建加密文件
[student@servera ~]$ mkdir example
[student@servera ~]$ cd example
[student@servera example]$ ansible-vault create vault.yml
New Vault password: 123
Confirm New Vault password: 123
---
- name: ansible vault
hosts: localhost
tasks:
- name: debug
debug:
msg: "test the ansible-vault."
查看加密文件
[student@servera example]$ cat vault.yml
$ANSIBLE_VAULT;1.1;AES256
62646137346235623766383531663462663662653261643933613639343133613630616239386437
3364356562656531636561643336336136653266643632350a306235653336666236336535643230
63303331383435323065663562653331373636633761633437383737303538336365326133363637
3663633061383434640a353738333230633432366337383535613363356537383731613966616365
37343430653035656663353832303465333531623738643235643163663763363737346362376437
61636135623737353939653436376535666337653364326433613064643734346638333133303462
30356365313835396566666563396162653336633066653663376132383961396166353766316236
61633833356633613635343465306534656339373732333535633931303034323439323762393234
35313930396534356338333139643033636661666564316564376664653135343130343637383634
3661336535303461353065356335613561366533663534636566
[student@servera example]$ ansible-vault view vault.yml
Vault password: 123
---
- name: ansible vault
hosts: localhost
tasks:
- name: debug
debug:
msg: "test the ansible-vault."
编辑加密文件
[student@servera example]$ ansible-vault edit vault.yml
Vault password: 123
---
- name: ansible vault
hosts: localhost
tasks:
- name: debug msg
debug:
msg: "test the ansible-vault."
更改密码
[student@servera example]$ ansible-vault rekey vault.yml
Vault password: 123
New Vault password: 321
Confirm New Vault password: 321
Rekey successful
取消加密(解密)
[student@servera example]$ ansible-vault decrypt vault.yml
Vault password: 321
Decryption successful
[student@servera example]$ cat vault.yml
---
- name: ansible vault
hosts: localhost
tasks:
- name: debug msg
debug:
msg: "test the ansible-vault."
加密现有文件
[student@servera example]$ ansible-vault encrypt vault.yml
New Vault password: 123
Confirm New Vault password: 123
Encryption successful
[student@servera example]$ ansible-vault view vault.yml
Vault password: 123
---
- name: ansible vault
hosts: localhost
tasks:
- name: debug msg
debug:
msg: "test the ansible-vault."
运行ansible vault加密的剧本
交互式运行
[student@servera example]$ ansible-playbook vault.yml
ERROR! Attempting to decrypt but no vault secrets found
[student@servera example]$ ansible-playbook --ask-vault-pass vault.yml
Vault password: 123
PLAY [ansible vault] *******************************************************************
TASK [Gathering Facts] *****************************************************************
ok: [localhost]
TASK [debug msg] ***********************************************************************
ok: [localhost] => {
"msg": "test the ansible-vault."
}
PLAY RECAP *****************************************************************************
localhost: ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
指定密码文件
[student@servera example]$ vim passwordfile
123
[student@servera example]$ ansible-playbook --vault-password-file=passwordfile vault.yml
PLAY [ansible vault] *******************************************************************
TASK [Gathering Facts] *****************************************************************
ok: [localhost]
TASK [debug msg] ***********************************************************************
ok: [localhost] => {
"msg": "test the ansible-vault."
}
PLAY RECAP *****************************************************************************
localhost: ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[student@servera example]$ ansible-playbook --vault-id=passwordfile vault.yml
PLAY [ansible vault] *******************************************************************
TASK [Gathering Facts] *****************************************************************
ok: [localhost]
TASK [debug msg] ***********************************************************************
ok: [localhost] => {
"msg": "test the ansible-vault."
}
PLAY RECAP *****************************************************************************
localhost: ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
总结
- ansible-vault用于加密达到安全性需求。
- 熟悉命令常用选项和参数。
- 可用交互式或者指定文件的形式进行剧本运行。
- 若喜欢金鱼哥的文章,顺手点个赞。也可点关注,因为后续会不断上干货。
|