Ansible expect 模块不响应密码提示

Ansible expect module doesn't respond to password prompt

我正在尝试使用 Ansible 为 openvpn 用户创建密码。我正在使用 expect 模块,但它不起作用。

下一个是我要执行的任务。

- name: Crear contraseña
  expect:
    command: passwd openvpn
    responses:
      Question:
        - (?i)Introduzca la nueva contraseña de UNIX: "a1245"
        - (?i)Introduzca la nueva contraseña de UNIX: "a1245"

下一个错误是:

TASK [instalarVPN : Crear contraseña] ******************************************
fatal: [172.16.8.231]: FAILED! => {"changed": true, "cmd": "passwd openvpn", "delta": "0:00:30.108342", "end": "2016-09-14 10:13:07.370289", "failed": true, "rc": 10, "start": "2016-09-14 10:12:37.261947", "stdout": "Introduzca la nueva contraseña de UNIX: ", "stdout_lines": ["Introduzca la nueva contraseña de UNIX: "]}

如果我使用:

- name: Crear contraseña
  expect:
    command: passwd openvpn
    responses:
      (?i)Introduzca la nueva contraseña de UNIX: "a1245"
      (?i)Introduzca la nueva contraseña de UNIX: "a1245"

我有下一个错误:

fatal: [172.16.8.231]: FAILED! => {"changed": false, "failed": true, "module_stderr": "", "module_stdout": "\r\nTraceback (most recent call last):\r\n  File \"/tmp/ansible_98VaZx/ansible_module_expect.py\", line 230, in <module>\r\n    main()\r\n  File \"/tmp/ansible_98VaZx/ansible_module_expect.py\", line 151, in main\r\n    events[key.decode()] = response\r\nUnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 32: ordinal not in range(128)\r\n", "msg": "MODULE FAILURE", "parsed": false}

以下是多个提示和答案的语法:

- name: Crear contraseña
  expect:
    command: passwd openvpn
    responses:
      (?i)Enter new password: "a1245"
      (?i)Enter new password: "a1245"

注意:ñ是Unicode字符,可能需要加引号,或者您可以将语言改为英文。

您需要 Ansible 2.1 - 根据 expect 模块文档中的 responses 参数描述:

If the response is a list, successive matches return successive responses. List functionality is new in 2.1.


也就是说,使用 expect 模块来完成您可以使用 user 模块实现的 "natively" 有点麻烦。