Ansible 实战案例--Ansible-vault 管理机密

前言

管理加密/解密yml(palybook)文件工具
有时编写的playbook文件中会存在重要信息,考虑到安全,可以使用此工具进行加密


提示:本篇文章所使用的环境为centos-8.2基于ansible-2.8.0 搭建
具体环境搭建,请参考:ansible-2.8.0 搭建链接

一、获取Ansible-vault 命令帮助

  • Ansible-vault 命令帮助
[root@ansible-server ansible]# ansible-vault --help
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.
  • create 功能的命令帮助
[root@ansible-server ansible]# ansible-vault create --help
usage: ansible-vault create [-h] [--encrypt-vault-id ENCRYPT_VAULT_ID]
                            [--vault-id VAULT_IDS]
                            [--ask-vault-pass | --vault-password-file VAULT_PASSWORD_FILES]
                            [-v]
                            [file_name [file_name ...]]

positional arguments:
  file_name             Filename

optional arguments:
  -h, --help            show this help message and exit
  --encrypt-vault-id ENCRYPT_VAULT_ID
                        the vault id used to encrypt (required if more than
                        vault-id is provided)
  --vault-id VAULT_IDS  the vault identity to use
  --ask-vault-pass      ask for vault password
  --vault-password-file VAULT_PASSWORD_FILES
                        vault password file
  -v, --verbose         verbose mode (-vvv for more, -vvvv to enable
                        connection debugging)

二、创建加密文件

[root@ansible-server ansible]# ansible-vault create user_file
New Vault password: 
Confirm New Vault password: 
user_name: bob
[root@ansible-server ansible]# cat user_file 
$ANSIBLE_VAULT;1.1;AES256
39303734363366613735306537356562346330666431353263383030393663313638346339626232
6534366330323163353431326561303066623132623365310a326331626362623739343163636435
64656538346261636133373037303838633931313334313838666462336432616561366138393961
3234643064396561640a643937383537613862633839353064363231376339333138376532356534
3135

#编辑playbook
[root@ansible-server ansible]# vim add_user.yml
---
- hosts: all
  vars_files:
    user_file
  tasks:
  - name: create user
    user:
      name: '{{ user_name }}'
      state: present

#执行playbook
[root@ansible-server ansible]# ansible-playbook add_user.yml --ask-vault-pass
Vault password: 

PLAY [all] *********************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************
ok: [node02]
ok: [node03]
ok: [node04]
ok: [node01]

TASK [create user] *************************************************************************************************
ok: [node02]
ok: [node01]
ok: [node04]
ok: [node03]

PLAY RECAP *********************************************************************************************************
node01                     : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node02                     : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node03                     : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node04                     : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  


##另外,还可以创建密码文件,用于playbook的执行和用户文件的查看、编辑以及更改
#常见密码文件
[root@ansible-server ansible]# echo '123' > passwdfile
#执行playbook
[root@ansible-server ansible]# ansible-playbook add_user.yml --vault-password-file passwdfile 

PLAY [all] *********************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************
ok: [node01]
ok: [node02]
ok: [node03]
ok: [node04]

TASK [create user] *************************************************************************************************
ok: [node01]
ok: [node03]
ok: [node02]
ok: [node04]

PLAY RECAP *********************************************************************************************************
node01                     : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node02                     : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node03                     : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node04                     : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

三、解密密码文件

[root@ansible-server ansible]# ansible-vault decrypt user_file 
Vault password: 
Decryption successful
[root@ansible-server ansible]# cat user_file 
user_name: bob

四、加密密码文件

[root@ansible-server ansible]# ansible-vault encrypt user_file --vault-password-file passwdfile
Encryption successful
[root@ansible-server ansible]# cat user_file 
$ANSIBLE_VAULT;1.1;AES256
61393833343530313039613366613035366462373230323165663163623434393162363764393163
3966303535336435323066373564303134396138663761340a336363313437303130303739383433
32616439366363613234643863363131313834353461623233333435613833646661396139663065
3134663162393231660a306265383932313636306565346266653936313338626664653436376437
3064

五、查看密码文件

[root@ansible-server ansible]# ansible-vault view user_file --vault-password-file passwdfile
user_name: bob

六、编辑密码文件

#更改文件内容
[root@ansible-server ansible]# ansible-vault edit user_file --vault-password-file passwdfile
user_name: tom
user_name: tom
#查看文件内容
[root@ansible-server ansible]# ansible-vault view user_file --vault-password-file passwdfile
user_name: tom
user_name: tom

七、更改密码文件的密码

[root@ansible-server ansible]# ansible-vault rekey user_file 
Vault password: 
New Vault password: 
Confirm New Vault password: 
Rekey successful
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值