如何在 Ansible 中使用命令 stdin?

l0b*_*0b0 7 ansible

我试过这个

- name: Log into Docker registry
  command: docker login --username "{{ docker_registry_username }}" --password-stdin
  stdin: "{{ docker_registry_password }}"
Run Code Online (Sandbox Code Playgroud)

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

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

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

我也试过这个

- name: Log into Docker registry
  command: docker login --username "{{ docker_registry_username }}" --password-stdin
    stdin: "{{ docker_registry_password }}"
Run Code Online (Sandbox Code Playgroud)

这会导致语法错误:

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

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

lar*_*sks 15

如果要使用模块的stdin参数command,请查看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
Run Code Online (Sandbox Code Playgroud)

对于您的用例,您需要:

- name: Log into Docker registry
  command: docker login --username "{{ docker_registry_username }}" --password-stdin
  args:
    stdin: "{{ docker_registry_password }}"
Run Code Online (Sandbox Code Playgroud)

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

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