In my case, ansible was "hanging forever" because apt-get was trying to ask me a question! How did I figure this out? I went to the target server and ran ps -aef | grep apt and then did a kill on the appropriate "stuck" apt-get command.
Immediately after I did that, my ansible playbook sprang back to life and reported (with ansible-playbook -vvv option given):
" ==> Deleted (by you or by a script) since installation.",
" ==> Package distributor has shipped an updated version.",
" What would you like to do about it ? Your options are:",
" Y or I : install the package maintainer's version",
" N or O : keep your currently-installed version",
" D : show the differences between the versions",
" Z : start a shell to examine the situation",
" The default action is to keep your current version.",
"*** buildinfo.txt (Y/I/N/O/D/Z) [default=N] ? "
After reading that helpful diagnostic output, I immediately realized I needed some appropriate dpkg options (see for example, this devops post). In my case, I chose:
apt:
name: '{{ item }}'
state: latest
update_cache: yes
# Force apt to always update to the newer config files in the package:
dpkg_options: 'force-overwrite,force-confnew'
loop: '{{ my_packages }}'
Also, don't forget to clean up after your killed ansible session with something like this, or your install will still likely fail:
sudo dpkg --configure -a