negating a command

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

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

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