首页 > 解决方案 > 如何在ansible中为运行命令添加密码

问题描述

我的任务是运行密码加密密码工具。

  tags: encrypting
  command: sh {{ansible_env.HOME}}/wso2/apim/{{item}}/wso2am-2.6.0/bin/ciphertool.sh -Dconfigure 
  with_sequence: start=1  end={{ no_of_nodes }}

此任务需要密码。但它失败了,因为没有提供提示密码来发送密码。如何解决这个问题?

标签: shellansiblewso2wso2-am

解决方案


使用期望模块。引用示例如何提供command密码

- name: Case insensitive password string match
  expect:
    command: passwd username
    responses:
      (?i)password: "MySekretPa$$word"
  # you don't want to show passwords in your logs
  no_log: true

在您的情况下,该任务可能与此类似。使password提示符符合您的需求

- expect:
    command: sh {{ansible_env.HOME}}/wso2/apim/{{item}}/wso2am-2.6.0/bin/ciphertool.sh -Dconfigure 
    responses:
      (?i)password: "MySekretPassword"
  loop: "{{ range(1, no_of_nodes)|list }}"
  no_log: true
  tags: encrypting

推荐阅读