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 engineer, Cloud/Kubernetes specialist, Free/Open source geek, and an author.

I believe in self CI/CD (Continuous Improvements/Development), also that "the whole is greater than the sum of its parts".

DevOps transformation, automation, data, and metrics are my preferred areas. And I like to help both businesses and people to grow.

Contact Me


Email *

Message *

Latest Post

DevOps is not only a culture - Discussion Panel

Today is my second session JobStack 2023 after my previous one yesterday titled " Platform Engineering: Manage your infrastructure u...

Popular Posts

Blog Archive