Hi all,
I am using consul-template to generate configuration for my application and I have the following template which extracts network interfaces attached to an EC2 instance
{{- $ip_addresses := sockaddr ("GetPrivateInterfaces | exclude \"name\" \"lo\"") -}}
{{ printf "%s" $ip_addresses }}
This produces a configuration file which results in the following data format:
[10.77.29.85/21 {2 9001 primary 0a:24:ab:00:6d:9d up|broadcast|multicast|running} 10.77.31.163/21 {3 9001 secondary 0a:0c:0b:a4:03:84 up|broadcast|multicast|running}]
From this result I want to loop through the data and extract the name of the interface device. In this example, they are “primary” and “secondary”
I have tried using the range syntax but I get the error shown below:
{{ with $ifAddrs := sockaddr ("GetAllInterfaces | exclude \"name\" \"lo\"") -}}
{{- range $ifAddrs -}}
{{- sockaddr ("Attr \"name\" .") -}}
{{- end -}}
{{- end }}
execute: template: :2:12: executing “” at <$ifAddrs>: range can’t iterate over [10.77.29.85/21 {2 9001 primary 0a:24:ab:00:6d:9d up|broadcast|multicast|running} fe80::824:abff:fe00:6d9d/64 {2 9001 primary 0a:24:ab:00:6d:9d up|broadcast|multicast|running} 10.77.31.163/21 {3 9001 secondary 0a:0c:0b:a4:03:84 up|broadcast|multicast|running} fe80::80c:bff:fea4:384/64 {3 9001 secondary 0a:0c:0b:a4:03:84 up|broadcast|multicast|running}]
I have used the go templating examples from template package - github.com/hashicorp/go-sockaddr/template - Go Packages
Obviously I am not understanding the range syntax well enough. What am I doing wrong?
Jeff
Here is a snippet I tried based on the example from the documentation
{{ with $ifAddrs := sockaddr ("GetAllInterfaces | include \"type\" \"IP\" | sort \"+type,+address\"") -}}
{{- range $ifAddrs -}}
{{- sockaddr ("Attr \"address\" .") }} -- {{ sockaddr ("Attr \"network\" .") }}/{{ sockaddr ("Attr \"size\" .") -}}
{{- end -}}
{{- end }}
consul-template -template ena-exceeded.ctmpl:config.yml -log-level info -once
2023-11-03T20:37:10.828-0600 [INFO] consul-template v0.33.0 (15b89c9)
2023-11-03T20:37:10.828-0600 [INFO] (runner) creating new runner (dry: false, once: true)
2023-11-03T20:37:10.828-0600 [INFO] (runner) creating watcher
2023-11-03T20:37:10.829-0600 [INFO] (runner) starting
2023-11-03T20:37:10.830-0600 [ERR] (cli) ena-exceeded.ctmpl: execute: template: :2:12: executing "" at <$ifAddrs>: range can't iterate over [10.77.29.85/21 {2 9001 primary 0a:24:ab:00:6d:9d up|broadcast|multicast|running} 10.77.31.163/21 {3 9001 secondary 0a:0c:0b:a4:03:84 up|broadcast|multicast|running} 127.0.0.1/8 {1 65536 lo up|loopback|running} ::1 {1 65536 lo up|loopback|running} fe80::80c:bff:fea4:384/64 {3 9001 secondary 0a:0c:0b:a4:03:84 up|broadcast|multicast|running} fe80::824:abff:fe00:6d9d/64 {2 9001 primary 0a:24:ab:00:6d:9d up|broadcast|multicast|running}]
I managed to solve this by querying for the required fields and making the output a comma separated string and then using split to make it iteratable.
{{- $interfaces := sockaddr ("GetPrivateInterfaces | exclude \"name\" \"lo\" | join \"name\" \",\" ") | split "," -}}
{{- range $device_name := $interfaces }}
// do stuff with $device_name
{{ end }}
Not sure how I would approach this if I required more than one field from the data returned from GetPrivateInterfaces
query.