如何通过ansible任务中的秘密工具在gnome keyring中存储新记录,或者如何在ansible shell任务



我正试图在一个ansible任务中以编程方式在gnome keyring中存储一个新的密码记录。由于这个任务没有专用的ansible模块,我尝试了shell模块,但我很难传递新记录的密码。

在ansible任务中要转换的(bash(任务是:

$ echo "mysecret" | secret-tool store 
--label='secret_label' 'secret_key 'secret_value'

秘密工具的手册页指出

要存储的密码也可以通过stdin管道输入。密码将是stdin的内容,直到EOF。如果您通过stdin提供换行符它将作为密码的一部分存储。

但在我的可执行任务中,我没有设法通过stdin输入密码

- name: Ensure gnome keyring entry for secret_label exists
shell: secret-tool store --label='{{ secret_label }}' '{{ secret_key }}' '{{ secret_value }}'
args:
stdin: "{{ mysecret }}"
stdin_add_newline: false

此任务不会产生任何错误,但没有在gnome keyring中创建新的密码记录。

如何将mysecret作为stdin管道传输到我的ansible shell命令?

您的剧本中似乎有语法错误。当您使用|文字块运算符时,块的内容被逐字使用,包括换行符。这意味着当你写:

shell: |
secret-tool store 
--label='{{ secret_label }}' 
'{{ secret_key }}' '{{ secret_value }}'

您正在尝试运行以下三个单独的命令:

  1. secret-tool store
  2. --label='{{ secret_label }}'
  3. `"{{secret_key}}">

这将失败,因为这些命令都不是有效命令。如果我自己运行,我会得到输出:

TASK [shell] **************************************************************************
fatal: [localhost]: FAILED! => {"changed": true, "cmd": "secret-tool storen--label='secret_label'n'secret_key' 'secret_Value'n", "delta": "0:00:00.005253", "end": "2019-11-17 19:20:41.147034", "msg": "non-zero return code", "rc": 127, "start": "2019-11-17 19:20:41.141781", "stderr": "secret-tool: must specify a label for the new itemnusage: secret-tool store --label='label' attribute value ...n       secret-tool lookup attribute value ...n       secret-tool clear attribute value ...n       secret-tool search [--all] [--unlock] attribute value ...n/bin/sh: line 1: --label=secret_label: command not foundn/bin/sh: line 2: secret_key: command not found", "stderr_lines": ["secret-tool: must specify a label for the new item", "usage: secret-tool store --label='label' attribute value ...", "       secret-tool lookup attribute value ...", "       secret-tool clear attribute value ...", "       secret-tool search [--all] [--unlock] attribute value ...", "/bin/sh: line 1: --label=secret_label: command not found", "/bin/sh: line 2: secret_key: command not found"], "stdout": "", "stdout_lines": []}

有几种不同的方法可以解决这个问题。一种选择是以与编写任何其他多行命令相同的方式编写它,并使用反斜杠将命令扩展到多行:

---
- hosts: localhost
gather_facts: false
vars:
secret_label: secret_label
secret_key: secret_key
secret_value: secret_value
mysecret: mysecret
tasks:
- shell: |
secret-tool store 
--label='{{ secret_label }}' 
'{{ secret_key }}' '{{ secret_value }}'
args:
stdin: "{{ mysecret }}"
stdin_add_newline: false

您也可以使用YAML折叠块运算符>,如下所示:

---
- hosts: localhost
gather_facts: false
vars:
secret_label: secret_label
secret_key: secret_key
secret_value: secret_value
mysecret: mysecret
tasks:
- shell: >
secret-tool store
--label='{{ secret_label }}'
'{{ secret_key }}' '{{ secret_value }}'
args:
stdin: "{{ mysecret }}"
stdin_add_newline: false

这种情况下的最终结果是相同的。在这两种情况下,在运行上述剧本后,我看到:

$ secret-tool lookup secret_key secret_value
mysecret

最新更新