0

Is it possible to be able to enter foo and have bash automatically convert it to time foo && notify-send "PLAYBOOK COMPLETE"?

I'd specifically like this to apply only to ansible-playbook commands (for those not familiar with Ansible, any number of options, arguments etc. might follow ansible-playbook so a simple alias probably won't do the trick) so that I get notified when a long playbook has finished.

A command before every bash command looked promising, but unfortunately didn't work correctly for me.

1

With a shell function:

function ansible-playbook { time command ansible-playbook "$@" && notify-send "PLAYBOOK COMPLETE"; }

or

ansible-playbook () { time command ansible-playbook "$@" && notify-send "PLAYBOOK COMPLETE"; }

The latter form is the POSIX way to define a function but ansible-playbook alias (if any) may interfere. The former form is immune but not portable. If there is no alias and you're using Bash then either form will work.

Or you can write a wrapper script that works in a similar way. If you name it ansible-playbook then make sure it calls the real ansible-playbook, not itself. In the above function the word command prevents calling the function itself in an infinite loop.

| improve this answer | |
  • Many thanks for this Kamil! Does this need to go in a bashrc file or something like that? – Sam Sep 29 '19 at 9:29
  • @Sam If you want the function to be automatically defined for Bash then yes, in ~/.bashrc. A properly managed wrapper script will work with any shell. – Kamil Maciorowski Sep 29 '19 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.