Search code examples
expectansiblepexpectansible-2.x

Ansible expect module doesn't respond to password prompt


I am trying to create a password to openvpn user using Ansible. I am using the expect module, but it does not work.

The next is the task I am trying to execute.

- 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"

The error is the next:

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: "]}

If I use:

- 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"

I have the next error:

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}

Solution

  • The following is the syntax for multiple prompts and answers:

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

    Notice: ñ is a Unicode character, so it might require quoting, or you can change the language to English.

    You need Ansible 2.1 - per responses argument description in the expect module doc:

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


    That said, it's somewhat cumbersome to use expect module to do what you can achieve "natively" with the user module.