Skip to content

Commit

Permalink
#14 ansibleise Add-vpn-user
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsimpson committed Oct 21, 2024
1 parent 72b4562 commit 4974f00
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 58 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/add-vpn-user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ name: Add VPN User
on:
workflow_dispatch:
inputs:
ansible_vault_password:
description: 'ansible_vault_password'
required: true

PSONO_SECRET_ID:
description: 'PSONO_SECRET_ID'
required: true
Expand All @@ -32,7 +28,7 @@ jobs:
echo Creating ANSIBLE_VAULT_PASSWORD_FILE
TMPFILE=$(mktemp)
trap "rm -f ${TMPFILE@Q}" EXIT
echo -n "${{ inputs.ansible_vault_password }}" > $TMPFILE
echo -n "${{ secrets.ANSIBLE_VAULT_PASSWORD }}" > $TMPFILE
export ANSIBLE_VAULT_PASSWORD_FILE=$TMPFILE
echo About to run playbook add-vpn-user.yml
Expand All @@ -44,7 +40,7 @@ jobs:
# the api token is used on other host groups.
# The dynamic vpn hosts inventory is using the dynamic inventory file inventory-vpn-servers-hcloud.yml
PSONO_SECRET_ID=${{ inputs.PSONO_SECRET_ID }} ansible-playbook --extra-vars _vault_hetzner_cloud_token=$(ANSIBLE_LOAD_CALLBACK_PLUGINS=1 ANSIBLE_STDOUT_CALLBACK=ansible.posix.json ansible localhost -i inventory.ini -m debug -a "msg={{ hostvars[inventory_hostname].hetzner_hcloud_token }}" | jq '.plays[0]["tasks"][0]["hosts"]["localhost"]["msg"]') -i inventory-vpn-servers-hcloud.yml playbooks/add-vpn-user.yml
ansible-playbook --extra_vars "PSONO_SECRET_ID=${{ inputs.PSONO_SECRET_ID }} _vault_hetzner_cloud_token=$(ANSIBLE_LOAD_CALLBACK_PLUGINS=1 ANSIBLE_STDOUT_CALLBACK=ansible.posix.json ansible localhost -i inventory.ini -m debug -a "msg={{ hostvars[inventory_hostname].hetzner_hcloud_token }}" | jq '.plays[0]["tasks"][0]["hosts"]["localhost"]["msg"]')" -i inventory-vpn-servers-hcloud.yml playbooks/add-vpn-user.yml
rm $TMPFILE
# Enable tmate debugging of manually-triggered workflows if the input option was provided
Expand Down
68 changes: 16 additions & 52 deletions src/vpn/playbooks/add-vpn-user.yml
Original file line number Diff line number Diff line change
@@ -1,52 +1,16 @@
name: Add VPN User

on:
workflow_dispatch:
inputs:
ansible_vault_password:
description: 'ansible_vault_password'
required: true

PSONO_SECRET_ID:
description: 'PSONO_SECRET_ID'
required: true

jobs:
generate-user-vpn-config:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup ansible
run: |
pip3 install ansible
ansible-galaxy collection install --requirements-file ./src/vpn/requirements.yml
- name: Run ansible
run: |
cd src/vpn
echo Creating ANSIBLE_VAULT_PASSWORD_FILE
TMPFILE=$(mktemp)
trap "rm -f ${TMPFILE@Q}" EXIT
echo -n "${{ inputs.ansible_vault_password }}" > $TMPFILE
export ANSIBLE_VAULT_PASSWORD_FILE=$TMPFILE
echo About to run playbook add-vpn-user.yml
# Bend over backwards to keep storing secrets in ansible vault rather than a specific CI/CD runner.
# note this uses dynamic inventory. Since you can't set/get group_vars from a dynamic inventory,
# this is an 'inventive' way using 'localhost' group_vars to the vpn hosts (in this case hetzer VM(s))
# api token out of ansible vault. The api token is stord in group_vars/localhost.yml (where in fact)
# the api token is used on other host groups.
# The dynamic vpn hosts inventory is using the dynamic inventory file inventory-vpn-servers-hcloud.yml
PSONO_SECRET_ID=${{ inputs.PSONO_SECRET_ID }} ansible-playbook --extra-vars _vault_hetzner_cloud_token=$(ANSIBLE_LOAD_CALLBACK_PLUGINS=1 ANSIBLE_STDOUT_CALLBACK=ansible.posix.json ansible localhost -i inventory.ini -m debug -a "msg={{ hostvars[inventory_hostname].hetzner_hcloud_token }}" | jq '.plays[0]["tasks"][0]["hosts"]["localhost"]["msg"]') -i inventory-vpn-servers-hcloud.yml playbooks/add-vpn-user.yml
rm $TMPFILE
# Enable tmate debugging of manually-triggered workflows if the input option was provided
- name: Setup tmate session
uses: mxschmitt/action-tmate@v3
if: ${{ failure() }}
---
- name: Add VPN user
hosts: "{{ vpn_servers | default('vpn_servers') }}"
gather_facts: false
become: yes
vars:
wireguard_dir: "/etc/wireguard/"

tasks:

- name: Generate new vpn peer config & Add save client config to password manager
ansible.builtin.shell: |
PSONO_CI_API_KEY_ID={{ PSONO_CI_API_KEY_ID }} PSONO_CI_API_SECRET_KEY_HEX={{ PSONO_CI_API_SECRET_KEY_HEX }} PSONO_CI_SERVER_URL={{ PSONO_CI_SERVER_URL }} PSONO_SECRET_ID={{ PSONO_SECRET_ID }} ./add-vpn-user.sh
exit 0
args:
chdir: "{{ wireguard_dir }}"

0 comments on commit 4974f00

Please sign in to comment.