Cri*_*oro 17 security ansible ansible-vault
我是ansible的新手,我正在使用一个非常简单的剧本来发布sudo apt-get update
和sudo apt-get upgrade
在几台服务器上.
这是我正在使用的剧本:
---
- name: Update Servers
hosts: my-servers
become: yes
become_user: root
tasks:
- name: update packages
apt: update_cache=yes
- name: upgrade packages
apt: upgrade=dist
Run Code Online (Sandbox Code Playgroud)
这是我~/.ansible/inventory/hosts
文件的摘录:
[my-servers]
san-francisco ansible_host=san-francisco ansible_ssh_user=user ansible_become_pass=<my_sudo_password_for_user_on_san-francisco>
san-diego ansible_host=san-diego ansible_ssh_user=user ansible_become_pass=<my_sudo_password_for_user_on_san-diego>
Run Code Online (Sandbox Code Playgroud)
如果我启动剧本,这就是我得到的:
$ ansible-playbook update-servers-playbook.yml
PLAY [Update Servers] **********************************************************
TASK [setup] *******************************************************************
ok: [san-francisco]
ok: [san-diego]
TASK [update packages] *********************************************************
ok: [san-francisco]
ok: [san-diego]
TASK [upgrade packages] ********************************************************
ok: [san-francisco]
ok: [san-diego]
PLAY RECAP *********************************************************************
san-francisco : ok=3 changed=0 unreachable=0 failed=0
san-diego : ok=3 changed=0 unreachable=0 failed=0
Run Code Online (Sandbox Code Playgroud)
困扰我的是我user
在我的~/.ansible/inventory/hosts
文件中以明文存储的用户密码.
我已经阅读过有关保险库的内容,我还阅读了有关变量和保险库的最佳实践,但我不明白如何将其应用于我的极小用例.
我也试过使用查找.虽然一般来说它们也在库存文件中工作,但我能够做到这样的事情:
[my-servers]
san-francisco ansible_host=san-francisco ansible_ssh_user=user ansible_become_pass="{{ lookup('env', 'ANSIBLE_BECOME_PASSWORD_SAN_FRANCISCO') }}"
Run Code Online (Sandbox Code Playgroud)
在这种情况下,密码将存储在一个名为的环境变量中ANSIBLE_BECOME_PASSWORD_SAN_FRANCISCO
; 据我所知,没有办法在保险库中查找变量.
那么,我怎么能组织我的文件,以便能够从某个地方查找我的密码并安全地存储它们?
yda*_*coR 18
您需要创建一些存储的变量文件,然后将它们包含在您的playbooks或命令行中.
如果您更改库存文件以使用变量作为成为传递,则此变量可以是已存储的:
[my-servers]
san-francisco ansible_host=san-francisco ansible_ssh_user=user ansible_become_pass='{{ sanfrancisco_become_pass }}'
san-diego ansible_host=san-diego ansible_ssh_user=user ansible_become_pass='{{ sandiego_become_pass }}'
Run Code Online (Sandbox Code Playgroud)
然后使用ansible-vault create vaulted_vars.yml
以下内容创建一个拱形文件:
sanfrancisco_become_pass: <my_sudo_password_for_user_on_san-francisco>
sandiego_become_pass : <my_sudo_password_for_user_on_san-diego>
Run Code Online (Sandbox Code Playgroud)
然后要么将拱形文件包含为这样的额外变量:
ansible-playbook -i ~/.ansible/inventory/hosts playbook.yml --ask-vault-pass -e@~/.ansible/inventory/vault_vars
Run Code Online (Sandbox Code Playgroud)
或者使用include_vars任务在playbook中包含vars文件:
- name : include vaulted variables
include_vars: ~/.ansible/inventory/vault_vars
Run Code Online (Sandbox Code Playgroud)
ofr*_*mel 15
解决这个问题的最好方法是使用host_vars。最简单的设置是将ansible_become_pass
Vault 中的加密文件放入相应的 host_vars 目录中,如下所示:
myplaybook.yml
host_vars/onehost.com/crypted
host_vars/otherhost.com/crypted
Run Code Online (Sandbox Code Playgroud)
在crypted
文件中放置ansible_become_pass
变量的赋值:
ansible_become_pass: SuperSecre3t
Run Code Online (Sandbox Code Playgroud)
使用 创建文件ansible-vault create
,使用 编辑ansible-vault edit
。
按照Ansible 文档中的建议,您需要为每个主机创建一个附加文件,用于ansible_become_passwd
从具有不同名称的加密变量中分配 。ansible_become_passwd
这样就可以在项目文件中搜索。
myplaybook.yml
host_vars/onehost.com/plain
host_vars/onehost.com/crypted
host_vars/otherhost.com/plain
host_vars/otherhost.com/crypted
Run Code Online (Sandbox Code Playgroud)
其中plain
文件包含如下内容:
ansible_become_pass: "{{ vaulted_become_pass }}"
Run Code Online (Sandbox Code Playgroud)
文件crypted
设置vaulted_become_pass
如上所示。
所有crypted
文件必须使用相同的密钥加密,并且ansible-playbook
必须使用--ask-vault-pass
.
使用您自己的相关设置设置库存后。这些设置假设您已经设置了 rsa 密钥对来访问您的服务器。您应该能够使用 ssh remoteuser@155.42.88.199 ssh 进入您的服务器
[local]
localhost ansible_connection=local
[remote]
155.42.88.199 ansible_connection=ssh ansible_user=remoteuser ansible_become_user=root ansible_become=yes ansible_ssh_private_key_file=<private_key_file_path>
Run Code Online (Sandbox Code Playgroud)
您需要将 root 密码存储在一个文件中(我将其命名为“my_vault.yml”)。您可以使用以下命令来执行此操作:
~/.ansible$ ansible-vault create my_vault.yml
Run Code Online (Sandbox Code Playgroud)
简单存储您的远程服务器密码,如下所示(不包含“<>”标签)
su_password: <myreallyspecialpassword>
Run Code Online (Sandbox Code Playgroud)
密码现在将由保管库加密,查看密码的唯一方法是输入以下命令。
~/.ansible$ ansible-vault edit my_vault.yml
Run Code Online (Sandbox Code Playgroud)
我们现在需要将“my_vault.yml”文件包含在我们的剧本中。我们可以通过使用 来vars-files
获取 的值来做到这一点su-password
。我们现在可以创建一个标题为 var 的变量ansible_become_pass
,该变量将传递我们文件中的值my_vault.yml
,这将允许我们的远程用户在服务器上 su 一次。
---
- name: My Awesome Playbook
hosts: remote
become: yes
vars_files:
- ~/.ansible/my_vault.yml
vars:
ansible_become_pass: '{{ su_password }}'
roles:
- some_awesome_role
Run Code Online (Sandbox Code Playgroud)
由于我们每次想要运行此剧本时都使用Vault,因此我们需要使用以下命令。
ansible-playbook myawesome_playbook.yml --ask-vault-pass
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12581 次 |
最近记录: |