首页 > 解决方案 > 用ansible发送mailconfig命令,回答问题

问题描述

我需要在 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

但任务以错误结束:

任务 [执行 sendmailconfig] ************************************************ ****************************************************** ************************************************ 致命:[demoHostAnsible ]: 失败的!=> {“更改”:true,“cmd”:“sendmailconfig”,“delta”:“0:00:30.133671”,“end”:“2019-10-04 14:55:34.398377”,“msg”: “命令超出超时”,“rc”:null,“start”:“2019-10-04 14:55:04.264706”,“stdout”:“使用现有的 /etc/mail/sendmail.conf 配置 sendmail?[Y ] ", "stdout_lines": ["用现有的 /etc/mail/sendmail.conf 配置 sendmail?[Y] "]}

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

标签: ansibleconfigsendmail

解决方案


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

混合语法

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   

推荐阅读