Skip to content

Instantly share code, notes, and snippets.

@kvaps
Last active January 6, 2020 12:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kvaps/ff9d1af9a09c22ed481a02af5a4b9627 to your computer and use it in GitHub Desktop.
Save kvaps/ff9d1af9a09c22ed481a02af5a4b9627 to your computer and use it in GitHub Desktop.
ansible jsonnet proof of concept
# ansible all -i inventory.sh -m debug -a var=host_specific_var

host-a | SUCCESS => {
    "host_specific_var": "abcde-a"
}
host-b | SUCCESS => {
    "host_specific_var": "abcde-b"
}
host-c | SUCCESS => {
    "host_specific_var": "abcde-c"
}
#!/usr/bin/jsonnet
local host_specific_var = 'abcde';
{
jsonnet_hosts: {
hosts: [
'host-a',
'host-b',
'host-c',
],
vars: {
common_var: 'host01-host02',
},
},
_meta: {
hostvars: {
[x]: {
host_specific_var: host_specific_var + '-' + std.split(x, '-')[1],
}
for x in $.jsonnet_hosts.hosts
},
},
}
#!/bin/sh
dynamic_json_file=inventory.jsonnet
if [ "$1" == "--list" ] ; then
jsonnet ${dynamic_json_file}
elif [ "$1" == "--host" ]; then
echo '{"_meta": {"hostvars": {}}}'
else
echo "{ }"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment