如何让 ansible 回答 "yes" 到 sendmailconfig 询问的所有内容

How to let ansible answer "yes" to everything sendmailconfig asks

我正在使用 ansible 设置服务器,我想安装和配置 sendmail。为此,我首先使用 apt 安装它,然后我需要 运行 sendmailconfig 并回答 y 它提出的所有问题。

最后一部分是我认为最难的部分。 sendmailconfig 没有 -y 标志来回答所有问题,那么如何让 Ansible 简单地同意它提出的所有问题?

只需使用 yes shell 实用程序,

yes 'y' | <command-name>
#    ^^The repeated string being 'yes' as the OP had asked.

来自 man 页面,

NAME
       yes - output a string repeatedly until killed

SYNOPSIS
       yes [STRING]...
       yes OPTION

DESCRIPTION
       Repeatedly output a line with all specified STRING(s), or 'y'.

基本上,您需要执行两项任务:更新主机和 运行 sendmailconfig。 注意:如果您使用的是 运行ning Ansible 2.5.0,您可能需要在远程主机上安装 pexpect 模块,因此请将此任务包含到您的任务文件中。例如:

- name: Update hosts
  lineinfile:
    path: /etc/hosts
    regexp: '^127\.0\.0\.1'
    line: '127.0.0.1 localhost   {{ ansible_host }}'
    owner: root
    group: root
    mode: 0644

- name: Install pexpect module
  raw: sudo apt-get -y install python-pexpect

- name: Configure sendmail
  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