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

查看:60
本文介绍了如何在 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 参数,请查看 文档,其中展示了使用其他选项(例如 creates,如下所示:

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天全站免登陆