AnsibleInventoryGenerator.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System.Text;
  2. using RackPeek.Domain.Resources;
  3. namespace RackPeek.Domain.UseCases.Ansible;
  4. public sealed record InventoryOptions
  5. {
  6. /// <summary>
  7. /// If set, create groups based on these tags.
  8. /// Example: ["env", "site"] -> [env], [site]
  9. /// </summary>
  10. public IReadOnlyList<string> GroupByTags { get; init; } = [];
  11. /// <summary>
  12. /// If set, create groups based on these label keys.
  13. /// Example: ["env", "site"] -> [env_prod], [site_london]
  14. /// </summary>
  15. public IReadOnlyList<string> GroupByLabelKeys { get; init; } = [];
  16. /// <summary>
  17. /// If set, emitted under [all:vars].
  18. /// </summary>
  19. public IDictionary<string, string> GlobalVars { get; init; } = new Dictionary<string, string>();
  20. }
  21. public sealed record InventoryResult(string InventoryText, IReadOnlyList<string> Warnings);
  22. public static class AnsibleInventoryGenerator
  23. {
  24. /// <summary>
  25. /// Generate an Ansible inventory in INI format from RackPeek resources.
  26. /// </summary>
  27. public static InventoryResult ToAnsibleInventoryIni(
  28. this IReadOnlyList<Resource> resources,
  29. InventoryOptions? options = null)
  30. {
  31. options ??= new InventoryOptions();
  32. var warnings = new List<string>();
  33. var hosts = new List<HostEntry>();
  34. // Build host entries (only resources that look addressable)
  35. foreach (var r in resources)
  36. {
  37. var address = GetAddress(r);
  38. if (string.IsNullOrWhiteSpace(address))
  39. {
  40. continue;
  41. }
  42. var hostVars = BuildHostVars(r, address);
  43. hosts.Add(new HostEntry(r, hostVars));
  44. }
  45. // Groups: kind + tags + label-based
  46. var groupToHosts = new Dictionary<string, List<HostEntry>>(StringComparer.OrdinalIgnoreCase);
  47. void AddToGroup(string groupName, HostEntry h)
  48. {
  49. if (string.IsNullOrWhiteSpace(groupName)) return;
  50. groupName = SanitizeGroup(groupName);
  51. if (!groupToHosts.TryGetValue(groupName, out var list))
  52. groupToHosts[groupName] = list = new List<HostEntry>();
  53. // avoid duplicates if multiple rules add the same host
  54. if (!list.Any(x => string.Equals(x.Resource.Name, h.Resource.Name, StringComparison.OrdinalIgnoreCase)))
  55. list.Add(h);
  56. }
  57. foreach (var h in hosts)
  58. {
  59. // Tag groups
  60. var tags = options.GroupByTags.Intersect(h.Resource.Tags).ToArray();
  61. foreach (var tag in tags)
  62. {
  63. if (string.IsNullOrWhiteSpace(tag)) continue;
  64. AddToGroup(tag, h);
  65. }
  66. // Label-based groups: e.g. env=prod -> [env_prod]
  67. foreach (var key in options.GroupByLabelKeys)
  68. {
  69. if (string.IsNullOrWhiteSpace(key)) continue;
  70. if (h.Resource.Labels.TryGetValue(key, out var val) && !string.IsNullOrWhiteSpace(val))
  71. {
  72. AddToGroup($"{key}_{val}", h);
  73. }
  74. }
  75. }
  76. // Build output
  77. var sb = new StringBuilder();
  78. // [all:vars]
  79. if (options.GlobalVars.Count > 0)
  80. {
  81. sb.AppendLine("[all:vars]");
  82. foreach (var kvp in options.GlobalVars.OrderBy(k => k.Key, StringComparer.OrdinalIgnoreCase))
  83. sb.AppendLine($"{kvp.Key}={EscapeIniValue(kvp.Value)}");
  84. sb.AppendLine();
  85. }
  86. // Emit groups sorted, hosts sorted
  87. foreach (var group in groupToHosts.Keys.OrderBy(x => x, StringComparer.OrdinalIgnoreCase))
  88. {
  89. sb.AppendLine($"[{group}]");
  90. foreach (var h in groupToHosts[group].OrderBy(x => x.Resource.Name, StringComparer.OrdinalIgnoreCase))
  91. {
  92. sb.Append(h.Resource.Name);
  93. // host vars (inline)
  94. foreach (var kvp in h.HostVars.OrderBy(k => k.Key, StringComparer.OrdinalIgnoreCase))
  95. sb.Append($" {kvp.Key}={EscapeIniValue(kvp.Value)}");
  96. sb.AppendLine();
  97. }
  98. sb.AppendLine();
  99. }
  100. return new InventoryResult(sb.ToString().TrimEnd(), warnings);
  101. }
  102. // ---------- helpers ----------
  103. private sealed record HostEntry(Resource Resource, Dictionary<string, string> HostVars);
  104. private static string? GetAddress(Resource r)
  105. {
  106. // Preferred: ansible_host, else ip, else hostname
  107. if (r.Labels.TryGetValue("ansible_host", out var ah) && !string.IsNullOrWhiteSpace(ah))
  108. return ah;
  109. if (r.Labels.TryGetValue("ip", out var ip) && !string.IsNullOrWhiteSpace(ip))
  110. return ip;
  111. if (r.Labels.TryGetValue("hostname", out var hn) && !string.IsNullOrWhiteSpace(hn))
  112. return hn;
  113. return null;
  114. }
  115. private static Dictionary<string, string> BuildHostVars(Resource r, string address)
  116. {
  117. var vars = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
  118. {
  119. ["ansible_host"] = address
  120. };
  121. // Copy any labels prefixed with ansible_
  122. foreach (var (k, v) in r.Labels)
  123. {
  124. if (string.IsNullOrWhiteSpace(k) || string.IsNullOrWhiteSpace(v)) continue;
  125. if (k.StartsWith("ansible_", StringComparison.OrdinalIgnoreCase))
  126. {
  127. // don't overwrite ansible_host we already derived unless explicitly present
  128. if (string.Equals(k, "ansible_host", StringComparison.OrdinalIgnoreCase))
  129. vars["ansible_host"] = v;
  130. else
  131. vars[k] = v;
  132. }
  133. }
  134. return vars;
  135. }
  136. private static string SanitizeGroup(string s)
  137. {
  138. // Ansible group names: letters/digits/underscore
  139. var sb = new StringBuilder(s.Length);
  140. foreach (var ch in s.Trim().ToLowerInvariant())
  141. {
  142. if (char.IsLetterOrDigit(ch) || ch == '_')
  143. sb.Append(ch);
  144. else if (ch == '-' || ch == '.' || ch == ' ')
  145. sb.Append('_');
  146. // drop everything else
  147. }
  148. var result = sb.ToString();
  149. return string.IsNullOrWhiteSpace(result) ? "group" : result;
  150. }
  151. private static string EscapeIniValue(string value)
  152. {
  153. // quote if it contains spaces or special chars
  154. if (string.IsNullOrEmpty(value)) return "\"\"";
  155. var needsQuotes = value.Any(ch => char.IsWhiteSpace(ch) || ch is '"' or '\'' or '=');
  156. if (!needsQuotes) return value;
  157. return "\"" + value.Replace("\"", "\\\"") + "\"";
  158. }
  159. }