如何在ansible任务中通过秘密工具在gnome密钥环中存储新记录,或者如何在ansible shell任务中使用管道stdin?

如何在ansible任务中通过秘密工具在gnome密钥环中存储新记录,或者如何在ansible shell任务中使用管道stdin?,ansible,Ansible,我试图在ansible任务中以编程方式将新密码记录存储在gnome keyring中。由于没有专门用于此任务的ansible模块,我尝试了shell模块,但我很难传递新记录的密码 要在ansible任务中转换的(bash)任务是: $echo“mysecret”|秘密工具商店\ --label='secret\u label''secret\u key'secret\u value' 秘密工具的手册页声明 存储密码也可以通过stdin输入。密码将被删除 在EOF之前为标准DIN的内容。如果您通

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

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

$echo“mysecret”|秘密工具商店\
--label='secret\u label''secret\u key'secret\u value'
秘密工具的手册页声明

存储密码也可以通过stdin输入。密码将被删除 在EOF之前为标准DIN的内容。如果您通过stdin提供一条新线 它将作为密码的一部分存储

但在我的ansible任务中,我没有设法通过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密钥环中创建新的密码记录


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

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

  shell: |
    secret-tool store 
    --label='{{ secret_label }}' 
    '{{ secret_key }}' '{{ secret_value }}'
您正在尝试运行以下三个separate命令:

  • 秘密工具库
  • --label='{{secret\u label}}'
  • `{{secret\u key}}{{secret\u value}}
  • 这将失败,因为这些命令都无效。如果我自己运行,我会得到输出:

    TASK [shell] **************************************************************************
    fatal: [localhost]: FAILED! => {"changed": true, "cmd": "secret-tool store\n--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 item\nusage: 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 found\n/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
    

    亲爱的@larsks,谢谢你的评论-我奇怪的多行shell块不是我的“生产代码”,我只是想让SO的行长度满意。事实上,它只是我代码中的一行代码。我将还原该编辑。我认为在我的钥匙圈中没有获得密码输入的问题是因为某些环境变量丢失或设置不正确,即
    DISPLAY
    DBUS\u SESSION\u BUS\u ADDRESS
    。当通过
    环境
    为我的shell任务设置这些时,它会起作用。