如何在库存文件中使用ansible-vault加密密码?

问题描述 投票:1回答:1

我想在我的库存文件中使用加密密码和ansible-vault,然后针对该文件运行playbooks。就像是:

ansible-playbook --ask-vault-pass -i inventory test.yml

我为所有主机尝试了单个密码,它工作正常,但需要为不同的主机使用不同的密码。我们如何在库存文件中使用使用ansible-vault生成的变量?

以下是我累了的代码:

生成ansible-vault加密字符串

ansible-vault encrypt_string 'abc123' --name ansible_ssh_pass > a_password_file

test.yml文件

- hosts: hostgroup_1
  vars_files:
    - a_password_file
  tasks:
    - command: date
      register: output

    - debug:
        msg: "{{ output.stdout }}"

库存文件:

[hostgroup_1]
xxx.xxx.com ansible_host=xx.xx.xx.xx ansible_user=root
xxx.xxx.com ansible_host=xx.xx.xx.xx ansible_user=root

[hostgroup_2]
xxx.xxx.com ansible_host=xx.xx.xx.xx ansible_user=root

输出:

ansible-playbook -i inventory --ask-vault-pass test.yml

Vault password:

PLAY [valut test] *****************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************
ok: [xxx.xxx.com]
ok: [xxx.xxx.com]

TASK [command] ********************************************************************************************************************************************
changed: [xxx.xxx.com]
changed: [xxx.xxx.com]

TASK [debug] **********************************************************************************************************************************************
ok: [xxx.xxx.com] => {
    "msg": "XXX XXX  XX XX:XX:XX XXX XXXX"
}
ok: [xxx.xxx.com] => {
    "msg": "XXX XXX  XX XX:XX:XX XXX XXXX"
}

PLAY RECAP ************************************************************************************************************************************************
xxx.xxx.com : ok=3    changed=1    unreachable=0    failed=0
xxx.xxx.com : ok=3    changed=1    unreachable=0    failed=0

在上面的代码中,我对所有主机使用了相同的ansible_ssh_pass,但是想使用下面的库存文件,其中包含每个主机的不同passoword

库存文件:

[hostgroup_1]
xxx.xxx.com ansible_host=xx.xx.xx.xx ansible_user=root  ansible_ssh_pass=abc123
xxx.xxx.com ansible_host=xx.xx.xx.xx ansible_user=root  ansible_ssh_pass=123abc

[hostgroup_2]
xxx.xxx.com ansible_host=xx.xx.xx.xx ansible_user=root  ansible_ssh_pass=xyz098
ansible password-encryption ansible-inventory ansible-vault
1个回答
1
投票

将库加密文件分别保存在库存下的host_vars子目录中。

有关详细信息,请参阅Splitting Out Host and Group Specific Data

© www.soinside.com 2019 - 2024. All rights reserved.