Ansible 实战案例--管理机密文件 之 实践应用

一、创建密码文件

  1. 保险库的名称是/ansible/locker.yml
  2. 保险库包含以下一个变量: pwd的值是Imadev
  3. 加密和解密的密码是:whenyouwishuponastar
  4. 密码存储在/ansible/secret.txt文件中

参考答案:

[root@ansible-server ansible]# echo  whenyouwishuponastar > /ansible/secret.txt
[root@ansible-server ansible]# ansible-vault create  locker.yml  --vault-password-file secret.txt
pwd: Imadev

二、编写剧本

用户变量文件:users.yml
user_name: boss

create_users.yaml
要使用变量文件和密码文件
密码使用password_hash(‘sha512’)

参考答案:

[root@ansible-server ansible]# echo user_name: boss > users.yml
[root@ansible-server ansible]# vim  create_user.yaml
---
- hosts: dev
  vars_files:
    - users.yml
    - locker.yml
  tasks:
    - name: Create user
      user:
        name: "{{ user_name }}"
        password: "{{ pwd | password_hash('sha512') }}"

[root@ansible-server ansible]# ansible-playbook create_user.yaml --vault-password-file secret.txt

PLAY [dev] ****************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************
ok: [node02]

TASK [Create user] ********************************************************************************************
changed: [node02]

PLAY RECAP ****************************************************************************************************
node02                     : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

三、更改密码文件locker.yml的保护密码

更改密码文件locker.yml的保护密码
old:whenyouwishuponastar
new:thankyou

参考答案:

[root@ansible-server ansible]# ansible-vault rekey --vault-password-file secret.txt --new-vault-password-file newfile  locker.yml 
Rekey successful

四、登录测试

[E:\~]$ ssh boss@192.168.5.5


Connecting to 192.168.5.5:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

WARNING! The remote SSH server rejected X11 forwarding request.
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Mon Oct  5 20:56:38 2020 from 192.168.5.1
[boss@node02 ~]$ 
[boss@node02 ~]$ 
[boss@node02 ~]$ 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值