negating a command

40 views
Skip to first unread message

rambius

unread,
Dec 19, 2014, 12:03:34 PM12/19/14
to ansible...@googlegroups.com
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

Brian Coca

unread,
Dec 19, 2014, 12:15:09 PM12/19/14
to ansible...@googlegroups.com
look at the failed_when directive, it allows you to chose the failure
condition of a task.


--
Brian Coca

rambius

unread,
Dec 24, 2014, 12:57:47 PM12/24/14
to ansible...@googlegroups.com
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

---

- name: ensure server is not running
  command: pgrep -f programname
  register: is_server_running
  failed_when: is_server_running.rc == 0
- name: backupserver
  command: serverbackup.sh

Regards
Rambius
Reply all
Reply to author
Forward
0 new messages