Over a year ago, I wrote about the possibility to create a YAML/Bash hybrid file, that contains YAML configuration settings and a bash executable part. Lately, I started using a slight modification of this that allows to execute ansible playbooks with ./playbook.yaml as if they were executables.

The heart of this workflow is the command-line tool ansible-playbook. Usually we want to pass additional arguments to the command when invoking a playbook, such as --ask-vault-pass, or a custom --inventory file. Adding the shebang line #!/path/to/ansible-playbook --ask-vault-pass will not work. By default, the full line is taken as the name of the executable, including the space and the arguments.

Therefore, to work around this limitation, we need to create an additional bash script, e.g., ansible-deploy:

#!/bin/bash

ansible-playbook --ask-vault-pass "$@"

Add whatever argument you need. The script acts as a slim wrapper with the sole purpose to add additional arguments. The path of the playbook is passed via $@.

The final piece, is the appropriate shebang line for the playbook, e.g., playbook.yaml

#!/path/to/ansible-deploy

- hosts:
  - all
  tasks:
  - ping: