# stopping daemons before apt-get remove?

**URL:** <https://forum.ansible.com/t/stopping-daemons-before-apt-get-remove/17123>\
**Category:** Ansible Project\
**Created:** [December 20, 2014, 11:38pm UTC](https://forum.ansible.com/t/stopping-daemons-before-apt-get-remove/17123 "2014-12-20T23:38:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_Burton](https://avatars.discourse-cdn.com/v4/letter/k/dec6dc/32.png) [@Kevin\_Burton](https://forum.ansible.com/u/Kevin_Burton)\
**Post date:** [December 20, 2014, 11:38pm UTC](https://forum.ansible.com/t/stopping-daemons-before-apt-get-remove/17123/1 "2014-12-20T23:38:39Z")

</div>

I need the opposite of notify.

Basically, I have a ‘foo-off’ role to turn services / daemons off for a specific host.

So if foo is the service, I want to stop foo , then apt-get remove it.

- service: name=foo state=stopped

… because the second time it’s run the daemon isn’t present because the package is removed.

So what I need is kind of a notify that runs BEFORE a task.

So something like

- name: apt-get remove foo  
apt: name=foo state=absent

before:

- stop foo

… is this possible? I guess I could stat the init script first but that adds more complexity.

---

<div class="post-metadata">

**Author:** ![vaida.dan](https://avatars.discourse-cdn.com/v4/letter/v/5daacb/32.png) [@vaida.dan](https://forum.ansible.com/u/vaida.dan)\
**Post date:** [January 5, 2015, 7:57pm UTC](https://forum.ansible.com/t/stopping-daemons-before-apt-get-remove/17123/2 "2015-01-05T19:57:48Z")

</div>

I’d construct a list var with the packages names and pass it to two tasks: first one would stop and disable the services and the second one would remove the packages.  
Some packages are stopping the daemons as part of the uninstall/purge, some don’t.

You could also do a ‘dpkg-query -L ’, save that in a list, do a ‘apt-get remove --purge ’ and then call the file module with state=absent and ignore\_errors: True to ingest the previous list.

Not what you were expecting but it’s simpler than stat’ing the init file 🙂

---

<div class="post-metadata">

**Author:** ![Tom\_Bamford1](https://avatars.discourse-cdn.com/v4/letter/t/43a26b/32.png) [@Tom\_Bamford1](https://forum.ansible.com/u/Tom_Bamford1)\
**Post date:** [January 6, 2015, 8:58am UTC](https://forum.ansible.com/t/stopping-daemons-before-apt-get-remove/17123/3 "2015-01-06T08:58:47Z")

</div>

You could first check if the package is installed and register a variable to that effect.

```auto
- shell: dpkg -l foo
  changed_when: False
  failed_when: False
  register: foo_installed

- service: name=foo enabled=no state=stopped
  when: foo_installed.rc == 0

```

​
