# How to detect why Ansible playbook hangs during execution

**URL:** <https://forum.ansible.com/t/how-to-detect-why-ansible-playbook-hangs-during-execution/14227>\
**Category:** Ansible Project\
**Created:** [December 27, 2013, 6:40pm UTC](https://forum.ansible.com/t/how-to-detect-why-ansible-playbook-hangs-during-execution/14227 "2013-12-27T18:40:24Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![9devmail](https://avatars.discourse-cdn.com/v4/letter/9/779978/32.png) [@9devmail](https://forum.ansible.com/u/9devmail)\
**Post date:** [December 27, 2013, 6:40pm UTC](https://forum.ansible.com/t/how-to-detect-why-ansible-playbook-hangs-during-execution/14227/1 "2013-12-27T18:40:24Z")

</div>

Some of tasks I wrote start and never end. Ansible does not provide any errors or logs that would explain this, even with -vvvv option. Playbook just hangs and passing hours doesn’t change anything.

When I try to run my tasks manually (by entering commands via SSH) everything is fine.

Example task that hangs:

```auto
- name: apt upgrade
  shell: apt-get upgrade

```

Is there any way to see stdout and stderr ? I tried:

```auto
- name: apt upgrade
  shell: apt-get upgrade
  register: hello
- debug: msg="{{ hello.stdout }}"
- debug: msg="{{ hello.stderr }}"
 

```

but nothing changed.

I do have required permissions and I pass correct sudo password - other tasks that require sudo execute correctly.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [December 29, 2013, 2:32pm UTC](https://forum.ansible.com/t/how-to-detect-why-ansible-playbook-hangs-during-execution/14227/2 "2013-12-29T14:32:05Z")

</div>

Apt by default needs eleventy zillion flags to not go interactive.

There’s a parameter you should use on the ‘apt’ module that does upgrades that will take care of this for you.

---

<div class="post-metadata">

**Author:** ![9devmail](https://avatars.discourse-cdn.com/v4/letter/9/779978/32.png) [@9devmail](https://forum.ansible.com/u/9devmail)\
**Post date:** [December 29, 2013, 4:40pm UTC](https://forum.ansible.com/t/how-to-detect-why-ansible-playbook-hangs-during-execution/14227/3 "2013-12-29T16:40:42Z")

</div>

Thanks for answer.

However, apt is not the only task that hangs without ANY information why.

Another example:

shell: apt-get install --no-install-recommends virtualbox-guest-utils && sudo apt-get install virtualbox-guest-dkms

Should I always use your apt module? Why can’t I just execute simple shell command ?

Note that I tried to execute all these tasks manually and there was no errors and no need for any user interaction. Why Ansible has problem with them?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [December 29, 2013, 4:52pm UTC](https://forum.ansible.com/t/how-to-detect-why-ansible-playbook-hangs-during-execution/14227/4 "2013-12-29T16:52:17Z")

</div>

you can execute shell commands, just not ones that expect interactivity as they hold the tty (this happens with any background ssh execution).

for apt commands, yes use the apt module, it takes care of 99% f these issues.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [December 29, 2013, 5:10pm UTC](https://forum.ansible.com/t/how-to-detect-why-ansible-playbook-hangs-during-execution/14227/5 "2013-12-29T17:10:55Z")

</div>

You should absolutely use the apt module.

You can install multiple packages in a single step by using “with\_items” and there’s a flag for controlling whether recommended packages are installed (see module docs).

I suspect as for the commands not being interactive the second time it was because you’ve already executed them and already bypassed the shell prompts.

---

<div class="post-metadata">

**Author:** ![9devmail](https://avatars.discourse-cdn.com/v4/letter/9/779978/32.png) [@9devmail](https://forum.ansible.com/u/9devmail)\
**Post date:** [December 29, 2013, 5:44pm UTC](https://forum.ansible.com/t/how-to-detect-why-ansible-playbook-hangs-during-execution/14227/6 "2013-12-29T17:44:46Z")

</div>

Thank you for your answers. I will use your apt module.

But just for the record - I always rollback to fresh virtual machine, so the reason for hanging is not double task execution for sure.

---

<div class="post-metadata">

**Author:** ![Michael\_Palumbo](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@Michael\_Palumbo](https://forum.ansible.com/u/Michael_Palumbo)\
**Post date:** [July 10, 2014, 11:45pm UTC](https://forum.ansible.com/t/how-to-detect-why-ansible-playbook-hangs-during-execution/14227/7 "2014-07-10T23:45:03Z")

</div>

What is the parameter of the apt module to use to avoid that?

I run the following and it hangs…

- name: update APT package cache  
sudo: yes  
apt: update\_cache=yes

- name: upgrade all safe packages  
sudo: yes  
apt: upgrade=safe

Thanks

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 11, 2014, 10:30pm UTC](https://forum.ansible.com/t/how-to-detect-why-ansible-playbook-hangs-during-execution/14227/8 "2014-07-11T22:30:30Z")

</div>

apt should never need a parameter to not hang, however some _specific_ packages can sometimes be poorly coded.

Not saying that’s the case here.

I’d first check your ansible version and then file a bug with steps to reproduce.

---

<div class="post-metadata">

**Author:** ![Michael\_Palumbo](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@Michael\_Palumbo](https://forum.ansible.com/u/Michael_Palumbo)\
**Post date:** [July 22, 2014, 9:41pm UTC](https://forum.ansible.com/t/how-to-detect-why-ansible-playbook-hangs-during-execution/14227/9 "2014-07-22T21:41:53Z")

</div>

So there is no solution to avoid being stuck on the following? It happens to me often when I am upgrading the system!

- name: upgrade all safe packages  
sudo: yes  
apt: upgrade=safe

Is there at least a way to see the progress? Looking at some logs on the machine?

The state of my machine is messy when this task is stuck because I have to stop ansible-playbook with CTRL+C and all the handlers are not notified… :-/

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 23, 2014, 1:30pm UTC](https://forum.ansible.com/t/how-to-detect-why-ansible-playbook-hangs-during-execution/14227/10 "2014-07-23T13:30:26Z")

</div>

When you kill a playbook you can use --force-handlers to make sure handlers on a repeated runs.

---

<div class="post-metadata">

**Author:** ![Michael\_Palumbo](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@Michael\_Palumbo](https://forum.ansible.com/u/Michael_Palumbo)\
**Post date:** [July 23, 2014, 2:53pm UTC](https://forum.ansible.com/t/how-to-detect-why-ansible-playbook-hangs-during-execution/14227/11 "2014-07-23T14:53:57Z")

</div>

Does that also work when one task fails and the playbook stops?  
Does it work with the tasks of the roles as well?

---

<div class="post-metadata">

**Author:** ![Michael\_Palumbo](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@Michael\_Palumbo](https://forum.ansible.com/u/Michael_Palumbo)\
**Post date:** [July 23, 2014, 5:20pm UTC](https://forum.ansible.com/t/how-to-detect-why-ansible-playbook-hangs-during-execution/14227/12 "2014-07-23T17:20:01Z")

</div>

I just tried to run my playbook with --force-handlers and did CTRL+C: it stopped it right away and the handlers were not notified.  
Does it work for you?  
I use ansible 1.6.7

Thanks.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 23, 2014, 10:10pm UTC](https://forum.ansible.com/t/how-to-detect-why-ansible-playbook-hangs-during-execution/14227/13 "2014-07-23T22:10:11Z")

</div>

This is a misunderstanding of force-handler behavior.

You would run-it again with --force-handlers, and any handlers would run, whether notified or not.

Don’t control-C the second run 🙂

---

<div class="post-metadata">

**Author:** ![Antonio\_Marin](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@Antonio\_Marin](https://forum.ansible.com/u/Antonio_Marin)\
**Post date:** [July 24, 2014, 10:09am UTC](https://forum.ansible.com/t/how-to-detect-why-ansible-playbook-hangs-during-execution/14227/14 "2014-07-24T10:09:43Z")

</div>

I use a screen trick but mostly useful when using commands modules. Anyway, if it hangs doing apt upgrade, the behavior should be the same if your run it as a command.

- debug: msg=“Please, connect to {{ inventory\_hostname }}, screen -r, and respond the possible questions.”  
when: ansible\_hostname == drbd\_master.name
- name: Enable HA  
**command: screen -dm -S halizard** bash -c ‘ha-cfg status’  
when: ansible\_hostname == drbd\_master.name  
async: 60  
poll: 60

It’s also useful when you cannot avoid interactive mode on some actions, but you miss the unattended possibilities.

Br,  
Tony

> **(attachments)**
>
> ![email_smslogo.png](https://us1.discourse-cdn.com/ansible/original/2X/b/bf3e6c6449d4dec753826c49c05d006b991d5909.png)  
> ![email_icon_mobile.png](https://us1.discourse-cdn.com/ansible/original/2X/b/b0f139f5c5e43ef87d126400b917bd24915f3d09.png)  
> ![email_icon_phone.png](https://us1.discourse-cdn.com/ansible/original/2X/b/b74cd524dbbf0e00b6a3785174947431b0201bac.png)  
> ![email_icon_skype.png](https://us1.discourse-cdn.com/ansible/original/2X/e/e823c7e1a55292a1f987d45940679ebd0be5270c.png)  
> ![email_icon_location.png](https://us1.discourse-cdn.com/ansible/original/2X/3/338e299801b9a07a4c7900163a9772f30da0a261.png)  
> ![email_icon_email.png](https://us1.discourse-cdn.com/ansible/original/2X/4/46940bb242ccbb82b87bd8e8f1749e2430b0c148.png)  
> ![email_icon_linkedin.png](https://us1.discourse-cdn.com/ansible/original/2X/c/ca838365667d8197fc713b2d1e1c59bf98a0871c.png)  
> ![email_icon_website.png](https://us1.discourse-cdn.com/ansible/original/2X/5/5c81c3c6ff3c344a10d980a674d7ef9d98002b77.png)

---

<div class="post-metadata">

**Author:** ![Karl\_E\_Jorgensen](https://avatars.discourse-cdn.com/v4/letter/k/c4cdca/32.png) [@Karl\_E\_Jorgensen](https://forum.ansible.com/u/Karl_E_Jorgensen)\
**Post date:** [July 24, 2014, 11:33am UTC](https://forum.ansible.com/t/how-to-detect-why-ansible-playbook-hangs-during-execution/14227/15 "2014-07-24T11:33:46Z")

</div>

Hi

> So there is no solution to avoid being stuck on the following? It happens to me  
> often when I am upgrading the system!  
> - name: upgrade all safe packages  
> &nbsp;&nbsp;sudo: yes  
> &nbsp;&nbsp;apt: upgrade=safe
> 
> Is there at least a way to see the progress? Looking at some logs on the  
> machine?

In the case of apt (which uses dpkg), it may be useful to:

tail -f /var/log/dpkg.log

on the server being upgraded.

If you interrupt the task, I'd expect ansible to give info about what  
was output by the upgrade so far - that should contain clues as to  
what it was doing at the time of the upgrade.

Also, for debugging purposes, it may be useful to find out which tty  
is being used by ansible on the target machine (ps -ef|grep ansible or  
similar), and see what is running on that tty:

&nbsp;&nbsp;&nbsp;&nbsp;ps -ft${basename-of-the-tty}

Hope this helps

---

<div class="post-metadata">

**Author:** ![Ruben\_Ortiz](https://avatars.discourse-cdn.com/v4/letter/r/a9a28c/32.png) [@Ruben\_Ortiz](https://forum.ansible.com/u/Ruben_Ortiz)\
**Post date:** [April 10, 2015, 9:41am UTC](https://forum.ansible.com/t/how-to-detect-why-ansible-playbook-hangs-during-execution/14227/16 "2015-04-10T09:41:00Z")

</div>

Hi

sorry for reply in old post but just want to comment something about…

What about asynchronous polling?

[https://docs.ansible.com/playbooks\_async.html](https://docs.ansible.com/playbooks_async.html)

What if we run a playbook like:

- hosts: vm-ubuntu  
sudo: true  
user: root  
serial: 4  
vars\_files:
- external\_vars.yml  
tasks:
- name: update cache  
apt: update\_cache=yes cache\_valid\_time=86400
- name: update packages full into each server  
apt: name={{ item }} state=present  
with\_items: ubuntu\_packages  
ignore\_errors: True  
async: 45  
pool: 5  
tags:
- install

Will be useful to avoid SSH timeouts in a long playbooks tasks?

What do you think?

Thanks
