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, I'm a passionate Tech Lead DevOps Engineer. 👋

With 16+ years of open-source contributions, 12+ years of professional hands-on experience in DevOps, and an M.Sc. in Data Engineering from Edinburgh Napier University (UK), I enjoy facilitating the growth of both businesses and individuals.

I specialize in Cloud-Native and Kubernetes. I'm also a Free/Open source geek and book author. My favorite topics are DevOps transformation, automation, data, and metrics.

Contact Me


Email *

Message *

Start Your DevOps Engineer Journey!

Start Your DevOps Engineer Journey!
Start your DevOps career for free the Agile way in 2024 with the Dynamic DevOps Roadmap ⭐

Latest Post

2023 Highlights

Image generated with Craiyon . Finally, 2023 is over! What a year! One more crazy year, but it was the culmination ...

Popular Posts

Blog Archive