Skip to content

Instantly share code, notes, and snippets.

@lxhunter
Last active August 15, 2019 09:47
Show Gist options
  • Save lxhunter/4cef05e58569db80543908dc23b8dbdb to your computer and use it in GitHub Desktop.
Save lxhunter/4cef05e58569db80543908dc23b8dbdb to your computer and use it in GitHub Desktop.
Ansible deployment with Travis-CI
#!/bin/bash
[ -z "$GITHUB_SSH_URL" ] && echo "You need to set GITHUB_SSH_URL - e.g. GITHUB_SSH_URL=git@github.com:user/repository.git" && exit 1;
[ -z "$ANSIBLE_PLAYBOOK" ] && echo "You need to set ANSIBLE_PLAYBOOK - e.g. ANSIBLE_PLAYBOOK=deploy.yml" && exit 1;
[ -z "$ANSIBLE_HOSTS" ] && echo "You need to set ANSIBLE_HOSTS - e.g. ANSIBLE_HOSTS=host.ini" && exit 1;
[ -z "$ANSIBLE_ROLES_FOLDER" ] && echo "You need to set ANSIBLE_ROLES_FOLDER - e.g. ANSIBLE_ROLES_FOLDER=/var/tmp/user-repository/roles" && exit 1;
[ -z "$ANSIBLE_REQUIREMENTS_FILE" ] && echo "You need to set ANSIBLE_REQUIREMENTS_FILE - e.g. ANSIBLE_REQUIREMENTS_FILE=/var/tmp/user-repository/requirements.yml" && exit 1;
[ -z "$ANSIBLE_SSH_USER" ] && echo "You need to set ANSIBLE_SSH_USER - e.g. ANSIBLE_SSH_USER=user" && exit 1;
[ -z "$GIT_REPOSITORY" ] && echo "You need to set GIT_REPOSITORY - e.g. GIT_REPOSITORY=/var/tmp/user-repository" && exit 1;
[ -z "$GITHUB_BRANCH" ] && echo "You need to set GITHUB_BRANCH - e.g. GITHUB_BRANCH=master" && exit 1;
if [ -n "$ANSIBLE_VAULT_PASSWORD" ]; then
echo "Creating Vault-file: ${ANSIBLE_VAULT_FILE}"
/bin/cat <<EOM >$ANSIBLE_VAULT_FILE
${ANSIBLE_VAULT_PASSWORD}
EOM
fi
if [ ! -d "${GIT_REPOSITORY}" ]; then
echo "Cloning ${GITHUB_SSH_URL}:${GITHUB_BRANCH} into ${GIT_REPOSITORY}"
git clone "${GITHUB_SSH_URL}" "${GIT_REPOSITORY}" -b "${GITHUB_BRANCH}"
fi
if [ ! -d "${ANSIBLE_ROLES_FOLDER}" ]; then
echo "Installing roles into ${ANSIBLE_ROLES_FOLDER}"
ansible-galaxy install -r "${ANSIBLE_REQUIREMENTS_FILE}" -p "${ANSIBLE_ROLES_FOLDER}" -i
fi
if [ -d "${GIT_REPOSITORY}" ]; then
echo "Ansible run"
(cd "${GIT_REPOSITORY}"; ansible-playbook -i "${ANSIBLE_HOSTS}" "${ANSIBLE_PLAYBOOK}" -u "${ANSIBLE_SSH_USER}")
fi
if [ -f "${ANSIBLE_VAULT_FILE}" ]; then
shred "${ANSIBLE_VAULT_FILE}"
fi
if [ -d "${GIT_REPOSITORY}" ]; then
srm -r -l "${GIT_REPOSITORY}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment