What does the SSH Config to Ansible Inventory Converter do?
The SSH Config to Ansible Inventory Converter parses OpenSSH client configuration files (~/.ssh/config) and compiles them into ready-to-run Ansible INI (hosts.ini) or Ansible YAML inventory files. It automatically maps SSH host connection variables (HostName, User, Port, IdentityFile) to their standard Ansible inventory counterparts (ansible_host, ansible_user, ansible_port, ansible_ssh_private_key_file).
Core Concepts
Understanding SSH config to Ansible inventory mapping:
- Host & Variable Extraction: Extracts SSH aliases into Ansible inventory inventory identifiers and assigns
ansible_hostfromHostName. - Credential & Port Mapping: Maps
IdentityFilepaths toansible_ssh_private_key_fileand attaches customansible_portvalues for non-standard SSH ports. - Dual Inventory Formats: Compiles either classic flat Ansible INI format (
[group] host key=value) or modern structured Ansible YAML hierarchy. - Custom Host Groups: Places all imported hosts under a customizable group identifier (such as
[webservers]or[database]).
How to use the tool?
- Paste SSH Config: Paste your
~/.ssh/configfile content into the editor or click Load Sample Config. - Configure Format & Group: Select your desired output format (Ansible INI or Ansible YAML) and enter a Group Name (e.g.
webservers). - Convert & Copy: Click Convert to Ansible Inventory, then click Copy or Download to export the inventory file.
Related Developer Utilities
If you work with SSH keys, server provisioning, and infrastructure automation, explore these complementary tools:
- SSH Config to /etc/hosts Converter: Convert SSH config hosts into local
/etc/hostsaliases. - SSH Public Key Fingerprint Generator: Calculate SHA256 & MD5 SSH key fingerprints.
- SSH Public Key Format Converter: Convert keys between OpenSSH, RFC 4716, and PEM formats.
- YAML to JSON Converter: Convert structured YAML documents into clean JSON.
REST API Integration
Blueutils provides a free REST API endpoint (POST https://blueutils.com/api/ssh/ssh-config-to-ansible) to programmatically convert OpenSSH client configuration (~/.ssh/config) files into Ansible INI (hosts.ini) or Ansible YAML inventory files.
API Request Parameters
| Name | Type | Description | Example |
|---|---|---|---|
rawText |
String | OpenSSH ~/.ssh/config content to parse. |
"Host web-1\n HostName 10.0.1.10\n User ubuntu" |
format |
String | Optional. Output format: "ini" (default) or "yaml". |
"ini" |
groupName |
String | Optional. Ansible host group name (default: "servers"). |
"webservers" |
API Request Payload Examples
cURL
curl -X POST https://blueutils.com/api/ssh/ssh-config-to-ansible \
-H "Content-Type: application/json" \
-d '{
"rawText": "Host web-1\n HostName 10.0.1.10\n User ubuntu\n Port 2222\n IdentityFile ~/.ssh/id_rsa",
"format": "ini",
"groupName": "webservers"
}'Python
import requests
url = "https://blueutils.com/api/ssh/ssh-config-to-ansible"
payload = {
"rawText": "Host web-1\n HostName 10.0.1.10\n User ubuntu\n Port 2222\n IdentityFile ~/.ssh/id_rsa",
"format": "ini",
"groupName": "webservers"
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.json())Java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
String jsonPayload = """
{
"rawText": "Host web-1\\n HostName 10.0.1.10\\n User ubuntu\\n Port 2222\\n IdentityFile ~/.ssh/id_rsa",
"format": "ini",
"groupName": "webservers"
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://blueutils.com/api/ssh/ssh-config-to-ansible"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonPayload))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}API Response Parameters
| Name | Type | Description | Example |
|---|---|---|---|
isValid |
Boolean | Indicates whether conversion succeeded. | true |
format |
String | Output format produced ("ini" or "yaml"). |
"ini" |
groupName |
String | Name of the Ansible host group. | "webservers" |
hostsCount |
Number | Count of converted host definitions. | 1 |
converted |
String | Formatted Ansible inventory content string. | "[webservers]\nweb-1 ansible_host=10.0.1.10..." |
API Response Payload Examples
Success Response (HTTP 200 OK)
{
"isValid": true,
"format": "ini",
"groupName": "webservers",
"hostsCount": 1,
"converted": "[webservers]\nweb-1 ansible_host=10.0.1.10 ansible_user=ubuntu ansible_port=2222 ansible_ssh_private_key_file=~/.ssh/id_rsa"
}Validation Failure Response (HTTP 400 Bad Request)
{
"isValid": false,
"error": "No valid Host blocks were found in the SSH config."
}Rate Limit Exceeded Response (HTTP 429 Too Many Requests)
{
"error": "API rate limit exceeded. Please wait or contact support@blueutils.com."
}Why use an API to convert SSH config to Ansible?
Integrating the SSH Config to Ansible Converter API into DevOps provisioning scripts, cluster orchestrators, or AI agent tool calling provides key benefits:
- Rapid Script Validation: Generates dynamic Ansible inventories from workstation developer configs or cloud bastion nodes on the fly.
- Optimized Token Efficiency for AI Agents: LLMs frequently mix up Ansible INI and YAML syntax keys. Calling the API translates connection variables deterministically without token consumption.
- Deterministic Accuracy Without Hallucinations: Ensures 100% compliant Ansible syntax generation for both INI and YAML specifications.
Native Usage
How to convert SSH configs to Ansible inventories locally in terminal environments or scripts:
Windows (CMD / PowerShell)
# Parse ~/.ssh/config into Ansible INI in PowerShell
$configPath = "$env:USERPROFILE\.ssh\config"
if (Test-Path $configPath) {
Write-Output "[servers]"
Get-Content $configPath | ForEach-Object {
if ($_ -match '^\s*Host\s+([^\*]+)$') { $h = $matches[1].Trim() }
elseif ($_ -match '^\s*HostName\s+(\S+)') { Write-Output "$h ansible_host=$($matches[1].Trim())" }
}
}Linux / Unix (Bash)
# Parse ~/.ssh/config into Ansible INI format in Linux
awk '/^Host / {host=$2} /^ HostName / {print host " ansible_host="$2}' ~/.ssh/configPython
Using Python:
import re
with open("~/.ssh/config".replace("~", "/home/user")) as f:
text = f.read()
print("[servers]")
for block in re.findall(r'Host\s+([^\n]+)\n\s+HostName\s+([^\n]+)', text):
print(f"{block[0].strip()} ansible_host={block[1].strip()}")Java
Using Java:
import java.nio.file.*;
import java.util.List;
public class SshToAnsibleExample {
public static void main(String[] args) throws Exception {
List<String> lines = Files.readAllLines(Paths.get(System.getProperty("user.home"), ".ssh", "config"));
System.out.println("[servers]");
String currentHost = "";
for (String line : lines) {
if (line.trim().startsWith("Host ")) {
currentHost = line.trim().substring(5);
} else if (line.trim().startsWith("HostName ")) {
System.out.println(currentHost + " ansible_host=" + line.trim().substring(9));
}
}
}
}