handle interact shell in ansible
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
SYNOPSIS yes [STRING]… yes OPTION
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"
args:
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
expect:
command: passwd username
responses:
(?i)password: "MySekretPa$$word"
- name: Generic question with multiple different responses
expect:
command: /path/to/custom/command
responses:
Question:
- response1
- response2
- response3
Summary
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:
Comments