# negating  a command

**URL:** <https://forum.ansible.com/t/negating-a-command/17111>\
**Category:** Ansible Project\
**Created:** [December 19, 2014, 5:03pm UTC](https://forum.ansible.com/t/negating-a-command/17111 "2014-12-19T17:03:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rambius](https://avatars.discourse-cdn.com/v4/letter/r/bc79bd/32.png) [@rambius](https://forum.ansible.com/u/rambius)\
**Post date:** [December 19, 2014, 5:03pm UTC](https://forum.ansible.com/t/negating-a-command/17111/1 "2014-12-19T17:03:34Z")

</div>

Hello,

I am new to ansible and I am experimenting with a simple playbook I created. My question is how to negate the return code of a command. I use pgrep to check if a server is running and if it is not running I will back it up. If it is running the playbook should fail with an appropriate message. I use the following snippet that works:

- name: ensure server is not running  
command: pgrep -f programname  
register: is\_server\_running  
ignore\_errors: True
- name: backup server  
when: is\_server\_running|failed  
command: serverbackup.sh

This looks clumsy though. I tried with the following but does not work:

- name: ensure server is not running  
command: ! pgrep -f programname

! pgrep -f programname works fine when I execute it directly from the command line.

I would be very grateful for any help.

Regards  
Rambius

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 19, 2014, 5:15pm UTC](https://forum.ansible.com/t/negating-a-command/17111/2 "2014-12-19T17:15:06Z")

</div>

look at the failed\_when directive, it allows you to chose the failure  
condition of a task.

---

<div class="post-metadata">

**Author:** ![rambius](https://avatars.discourse-cdn.com/v4/letter/r/bc79bd/32.png) [@rambius](https://forum.ansible.com/u/rambius)\
**Post date:** [December 24, 2014, 5:57pm UTC](https://forum.ansible.com/t/negating-a-command/17111/3 "2014-12-24T17:57:47Z")

</div>

Hello,

19 декември 2014, петък, 12:15:09 UTC-5, Brian Coca написа:

> look at the failed\_when directive, it allows you to chose the failure  
> condition of a task.

Thank you. failed\_when did the job. My playbook now looks like
