Skip to content

Instantly share code, notes, and snippets.

@aabouzaid
Last active August 29, 2015 14:18
Show Gist options
  • Save aabouzaid/33f4233d71d44bb31a0b to your computer and use it in GitHub Desktop.
Save aabouzaid/33f4233d71d44bb31a0b to your computer and use it in GitHub Desktop.
Simple snippet to reformat hosts in ssh conf file to Ansible inventory file format.
#! /bin/bash
#This snippet assume each "Host", "Hostname", "Port", "User" in separated line, and valid ssh config file.
ssh_conf_to_ansible () {
awk 'BEGIN{IGNORECASE=1}
{gsub(/\s+/," ");
gsub("Host ","");
gsub("Hostname ","ansible_ssh_host=");
gsub("Port ","ansible_ssh_port=");
gsub("User ","ansible_ssh_user=")};
NR%4{printf $0" ";
next;
}1'
}
egrep -i "^(Host|Hostname|Port|User)" ~/.ssh/config | ssh_conf_to_ansible
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment