ansible 文件加密

 

Ansible Vault 用于加密敏感信息,不是用来生成加密密码,它可以将一些敏感信息写入文件后,用密码加密此文件,当访问此文件时,需要传递密码。

1. 创建 Vault 加密文件

假设你想存储一个 Linux 用户密码:

ansible-vault create secrets.yml

 编辑文件

linux_user_password: "$1$6NXikaln$64e67B6Q2C0xK0fuNkVw30"

保存后这个 secrets.yml 文件就是加密的。

2. 在 Playbook 中引用该密码

- name: Example using Vault variables
  hosts: webservers
  vars_files:
    - secrets.yml   # 引用加密的变量文件

执行playbook需要输入vault密码

ansible-playbook add_user.yml --ask-vault-pass

或者使用密码文件:

可以对密码文件设置属主属组权限进行权限限制

ansible-playbook add_user.yml --vault-password-file ~/.vault_pass.txt

错误示范

[root@master-1 roles]# ll roles/es_cluster/vars/
总用量 8
-rw-r--r-- 1 root root 264 5月  11 20:24 main.yml
-rw------- 1 root root 484 5月  11 20:25 secret.yml
[root@master
-1 roles]# cat roles/es_cluster/vars/secret.yml $ANSIBLE_VAULT;1.1;AES256 65643465616261333865626132346230373433356336363238383130626131303738366464303035 3165616166356662386131393237393037393165363437650a663533643636323531316234313632 65333534633130393161343665636531643765653764316239343135626130363462643830313336 3564623133396464380a646662396333333965643965646532656465306363306366303331336336 33306561383465373332323061333237663339306131633766306466333532323566353038353361 3231333866663532313062353639303432383737663537376231 [root@master-1 roles]# ansible-playbook -i roles/es_cluster/hosts/inventory.yaml -t add_user main_es_cluster.yml --ask-vault-pass Vault password: PLAY [elasticsearch] ************************************************************************************************************************************************************************************ TASK [Gathering Facts] ********************************************************************************************************************************************************************************** ok: [es-node2] ok: [es-node3] ok: [es-node1] TASK [es_cluster : 创建系统es登录用户] ************************************************************************************************************************************************************************** fatal: [es-node3]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'password' is undefined\n\nThe error appears to be in '/opt/ansible/playbook/roles/roles/es_cluster/tasks/add_user.yml': line 1, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: 创建系统es登录用户\n ^ here\n"} fatal: [es-node2]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'password' is undefined\n\nThe error appears to be in '/opt/ansible/playbook/roles/roles/es_cluster/tasks/add_user.yml': line 1, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: 创建系统es登录用户\n ^ here\n"} fatal: [es-node1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'password' is undefined\n\nThe error appears to be in '/opt/ansible/playbook/roles/roles/es_cluster/tasks/add_user.yml': line 1, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: 创建系统es登录用户\n ^ here\n"}

原因分析:

在 role 中,只有 vars/main.ymldefaults/main.yml 会自动加载,而像 secret.yml 这样的自定义文件需要你 在 playbook 中显式声明 vars_files 引用它,否则变量是不可见的。

解决:

1. 把加密变量直接写进 vars/main.yml,直接加密整个main文件

2. 将加密文件单独写到default目录下

ll roles/es_cluster/defaults/
总用量 4
-rw------- 1 root root 679 5月  11 20:36 main.yml

3. 保留 secret.yml,但在 playbook 中显式引用

- hosts: elasticsearch
  remote_user: root
  # 收集远程主机信息
  gather_facts: yes
  roles:
    #- role: nginx
    - role: es_cluster
    #- role: mysql
    #- role: redis
  vars_files:
    - roles/es_cluster/vars/secret.yml

测试

小技巧:可以给某个task打上tags标签,然后在执行时使用 -t 进行精准调用,而不是全部执行一遍。

cat roles/es_cluster/tasks/add_user.yml
- name: 创建系统es登录用户
  ansible.builtin.user:
    name: es
    shell: /bin/bash
    password: "{{ password }}"
  tags: add_user
ansible-playbook -i roles/es_cluster/hosts/inventory.yaml  -t add_user main_es_cluster.yml --ask-vault-pass
Vault password:

PLAY [elasticsearch] ************************************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************
ok: [es-node2]
ok: [es-node3]
ok: [es-node1]

TASK [es_cluster : 创建系统es登录用户] **************************************************************************************************************************************************************************
changed: [es-node2]
changed: [es-node3]
changed: [es-node1]

PLAY RECAP **********************************************************************************************************************************************************************************************
es-node1                   : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
es-node2                   : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
es-node3                   : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

 解密文件

ansible-vault view  roles/es_cluster/defaults/main.yml
ansible-vault edit   roles/es_cluster/defaults/main.yml

永久解密文件
ansible-vault decrypt  roles/es_cluster/defaults/main.yml
Vault password:
Decryption successful

[root@master-1 roles]# cat roles/es_cluster/vars/secret.yml   # 解密后,文件不再加密
password: "$1$6NXikaln$64e67B6Q2C0xK0fuNkVw30"

 

posted @ 2025-05-11 21:23  不会跳舞的胖子  阅读(66)  评论(0)    收藏  举报