Sendmailconfig command with ansible,回答问题

Sendmailconfig conmand with ansible, answer questions

我需要在 Ansible 任务中执行 "sendmailconfig" 命令并回答是。

我用这段代码试试:

- name: Exec sendmailconfig
  expect:
    command: sendmailconfig
    responses:
      Question:
        - Configure sendmail with the existing /etc/mail/sendmail.conf? [Y]: y
        - Configure sendmail with the existing /etc/mail/sendmail.mc? [Y]: y
        - Reload the running sendmail now with the new configuration? [Y]: y
    timeout: 30

但任务完成时出现错误:

TASK [Exec sendmailconfig] ********************************************************************************************************************************************* fatal: [demoHostAnsible]: FAILED! => {"changed": true, "cmd": "sendmailconfig", "delta": "0:00:30.133671", "end": "2019-10-04 14:55:34.398377", "msg": "command exceeded timeout", "rc": null, "start": "2019-10-04 14:55:04.264706", "stdout": "Configure sendmail with the existing /etc/mail/sendmail.conf? [Y] ", "stdout_lines": ["Configure sendmail with the existing /etc/mail/sendmail.conf? [Y] "]}

问题是什么?还有其他方法可以从 Ansible 执行 sendmailconfig 命令吗?

你可能有两个,如果不是三个问题的话:

混合语法

expect 模块有两种你混合使用的语法。

要么你必须使用它,只需提供后续响应:

responses:
  Questions:
    - response 1
    - response 2
    - response 3

要么你必须将它与(问题/响应)作为(键/值)对一起使用

responses:
  question 1: response 1
  question 2: response 2
  question 3: response 3

当你做的是:

# Author disclaimer, this is a bad syntax!
responses:
  Questions:
    - question 1: response 1
    - question 2: response 2
    - question 3: response 3

提供给 expect 的问题被解析为正则表达式

如果您使用问题/响应对,那么问题将被解析为正则表达式。在您的问题中,您有 5 个符号需要转义才能被视为文字而不是正则表达式语法:/.?[].

因此您的回复必须如下所示:

responses:
  Configure sendmail with the existing \/etc\/mail\/sendmail\.conf\? \[Y\] : y
  Configure sendmail with the existing \/etc\/mail\/sendmail\.mc\? \[Y\] : y
  Reload the running sendmail now with the new configuration\? \[Y\] : y

您可以通过在正则表达式解释器中提出问题来轻松识别此类问题,例如 regex101

您的超时时间可能太短

你的 timeout 可能太低了,即使设置在 60 它仍然超过了我 docker 测试环境的超时,我不得不将它提高到 120 让它工作。


两种可能的解决方法

  1. 作为键/值的问题/响应:
---
- hosts: localhost
  connection: local

  tasks:
    - name: Exec sendmailconfig
      expect:
        command: sendmailconfig
        responses:
          Configure sendmail with the existing \/etc\/mail\/sendmail\.conf\? \[Y\] : y
          Configure sendmail with the existing \/etc\/mail\/sendmail\.mc\? \[Y\] : y
          Reload the running sendmail now with the new configuration\? \[Y\] : y
        timeout: 120
  1. 回复列表
---
- hosts: localhost
  connection: local

  tasks:
    - name: Exec sendmailconfig
      expect:
        command: sendmailconfig
        responses:
          Questions:
            - y
            - y
            - y
        timeout: 120

这两种语法都让我回顾一下:

PLAY [localhost] ****************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [localhost]

TASK [Exec sendmailconfig] ******************************************************************************************************************************************
changed: [localhost]

PLAY RECAP **********************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0