Generate Ansible inventory from SSH config file.

Update 10/1/2016: I updated script and made a Github repository for it: "ansible-inventory-from-ssh-config".

For a long time I used "Gnome Connection Manager" to manage remote servers, however I moved to "Terminator". I really love Terminator, but If I'm missing something, it will be the sidebar in GCM :D

Anyway, I'm currently using "~/.ssh/config" file to manage remote servers (which contains: servers name, hostname, ssh port, username), till I started using best configuration management tool ever ... Ansible :D

So, I was needed to make a Ansible inventory file from the servers I manage, and as usual AWK is the best for this purpose ... quick and simple :D

I wrote small snippet to generate Ansible inventory from SSH config file, it's really simple script and assume you have "Host", "Hostname", "Port", "User" in separated lines, and valid SSH config file (there are no checks or tests done at all).

#! /bin/bash
#This snippet assume each "Host", "Hostname", "Port", "User" in separated line, and valid ssh config file.

ssh_conf_to_ansible () {
     {gsub(/\s+/," ");
      gsub("Host ","");
      gsub("Hostname ","ansible_ssh_host=");
      gsub("Port ","ansible_ssh_port=");
      gsub("User ","ansible_ssh_user=")};
 NR%4{printf $0" ";

egrep -i "^(Host|Hostname|Port|User)" ~/.ssh/config | ssh_conf_to_ansible
Powered by Blogger.

Hello, my name is Ahmed AbouZaid and this is my "lite" technical blog!

I'm a passionate DevOps, Linux system engineer, Cloud/Kubernetes specialist, Free/Open source geek, author, interested in environment, calligraphy, and I believe that “Details Matter”!

DevOps transformation, automation, data, and metrics are my preferred areas. I have a built-in monitoring chip, and too lazy to do anything manually :D

Popular Posts