Finding Hosts on Ansible Inventories

Erika Heidi - May 2 '19 - - Dev Community

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"
Enter fullscreen mode Exit fullscreen mode

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

$ inventory mysql
Enter fullscreen mode Exit fullscreen mode

And this is the kind of output I get:

[mysqlservers]
mysql1 ansible_host=165.22.254.246
Enter fullscreen mode Exit fullscreen mode

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

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player