bash 如何让 ansible 对 sendmailconfig 询问的所有内容回答“是”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41871918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:39:44  来源:igfitidea点击:

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

linuxbashansiblecommand-line-interfacesendmail

提问by kramer65

I'm setting up a server with ansible, and I want to install and configure sendmail. To do this I first install it using apt, and then I need to run sendmailconfigAND asnwer yto all the questions it asks.

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

That last part is the hardest I think. sendmailconfigdoesn't have a -yflag to answer yes to everything, so how do I get Ansible to simple agree to all questions it asks?

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

回答by Inian

Just use the yesshell utility,

只需使用yesshell 实用程序,

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

From the manpage,

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'.

回答by Keyno

Basically, you would need to perform two tasks: update hosts and run sendmailconfig. Note: If you are running Ansible 2.5.0 you might need to install the pexpect module on the remote host, so include this task into your tasks file. For example:

基本上,您需要执行两项任务:更新主机和运行 sendmailconfig。注意:如果您运行的是 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