Your browser failed to load CSS style sheets. Your browser or web proxy might not support elliptic-curve TLS

Building network automation solutions

6 week online course

Start now!
back to overview

Turn Your Ansible Playbook into a Bash Command

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:

#!/usr/local/bin/ansible-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:

#!/usr/bin/env ansible-playbook

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.

8 comments:

  1. export ANSIBLE_STDOUT_CALLBACK=dense; ansible-playbook [options]

    ReplyDelete
    Replies
    1. Yeah, sure... have you tried adding that to the #! line? Did it work? If it did, under which shell?

      Delete
  2. I'd actually preffer making aliases in bashrc. Makes it easier to manage and move around instead of creating loads of symlinks.
    Just add this:
    alias get-ssh-keys="/vagrant/tools/ssh-keys/get-keys.yml"
    inside ~./bashrc
    Reload bash by typing source ~./bashrc

    You could also be creative and make a functions and such

    ReplyDelete
  3. Ivan - aren't we now moving the "CLI"[-like] approach, upstream (the one we are just trying to depart, via the more structured and robust approach of RESTAPI), to the Bash shell, by maintaining more complex scripts?

    Who is / how are we going to fix the portability of Bash vs other shells (e.g. ksh, zsh) when taking scripts and move them across *nix platforms, or with diff path(s) and env, troubleshoot shell exit codes, etc.? Or am I misunderstanding the intent here?

    ReplyDelete
    Replies
    1. I would recommend anyone believing in the magic of whatever replacing CLI to read this (in particular #2):

      http://queue.acm.org/detail.cfm?id=1921361

      As for portability of shells, sysadmins might be yammering about many things, but the good ones seem to be doing a decent job:

      http://highscalability.com/blog/2013/11/19/we-finally-cracked-the-10k-problem-this-time-for-managing-se.html

      Obviously people who meet the requirements of RFC 1925 Rule #4 will tell you CLI is dead. Let's revisit this in 10 years ;)

      Delete
    2. Didn't mean to deviate from the main topic too much, but when looking at some of the CLIs just front-ending RESTAPIs, I wonder if "survival" of CLI isn't just in the eyes of the beholder.

      Delete
  4. This link explains the reason why the environment variable doesn't work in the shebang:

    https://stackoverflow.com/a/10217307

    Anyway, you can add that variable permanently to your shell http://www.linuxfromscratch.org/blfs/view/8.0/postlfs/profile.html:

    export ANSIBLE_STDOUT_CALLBACK=dense

    or run the script with the env variable in the same line:

    $ ANSIBLE_STDOUT_CALLBACK=dense ~/bin/get-ssh-keys

    ReplyDelete
    Replies
    1. Thanks a million! Your Google-Fu is stronger than mine ;)

      Delete

You don't have to log in to post a comment, but please do provide your real name/URL. Anonymous comments might get deleted.

Sidebar