# Ansible task, how to confirm if a process is actually running?

**URL:** <https://forum.ansible.com/t/ansible-task-how-to-confirm-if-a-process-is-actually-running/21662>\
**Category:** Ansible Project\
**Created:** [June 3, 2016, 5:45pm UTC](https://forum.ansible.com/t/ansible-task-how-to-confirm-if-a-process-is-actually-running/21662 "2016-06-03T17:45:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Howard\_Lee](https://avatars.discourse-cdn.com/v4/letter/h/aca169/32.png) [@Howard\_Lee](https://forum.ansible.com/u/Howard_Lee)\
**Post date:** [June 3, 2016, 5:45pm UTC](https://forum.ansible.com/t/ansible-task-how-to-confirm-if-a-process-is-actually-running/21662/1 "2016-06-03T17:45:02Z")

</div>

Ansible 2.1

In the playbook, I started a process:

```auto
- name: Start Automation Agent, and enable start on boot
  service: name=mongodb-mms-automation-agent state=started enabled=yes

```

From play recap, it appears the process has successfully started.

```auto
TASK [install : Start automation agent, and enable start on boot] **************
changed: [server1]

```

However, when log in to remote host and do a `ps`, the process is not running. Checking on process log it had failed some pre-requisite (intended for testing).

How do I write a task in a playbook to confirm the process has successfully started?

---

<div class="post-metadata">

**Author:** ![Mike\_Fennemore](https://avatars.discourse-cdn.com/v4/letter/m/45deac/32.png) [@Mike\_Fennemore](https://forum.ansible.com/u/Mike_Fennemore)\
**Post date:** [June 3, 2016, 6:25pm UTC](https://forum.ansible.com/t/ansible-task-how-to-confirm-if-a-process-is-actually-running/21662/2 "2016-06-03T18:25:55Z")

</div>

You could use register: to register a variable for the result then use debug: var= \<your registered variable\>.\<property you want\>

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [June 3, 2016, 7:43pm UTC](https://forum.ansible.com/t/ansible-task-how-to-confirm-if-a-process-is-actually-running/21662/3 "2016-06-03T19:43:58Z")

</div>

But if starting the service fails, shouldn't ansible report the task  
as failed?

Johannes

---

<div class="post-metadata">

**Author:** ![Howard\_Lee](https://avatars.discourse-cdn.com/v4/letter/h/aca169/32.png) [@Howard\_Lee](https://forum.ansible.com/u/Howard_Lee)\
**Post date:** [June 3, 2016, 10:28pm UTC](https://forum.ansible.com/t/ansible-task-how-to-confirm-if-a-process-is-actually-running/21662/4 "2016-06-03T22:28:31Z")

</div>

@Johannes That’s what I thought, but unfortunately Ansible does not verify if it had successfully ran.

---

<div class="post-metadata">

**Author:** ![Mike\_Fennemore](https://avatars.discourse-cdn.com/v4/letter/m/45deac/32.png) [@Mike\_Fennemore](https://forum.ansible.com/u/Mike_Fennemore)\
**Post date:** [June 4, 2016, 9:51am UTC](https://forum.ansible.com/t/ansible-task-how-to-confirm-if-a-process-is-actually-running/21662/5 "2016-06-04T09:51:59Z")

</div>

Indeed it will. If that is a possibility then I add ignore\_errors:true and still register a variable for the result. The variable will then give you a status and msg if the service fails.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 4, 2016, 1:27pm UTC](https://forum.ansible.com/t/ansible-task-how-to-confirm-if-a-process-is-actually-running/21662/6 "2016-06-04T13:27:52Z")

</div>

Yes, and Ansible would do that, but the problem is that the init script is reporting everything is OK when it isn't.

So you have some options,  
- fix the init script or,  
- use ansible shell or command to check if the process is running or,  
- use the service command a second time and fail it if the second one also report changed.

---

<div class="post-metadata">

**Author:** ![Howard\_Lee](https://avatars.discourse-cdn.com/v4/letter/h/aca169/32.png) [@Howard\_Lee](https://forum.ansible.com/u/Howard_Lee)\
**Post date:** [June 7, 2016, 9:12pm UTC](https://forum.ansible.com/t/ansible-task-how-to-confirm-if-a-process-is-actually-running/21662/7 "2016-06-07T21:12:03Z")

</div>

This is what I do now. Ansible 1.4 has “failed\_when”. [http://docs.ansible.com/ansible/playbooks\_error\_handling.html#controlling-what-defines-failure](http://docs.ansible.com/ansible/playbooks_error_handling.html#controlling-what-defines-failure)

- name: Confirm Automation Agent is running  
command: service mongodb-mms-automation-agent status  
register: agent\_status  
failed\_when: “‘NOT’ in agent\_status.stdout”  
changed\_when: False

(Added “changed\_when: False” to suppress “changed” status)
