Provision a server by Ansible #9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Provision a server by Ansible | |
on: | |
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#onworkflow_dispatch | |
workflow_dispatch: | |
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#permissions | |
permissions: | |
contents: read # for "git clone" | |
defaults: | |
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#defaultsrun | |
run: | |
# Enable fail-fast behavior using set -eo pipefail | |
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#exit-codes-and-error-action-preference | |
shell: bash | |
jobs: | |
setup-server: | |
name: Provision a server | |
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idruns-on | |
runs-on: ubuntu-20.04 | |
steps: | |
- name: Clone source code | |
uses: actions/[email protected] # https://github.com/actions/checkout | |
with: | |
# Whether to configure the token or SSH key with the local git config. Default: true | |
persist-credentials: false | |
- name: Install ansible | |
# The command pip3 install --user ansible==2.10.17 doesn't work as we have an old version | |
# See https://docs.ansible.com/ansible/2.10/installation_guide/intro_installation.html#installing-devel-from-github-with-pip | |
run: python3 -m pip install --user https://github.com/ansible/ansible/archive/refs/tags/v2.10.17.tar.gz | |
- name: Show ansible version | |
run: ansible --version | |
- name: Decrypt ansible files | |
working-directory: infra/vagrant | |
env: | |
# https://docs.github.com/en/actions/security-guides/encrypted-secrets#using-encrypted-secrets-in-a-workflow | |
VAULT_PASSWORD: ${{ secrets.VAULT_PASSWORD }} | |
run: | | |
printf '%s' "$VAULT_PASSWORD" >vault-pass.txt | |
for FILENAME in provisioning/vars/prod.yml provisioning/coder_rsa; do | |
echo "Decrypting ${FILENAME}.enc to $FILENAME" | |
ansible-vault decrypt \ | |
--vault-password-file vault-pass.txt \ | |
--output "$FILENAME" \ | |
"${FILENAME}.enc" | |
done | |
- name: Install required collections | |
working-directory: infra/vagrant | |
run: ansible-galaxy role install --role-file requirements.yml --roles-path provisioning/roles | |
- name: Run ansible in syntax check mode | |
working-directory: infra/vagrant | |
run: ansible-playbook provisioning/prod.yml -i provisioning/prod.inventory --syntax-check | |
- name: Run ansible | |
working-directory: infra/vagrant | |
env: | |
# Disable host key checking to suppress interactive prompt. | |
# See: https://docs.ansible.com/ansible/2.10/user_guide/connection_details.html#managing-host-key-checking | |
ANSIBLE_HOST_KEY_CHECKING: 'False' | |
# See: https://docs.ansible.com/ansible/2.10/reference_appendices/config.html#envvar-ANSIBLE_PRIVATE_KEY_FILE | |
ANSIBLE_PRIVATE_KEY_FILE: 'provisioning/coder_rsa' | |
run: ansible-playbook provisioning/prod.yml -i provisioning/prod.inventory | |
- name: Cleanup | |
if: always() | |
working-directory: infra/vagrant | |
run: | | |
for FILE in vault-pass.txt provisioning/vars/prod.yml provisioning/coder_rsa; do | |
[ ! -f "$FILE" ] || rm -fv "$FILE" | |
done |