handle interact shell in ansible

1 minute read

I have been using Ansible to automate some of environment setup work during my daily development/test. One thing I recently run across is to handle the interact shell in ansible.

I have been using Byobu as the terminal manager for my remote server. Since the server will be recreated every time I have to update a new build (usually every week or two). So it’s a good use case to use Ansible to automate this, so I don’t have to run the configuration manually.

When I tried to configure fzf in the server, it prompts me to type yes during the install script. And this script will hang there if I just use the Shell command in Ansible.

By some research, I found there are two ways to deal with this interactive shell:

Using yes

Basically you can use /usr/bin/yes command to automatically respond y when running a script. Here is an example:

yes | script

Here is the explanation from the man page:

NAME yes - output a string repeatedly until killed


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

Here is a good answer is askubuntu.com

And here is how I use yes to install fzf:

- name: "install fzf"
  shell: "git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf && yes | ~/.fzf/install"
      creates: ~/.fzf

Using expect in Ansible

Ansible actually has a module called expect to handle the interactive scenario. This module works very similar to the expect command in U(L)nix. So it can handle more complex scenario than yes, e.g. for scenario where you need to provide something specific as a response, not just yes or no.

Here is the example from the ansible website.

- name: Case insensitve password string match
    command: passwd username
      (?i)password: "MySekretPa$$word"

- name: Generic question with multiple different responses
    command: /path/to/custom/command
        - response1
        - response2
        - response3


As you can see, both yes and the expect module can handle the scenario I have. But expect is definitely more powerful to handle more complex scenario.

Here are some other helpful readings: