【问题标题】:How to use command stdin in Ansible?如何在 Ansible 中使用命令标准输入?
【发布时间】:2018-11-01 20:32:57
【问题描述】:

我试过this:

- name: Log into Docker registry
  command: docker login --username "{{ docker_registry_username }}" --password-stdin
  stdin: "{{ docker_registry_password }}"

这会导致警告和失败的命令:

[警告]:忽略无效属性:stdin

无法从非 TTY 设备执行交互式登录

我也试过this:

- name: Log into Docker registry
  command: docker login --username "{{ docker_registry_username }}" --password-stdin
    stdin: "{{ docker_registry_password }}"

这会导致语法错误:

错误!加载 YAML 时出现语法错误。

command stdin 真的可以在 Ansible 2.7 中工作吗?如果是这样,我应该如何使用它?

【问题讨论】:

    标签: ansible


    【解决方案1】:

    如果您想在command 模块中使用stdin 参数,请查看the docs,其中显示了使用其他选项(例如creates)的示例,如下所示:

    # You can also use the 'args' form to provide the options.
    - name: This command will change the working directory to somedir/ and will only run when /path/to/database doesn't exist.
      command: /usr/bin/make_database.sh arg1 arg2
      args:
        chdir: somedir/
        creates: /path/to/database
    

    对于您的用例,您会希望:

    - name: Log into Docker registry
      command: docker login --username "{{ docker_registry_username }}" --password-stdin
      args:
        stdin: "{{ docker_registry_password }}"
    

    您的第一次尝试失败了,因为您将stdin 设置为任务级别的键(如whenignore_errors 等),而您实际上希望它成为command 模块的参数。

    您的第二次尝试失败,因为它不是有效的 YAML。

    【讨论】:

      【解决方案2】:

      为什么要使用命令/shell 来登录 Docker 注册表? Ansible 有 docker_login 模块: https://docs.ansible.com/ansible/latest/modules/docker_login_module.html 它处于预览阶段,来自社区,但它可以工作。

      您可以创建包含敏感数据的保管库文件。

      【讨论】:

      • 这不起作用,因为reset_connection 不适用于ansible_local
      • 如果您正在谈论 ansible_connection=local for localhost...嗯...是的,这可能是真的。您可以将 ssh 密钥添加到 localhost 并将连接更改为 ssh :)
      • 不,我的意思是使用 Vagrant ansible_localdirective/plugin/something。
      【解决方案3】:

      听起来你想使用 Ansible 的 expect 模块。见https://docs.ansible.com/ansible/latest/modules/expect_module.html

      希望这会有所帮助。

      【讨论】:

      • 不,我真的不知道。我在问为什么记录的功能似乎根本不起作用。 Expect 是一个可怕的 hack。
      猜你喜欢
      • 2016-08-09
      • 2014-02-02
      • 1970-01-01
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 2016-07-03
      • 2011-12-13
      • 2010-09-13
      相关资源
      最近更新 更多