Include queried facts as host vars in -A inventory
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful

`-A` emitted hosts with empty vars, so `-F ipaddress,enc_role -A` lost the facts
it just queried. Ansible inventories are far more useful with the values inline.

- Extract factsByHost() (the {host: {fact: value}} builder) and share it between
  -j and -A so the Ansible inventory attaches each host's queried fact(s) as
  host vars, keyed by real fact name.
- Strengthen the Ansible test to assert host vars and add a multi-fact case.
This commit is contained in:
2026-07-05 17:39:53 +10:00
parent 103ebb2393
commit 5d0f69483b
3 changed files with 65 additions and 26 deletions
+30 -24
View File
@@ -276,6 +276,31 @@ func matchValue(flagMatch string, args []string) string {
return ""
}
// factsByHost groups collected facts into {certname: {factname: value}}. Each
// value is keyed by the fact's real name, so multiple -F facts each appear
// under the host; it falls back to the -F string, the role fact (with -R), or
// "value" only when a result carries no name. Shared by the -j and -A outputs.
func factsByHost(collected []fact, factName, roleFact string, showRole bool) map[string]map[string]interface{} {
out := map[string]map[string]interface{}{}
for _, f := range collected {
if _, ok := out[f.Certname]; !ok {
out[f.Certname] = map[string]interface{}{}
}
key := f.Name
if key == "" {
if key = factName; key == "" {
if showRole {
key = roleFact
} else {
key = "value"
}
}
}
out[f.Certname][key] = valueAny(f.Value)
}
return out
}
func allFactsForNode(puppetDBURL, node string) ([]fact, error) {
query, _ := json.Marshal([]interface{}{"=", "certname", node})
return queryPuppetDB(puppetDBURL, string(query))
@@ -346,30 +371,10 @@ func run(cfg config, nodeName, factName, match string, showRole, partial, invers
switch {
case jsonMode:
hostFactMap := map[string]map[string]interface{}{}
for _, f := range collected {
if _, ok := hostFactMap[f.Certname]; !ok {
hostFactMap[f.Certname] = map[string]interface{}{}
}
// Key by the fact's actual name so multiple -F facts (e.g.
// ipaddress,enc_role) each appear under the host. Fall back to the
// computed key only if the result carries no name.
key := f.Name
if key == "" {
if key = factName; key == "" {
if showRole {
key = cfg.RoleFact
} else {
key = "value"
}
}
}
hostFactMap[f.Certname][key] = valueAny(f.Value)
}
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
enc.SetEscapeHTML(false)
_ = enc.Encode(hostFactMap)
_ = enc.Encode(factsByHost(collected, factName, cfg.RoleFact, showRole))
case count:
values := stdinLines
@@ -379,10 +384,11 @@ func run(cfg config, nodeName, factName, match string, showRole, partial, invers
fmt.Println(strings.Join(countResults(values), "\n"))
case ansible:
// Attach each host's queried fact(s) as inventory host vars, e.g.
// `-F ipaddress,enc_role -A` yields hosts with ipaddress + enc_role set.
hosts := map[string]interface{}{}
for _, line := range returnData {
host := strings.Fields(line)[0]
hosts[host] = map[string]interface{}{}
for host, vars := range factsByHost(collected, factName, cfg.RoleFact, showRole) {
hosts[host] = vars
}
inventory := map[string]interface{}{
"all": map[string]interface{}{"hosts": hosts},