Skip to content

Instantly share code, notes, and snippets.

@davidtsadler
Last active June 28, 2018 15:31
Show Gist options
  • Save davidtsadler/786620cad83a544a553cdc206334b423 to your computer and use it in GitHub Desktop.
Save davidtsadler/786620cad83a544a553cdc206334b423 to your computer and use it in GitHub Desktop.
Bash script to install puppet on Ubuntu 16.04. Puppet can then be used to provision the machine.
#!/bin/bash
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root." >&2
exit 1
fi
PROVISION_REPO=$1
BRANCH=$2
if [ "$#" -ne 2 ]; then
echo "Usage: $0 PROVISION_REPO BRANCH"
exit 1
fi
PUPPET_BIN=/opt/puppetlabs/puppet/bin
PUPPET_ENV=/etc/puppetlabs/code/environments
which wget > /dev/null 2>&1
WGET_CHECK=$?
which git > /dev/null 2>&1
GIT_CHECK=$?
test -f ${PUPPET_BIN}/puppet
PUPPET_CHECK=$?
if [ ${WGET_CHECK} == "1" ] || [ ${GIT_CHECK} == "1" ] || [ ${PUPPET_CHECK} == "1" ]; then
echo "Initial apt-get update..."
apt-get update >/dev/null
fi
if [ ${WGET_CHECK} == "1" ]; then
echo "Installing wget..."
apt-get --yes install wget >/dev/null
echo "Wget installed."
fi
if [ ${GIT_CHECK} == "1" ]; then
echo "Installing git..."
apt-get --yes install git >/dev/null
echo "Git installed."
fi
if [ ${PUPPET_CHECK} == "1" ]; then
echo "Configuring PuppetLabs repo..."
source /etc/lsb-release
apt-key adv --fetch-keys http://apt.puppetlabs.com/DEB-GPG-KEY-puppet >/dev/null
TMP_DEB=$(mktemp)
wget --output-document=${TMP_DEB} http://apt.puppetlabs.com/puppetlabs-release-pc1-${DISTRIB_CODENAME}.deb 2>/dev/null
dpkg -i ${TMP_DEB} >/dev/null
apt-get update >/dev/null
echo "Installing puppet..."
apt-get --yes install puppet-agent >/dev/null
unlink ${TMP_DEB}
echo "Puppet installed."
fi
if [ ! -d ${PUPPET_ENV}/production.orig ]; then
mv ${PUPPET_ENV}/production ${PUPPET_ENV}/production.orig
fi
if [ ! -d ${PUPPET_ENV}/production ]; then
echo "Installing puppet manifests..."
cd ${PUPPET_ENV}
git clone -q ${PROVISION_REPO} production
cd production
git checkout -q ${BRANCH}
${PUPPET_BIN}/gem install r10k --no-rdoc --no-ri >/dev/null
[ -f Puppetfile ] && ${PUPPET_BIN}/r10k puppetfile install >/dev/null
echo "Puppet manifests installed."
echo "Provisioning..."
${PUPPET_BIN}/puppet apply --environment=production ${PUPPET_ENV}/production/manifests/ >/dev/null
echo "Done."
fi
@diegolaciar
Copy link

How would you update this script If you have a private repository ? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment