简体   繁体   English

Ansible 将保险库密码文件传递给剧本

[英]Ansible passing vault password file to playbook

I'm running shell command in ansible playbook which takes password to complete.我在 ansible playbook 中运行 shell 命令,它需要密码才能完成。 I've generated vault file(test_vault.txt)that contains my encrypted password.我生成了包含我的加密密码的保险库文件(test_vault.txt)。 How do I pass it to my playbook so when the playbook runs shell task, it will take the encrypted password from my vault password file?我如何将它传递给我的剧本,以便当剧本运行 shell 任务时,它将从我的保管库密码文件中获取加密的密码? My ansible code looks like this:我的 ansible 代码如下所示:

- name: run openssl
  shell: openssl rsa -in hostname.enc.key -text -noout

If I run this command at Linux prompt, I got:如果我在 Linux 提示符下运行这个命令,我得到:

Enter pw for hostname.enc.key: 

I then enter the password here.然后我在这里输入密码。 How do I pass my vault password in test_vault.txt to playbook?如何将 test_vault.txt 中的保险库密码传递给 playbook?

openssl supports a variety of ways to send in passwords, but likely the easiest is via -passin env:MY_AWESOME_PASSWORD and then set that in the environment: for your shell: openssl支持多种发送密码的方式,但最简单的方法可能是通过-passin env:MY_AWESOME_PASSWORD然后在environment:设置environment:对于您的shell:

- name: run openssl
  shell: openssl rsa -passin env:MY_AWESOME_PASSWORD -in hostname.enc.key -text -noout
  environment:
    MY_AWESOME_PASSWORD: hunter2

This does pose the risk that anyone on the machine with the privileges to inspect the environment of other processes will be able to exfiltrate the password.这确实带来了风险,即机器上有权检查其他进程环境的任何人都将能够泄露密码。 If that is a risk that concerns you, you'll want to explore some of the other password communication schemes, which have their own threat models.如果您担心这种风险,您将需要探索其他一些密码通信方案,它们有自己的威胁模型。

Let's have the variable pw_for_hostname_enc_key with the password encrypted in the file test_vault.txt .让我们变pw_for_hostname_enc_key用密码加密的文件test_vault.txt For example例如

shell> cat test_vault.txt
pw_for_hostname_enc_key: 4PepNGRTyzA

shell> ansible-vault encrypt test_vault.txt
Encryption successful

shell> cat test_vault.txt
$ANSIBLE_VAULT;1.1;AES256
35306366336231663239373437646639336432383030373937353065343266346561653039643038
3931396535613135633735613733346635363761616361650a373133663438383862643733343732
38356363623138316534343364346535313539653065303739386538626265366532616539653163
6232363232383965630a323831333162646239303630643837313937356233336664343634313766
31343536656637373038363936306563363232633432386631663334383030316339326332646162
3334396364353862613933326131366433363232656432323961

Then test the playbook.然后测试剧本。 See Variable precedence: Where should I put a variable?请参阅变量优先级:我应该将变量放在哪里? . . For example例如

shell> cat pb.yml
- hosts: localhost
  tasks:
    - include_vars: test_vault.txt
    - debug:
        var: pw_for_hostname_enc_key

gives (abridged)给(略)

shell> ansible-playbook pb.yml 

ok: [localhost] => 
  pw_for_hostname_enc_key: 4PepNGRTyzA

If it's working use it in other tasks.如果它工作正常,请在其他任务中使用它。 For example例如

    - name: run openssl
      shell: "openssl rsa -in hostname.enc.key 
                          -passin pass:{{ pw_for_hostname_enc_key }}
                          -text -noout"

The next option is to encrypt the password only.下一个选项是仅加密密码。 For example例如

shell> cat test_vault.txt
4PepNGRTyzA

shell> ansible-vault encrypt test_vault.txt
Encryption successful

shell> cat test_vault.txt
$ANSIBLE_VAULT;1.1;AES256
65656363363364376130323365303363643662313939346635646630613230656630343239666130
3563396666663763393132626438336433646661656232660a333239363063383434313237363730
61633931666630616337636434326536333335353836306230333464383432656664336431343637
3961316237346430660a656666316333313936386136383732366539373961303466313236343061
3332

Then test the playbook.然后测试剧本。 The lookup plugin file automatically decrypts vault-encrypted files. lookup插件文件会自动解密 Vault 加密的文件。 For example,例如,

- hosts: localhost
  tasks:
    - debug:
        var: pw_for_hostname_enc_key
      vars:
        pw_for_hostname_enc_key: "{{ lookup('file', 'test_vault.txt') }}"

gives (abridged)给(略)

shell> ansible-playbook pb.yml

ok: [localhost] => 
  pw_for_hostname_enc_key: 4PepNGRTyzA

If it's working use it in other tasks.如果它工作正常,请在其他任务中使用它。 For example例如

    - name: run openssl
      shell: "openssl rsa -in hostname.enc.key 
                          -passin pass:{{ pw_for_hostname_enc_key }}
                          -text -noout"
      vars:
        pw_for_hostname_enc_key: "{{ lookup('file', 'test_vault.txt') }}"

This solution is safer because the scope of the variable with the password is limited to this single task only.这个解决方案更安全,因为密码变量的范围仅限于这个单一的任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM