Provision a server by Ansible #10
This file contains hidden or 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 | |
# NOTE: during version bump don't forget to update in other places: deploy.yml and provisioning-by-terraform.yml | |
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/ansible | |
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 vars/prod.yml 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/ansible | |
run: ansible-galaxy role install --role-file requirements.galaxy.yml --roles-path roles | |
- name: Run ansible in syntax check mode | |
working-directory: infra/ansible | |
run: ansible-playbook prod.yml -i prod.inventory --syntax-check | |
- name: Run ansible | |
working-directory: infra/ansible | |
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: 'coder_rsa' | |
run: ansible-playbook prod.yml -i prod.inventory | |
- name: Cleanup | |
if: always() | |
working-directory: infra/ansible | |
run: | | |
for FILE in vault-pass.txt vars/prod.yml coder_rsa; do | |
[ ! -f "$FILE" ] || rm -fv "$FILE" | |
done |