How to let ansible answer "yes" to everything sendmailconfig asks
Asked Answered
O

2

5

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 sendmailconfig AND asnwer y to all the questions it asks.

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

Overwork answered 26/1, 2017 at 10:54 Comment(1)
expect module is there to try.Boz
C
16

Just use the yes shell utility,

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

From the man page,

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'.
Chiachiack answered 26/1, 2017 at 10:55 Comment(0)
I
4

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:

- 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 
Itinerary answered 9/7, 2018 at 17:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.