Skip to content

Instantly share code, notes, and snippets.

@czunker
Created August 15, 2014 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save czunker/d6ae14c1c324e6c26cb8 to your computer and use it in GitHub Desktop.
Save czunker/d6ae14c1c324e6c26cb8 to your computer and use it in GitHub Desktop.
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