ansible - 如何使用 vault 在 ansible 中为一组主机提供 SSH 用户密码

标签 ansible ansible-inventory ansible-vault

考虑以下 Ansible hosts 文件:

[webservers]        
server1.example.com ansible_ssh_pass=1234567
server2.example.com ansible_ssh_pass=2345678
server3.example.com ansible_ssh_pass=3456789

我想从保险库文件中包含这些密码值,并有一个 hosts 文件(我的目的是拥有一个 ini 库存格式):
[webservers]        
server1.example.com ansible_ssh_pass={{ ssh_pass }}
server2.example.com ansible_ssh_pass={{ ssh_pass }}
server3.example.com ansible_ssh_pass={{ ssh_pass }}

其中 sss_pass 变量来自 host_vars 文件夹中定义的拱形文件。

相关的 ansible 文件夹结构如下所示:
playbook.yml
inventories/
  atlanta/
    group_vars/
    hosts
    host_vars/
      server1.example.com
      server2.example.com
      server3.example.com

但 ansible 正在提示:
[WARNING]:  * Failed to parse /root/hsm-ansible-deploy/inventories/atlanta/hosts with ini plugin: /root/hsm-ansible-deploy/inventories/atlanta/hosts:18: Expected key=value host variable assignment, got: ssh_pass
  • 为什么会出现错误?
  • 如何将密码导入 hosts 文件?
  • 最佳答案

    正如@techraf 所指出的,这只是一个语法问题。 ini的正确写法hosts文件是:

    [webservers]        
    server1.example.com ansible_ssh_pass="{{ ssh_pass }}"
    server2.example.com ansible_ssh_pass="{{ ssh_pass }}"
    server3.example.com ansible_ssh_pass="{{ ssh_pass }}"
    

    但我也找到了一个更优雅的解决方案,其中 hosts文件更优雅,不提供 ansible_ssh_passhosts 中完全可变:
    [webservers]        
    server1.example.com
    server2.example.com
    server3.example.com
    

    并使用 group_vars/all在那里定义这个变量:
    ---
    ansible_ssh_pass: "{{ vault_ansible_ssh_pass }}"
    

    哪里vault_ansible_ssh_pass在每个主机 secret 文件中定义,如 host_vars/server1.example.com
    ---
    vault_ansible_ssh_pass: "my secret password"
    

    然后这些文件使用 ansible-vault 加密:
    ansible-vault encrypt inventories/atlanta/host_vars/server*/vault --vault-password-file ~/.vault_pass.txt
    

    哪里~/.vault_pass.txt以明文形式包含 ansible 保险库密码。

    关于ansible - 如何使用 vault 在 ansible 中为一组主机提供 SSH 用户密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50973997/

    相关文章:

    encryption - ansible 是否安全地传输文件?

    python - 重新加密单独加密的 ansible 保险库变量?

    amazon-web-services - Ansible add_host 不起作用,它正在跳过主机

    python - 如何从 Python 脚本中查看/解密 Ansible 保险库凭据文件?

    mysql - 使用 Ansible mysql_user 模块设置的 root@localhost 密码不起作用

    ansible - 查找收集主机的所有地址

    ansible - 如何在 Ansible 中默认从 'all' 中排除库存组?

    amazon-web-services - Ansible EC2 动态 list - 包含连字符的标签

    amazon-web-services - 认证或权限失败,对远程目录没有权限

    python - 当我尝试使用错误 'NoneType' 对象没有属性 'group' 的组时,Ansible regex_search 失败