Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create Ansible dynamic inventory from Puppet CA
#!/bin/bash
/usr/bin/puppet cert list --all > /dev/null 2>&1
if [ $? -ne 0 ]
then
echo "Puppet error"
exit 1
fi
HOSTS="{\n\t\"all\":\n\t\t[\n"
FIRST_HOST="true"
while read hostname
do
# this way i don't get a trailing comma
if [ "${FIRST_HOST}" == "true" ]
then
HOSTS="${HOSTS}\t\t\t\"${hostname}\""
unset FIRST_HOST
else
HOSTS="${HOSTS}, \"${hostname}\""
fi
# can't use command | while because of subshell and loss of variable
done < <(/usr/bin/puppet cert list --all | egrep "^\+.*" | awk -F"\"" '{ print $2 }' | grep "example.com")
HOSTS="${HOSTS}\n\t\t]\n}"
echo -e "${HOSTS}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.