In one of the previous blog posts I described the playbook I use to collect SSH keys from network devices. As I use it quite often, it became tedious to write ansible-playbook path-to-playbook every time I wanted to run the collection process.
Ansible playbooks are YAML documents, and YAML documents use # to start comments, so I thought “what if I’d use a YAML comment to add shebang and turn my YAML document into a script”
TL&DR: It works. Now for the longer story…
Bash (or any other shell) will try to execute a file that has execute bit set. Use chmod +x playbook to make the playbook executable.
Text files with execute bit set are assumed to be shell scripts unless the first line in the file starts with #! sequence which specifies the absolute path to the script interpreter to use.
You can use which ansible-playbook to find path to ansible-playbook command on your system and add that as the first line of your playbook:
It’s even better to use env command. It’s always (I hope) in /usr/bin directory and uses PATH variable to find the command you want executed. The first line of your Ansible playbook should therefore be:
Cherry on the cake: add a symbolic link to your playbook into one of the directories in your search path. For example:
$ ln –s /vagrant/tools/ssh-keys/get-keys.yml ~/bin/get-ssh-keys
Now you can execute your Ansible playbook like any other Linux command.
Supposedly, you can add environment variables to env command line. I tried to do that to change Ansible STDOUT callback to dense but it didn’t work. A solution to this challenge would be highly appreciated ;)
No idea what I’m talking about? Check out the Ansible for Networking Engineers webinar and online course.