Ansible Playbookで実行するとタスクが途中で止まる

Ansible Playbookのタスクが途中で止まってしまい(フリーズ、ハングアップ)、そこから先に進んでくれない。
たとえば以下のような状態で先に進まない。

$ ansible-playbook -i hosts site.yml --tags rails -vvv

・・・

TASK [rails : Rails new] **************************************************************************************************
task path: /Users/user/Documents/Programming/Servers/vagrant-ansible-ror/roles/rails/tasks/main.yml:42
<192.168.33.10> ESTABLISH SSH CONNECTION FOR USER: vagrant
<192.168.33.10> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile=".vagrant/machines/default/virtualbox/private_key"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="vagrant"' -o ConnectTimeout=10 -o ControlPath=/Users/user/.ansible/cp/d986014952 192.168.33.10 '/bin/sh -c '"'"'echo ~vagrant && sleep 0'"'"''
<192.168.33.10> (0, b'/home/vagrant\n', b'')
<192.168.33.10> ESTABLISH SSH CONNECTION FOR USER: vagrant
<192.168.33.10> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile=".vagrant/machines/default/virtualbox/private_key"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="vagrant"' -o ConnectTimeout=10 -o ControlPath=/Users/user/.ansible/cp/d986014952 192.168.33.10 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /home/vagrant/.ansible/tmp/ansible-tmp-1582382578.259243-102715982581542 `" && echo ansible-tmp-1582382578.259243-102715982581542="` echo /home/vagrant/.ansible/tmp/ansible-tmp-1582382578.259243-102715982581542 `" ) && sleep 0'"'"''
<192.168.33.10> (0, b'ansible-tmp-1582382578.259243-102715982581542=/home/vagrant/.ansible/tmp/ansible-tmp-1582382578.259243-102715982581542\n', b'')
Using module file /usr/local/Cellar/ansible/2.9.3/libexec/lib/python3.8/site-packages/ansible/modules/commands/command.py
<192.168.33.10> PUT /Users/user/.ansible/tmp/ansible-local-40504rzv7iwmj/tmpggurzqpy TO /home/vagrant/.ansible/tmp/ansible-tmp-1582382578.259243-102715982581542/AnsiballZ_command.py
<192.168.33.10> SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile=".vagrant/machines/default/virtualbox/private_key"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="vagrant"' -o ConnectTimeout=10 -o ControlPath=/Users/user/.ansible/cp/d986014952 '[192.168.33.10]'
<192.168.33.10> (0, b'sftp> put /Users/user/.ansible/tmp/ansible-local-40504rzv7iwmj/tmpggurzqpy /home/vagrant/.ansible/tmp/ansible-tmp-1582382578.259243-102715982581542/AnsiballZ_command.py\n', b'')
<192.168.33.10> ESTABLISH SSH CONNECTION FOR USER: vagrant
<192.168.33.10> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile=".vagrant/machines/default/virtualbox/private_key"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="vagrant"' -o ConnectTimeout=10 -o ControlPath=/Users/user/.ansible/cp/d986014952 192.168.33.10 '/bin/sh -c '"'"'chmod u+x /home/vagrant/.ansible/tmp/ansible-tmp-1582382578.259243-102715982581542/ /home/vagrant/.ansible/tmp/ansible-tmp-1582382578.259243-102715982581542/AnsiballZ_command.py && sleep 0'"'"''
<192.168.33.10> (0, b'', b'')
<192.168.33.10> ESTABLISH SSH CONNECTION FOR USER: vagrant
<192.168.33.10> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile=".vagrant/machines/default/virtualbox/private_key"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="vagrant"' -o ConnectTimeout=10 -o ControlPath=/Users/user/.ansible/cp/d986014952 -tt 192.168.33.10 '/bin/sh -c '"'"'/usr/libexec/platform-python /home/vagrant/.ansible/tmp/ansible-tmp-1582382578.259243-102715982581542/AnsiballZ_command.py && sleep 0'"'"''

今回のケースでは、途中で止まっていた原因は、プロンプト側で質問されていて、その入力待ちになっているため。

$ vagrant ssh して、どのような質問をされているのか確認する。
今回は $ rails new するタスクだったので、それを実行。

$ vagrant ssh
[vagrant@localhost sample-rails]$ cd sample-rails
[vagrant@localhost sample-rails]$ bundle exec rails new .
       exist
   identical  README.md
   identical  Rakefile
   identical  .ruby-version
   identical  config.ru
   identical  .gitignore
    conflict  Gemfile
Overwrite /home/vagrant/sample-rails/Gemfile? (enter "h" for help) [Ynaqdhm]

「上書きしますか?」という質問で止まっていたことが判明。

Ansibleの expect を利用して入力実行するか、 Conditionals のwhenを利用してスキップすることで対応可能。