如何在Ansible中使用命令stdin? [英] How to use command stdin in Ansible?

查看:189
本文介绍了如何在Ansible中使用命令stdin?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过:

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

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

This results in a warning and a failing command:

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

[WARNING]: Ignoring invalid attribute: stdin

...

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

Cannot perform an interactive login from a non TTY device

我也尝试过:

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

这会导致语法错误:

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

ERROR! Syntax Error while loading YAML.

command stdin是否在Ansible 2.7中实际工作?如果是这样,我应该如何使用它?

Does command stdin actually work in Ansible 2.7? If so, how am I supposed to use it?

推荐答案

如果要对command模块使用stdin自变量,请查看

If you want to use the stdin argument to the command module, take a look at the docs, which show examples using other options such as creates, which looks like this:

# 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

对于您的用例,您需要:

For your use case, you would want:

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

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

Your first attempt failed because you were setting stdin as a key at the task level (like when or ignore_errors, etc), when you actually want it to be an argument to the command module.

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

Your second attempt failed because it wasn't valid YAML.

这篇关于如何在Ansible中使用命令stdin?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆