【问题标题】:ansible read username and password from vaultansible 从保险库中读取用户名和密码
【发布时间】:2020-01-02 03:09:21
【问题描述】:

我在配置文件中有我的保险库令牌,并试图从我本地的保险库中读取用户名和密码,并将这些变量传递给远程主机。在下面尝试了这个,能够读取用户名和密码作为 getusername 和 getpassword 的一部分,但在最后一个块中,它们无法读取为变量,请让我知道我做错了什么。

- name: wait for native_transport_port
  wait_for:
    host="{{ inventory_hostname }}"
    port="9042"
    state=started
    timeout=300

- name: getusername
  gather_facts: no
  become_user: pc_user
  tasks:
  shell: source ~/.profile ;vault_username=`vault read --field=adm path`
  delegate_to: 127.0.0.1

- name: getpassword
  gather_facts: no
  become_user: pc_user
  tasks:
  shell: source ~/.profile ;vault_p=`vault read --field=adm_p path`
  delegate_to: 127.0.0.1

- name:  create keyspace
  command: cqlsh  -u $vault_username -p $vault_p -f filname"
  become_user: ec2-user

【问题讨论】:

  • “块”getusernamegetpassword 似乎是游戏,而不仅仅是任务 - 像 wait for native_transport_portcreate keyspace 一样是另一个游戏的一部分。这是故意的,还是错误的?您提供的这个 sn-p 不是您正在运行的代码。如果您添加了用于发现问题的实际代码,将会有所帮助。
  • 这是故意的。我主要关心的是我能够在 gettusername 和 getpassword vault_username 和 vault_p 中获得的值。在使用 $vault_username 和 $vault_p 时,我无法在创建密钥空间中传递它们。在键空间中传递这些变量值的方法是什么
  • 我收到错误 [Bad credentials] message=\"提供的用户名 $vault_username 和/或密码不正确\"',)})"], "stdout": "", "stdout_lines" : []}
  • 您需要将shell任务更改为“在其输出中回显字符串,并使用register子句将其保存到ansible中的变量中。我将举一个例子作为答案。

标签: ansible hashicorp-vault


【解决方案1】:

正如评论中简要说明的那样,您需要使 shell 任务返回用户名和密码作为输出,以便 ansible 可以捕获它。请参阅下面修改后的 sn-p 中的 3 个任务:

- name: getusername
  gather_facts: no
  become_user: pc_user
  tasks:
  shell: source ~/.profile ;vault read --field=adm path
  register: vault_username
  delegate_to: 127.0.0.1

- name: getpassword
  gather_facts: no
  become_user: pc_user
  tasks:
  shell: source ~/.profile ;vault read --field=adm_p path
  register: vault_password
  delegate_to: 127.0.0.1

- name:  create keyspace
  command: "cqlsh  -u {{ vault_username.stdout }} -p  {{ vault_password.stdout }} -f filname"
  become_user: ec2-user

这些假设两个 shell 命令:

source ~/.profile ;vault read --field=adm path
source ~/.profile ;vault read --field=adm_p path

将只返回用户名/密码作为输出,屏幕上不会显示任何其他内容。

编辑

忘了提一下,这些 ansible 变量(vault_username/vault_password)应该在同一个游戏中,它们不会在游戏中持续存在,不确定它们是否会按照您组织任务的方式工作。

希望这些帮助。

【讨论】:

  • 很高兴它有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多