Finding Hosts on Ansible Inventories

Finding Hosts on Ansible Inventories

Posted 4 years ago in

title: Finding Hosts on Ansible Inventories

published: true description: tags: quickfix, protips, ansible, devops

I tend to work with a bunch of different servers when writing and testing tutorials. Even though I always organize my nodes into groups and give them alias to facilitate running commands and playbooks, it still gets a bit chaotic. I found myself having to open or cat the contents of my Ansible inventory many times a day to get IP addresses of servers I knew only by alias name.

I tried to find a native, neat way to get this info, but I couldn't. This is what I came up with :)

alias inventory="cat /etc/ansible/hosts | grep $1"

Now whenever I need to get the IP from a host in my Ansible inventory, I just run:

$ inventory mysql

And this is the kind of output I get:

mysql1 ansible_host=

Just remember to include the alias in your .basrc (or .zshrc if you use Zsh like me) file so the alias is preserved.