restart-vpn.yml 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. ---
  2. - hosts: 10.0.0.2
  3. become: true
  4. gather_facts: false
  5. vars_files:
  6. - "{{ inventory_dir }}/vars/vault.yaml"
  7. vars:
  8. openvpn_service: "openvpn@nord"
  9. openvpn_remotes: "{{ vaulted_openvpn_remotes }}"
  10. openvpn_user: "{{ vaulted_openvpn_user }}"
  11. openvpn_pass: "{{ vaulted_openvpn_pass }}"
  12. tasks:
  13. - name: Select random NordVPN Endpoint
  14. ansible.builtin.set_fact:
  15. nord_endpoint: "{{ vaulted_openvpn_remotes | random }}"
  16. - name: Set config from chosen endpoint "{{ nord_endpoint.cn }}"
  17. ansible.builtin.set_fact:
  18. nord_remote: "{{ nord_endpoint.remote }}"
  19. nord_cn: "{{ nord_endpoint.cn }}"
  20. - name: Update config file settings
  21. ansible.builtin.lineinfile:
  22. path: /etc/openvpn/nord.conf
  23. regexp: "{{ item.regexp }}"
  24. line: "{{ item.line }}"
  25. loop:
  26. - { "label": "remote", "regexp": "^remote ", "line": "remote {{ nord_remote }} 443" }
  27. - { "label": "cn", "regexp": "^verify-x509-name", "line": "verify-x509-name CN={{ nord_cn }}" }
  28. loop_control:
  29. label: "{{ item.label }}"
  30. - name: Update motd script with new endpoint
  31. ansible.builtin.lineinfile:
  32. path: /usr/local/bin/vpn-endpoint
  33. regexp: "^endpoint="
  34. line: "endpoint={{ nord_cn }}"
  35. - name: Bounce {{ openvpn_service }} service
  36. ansible.builtin.systemd_service:
  37. name: "{{ openvpn_service }}"
  38. state: restarted
  39. - name: Bounce unbound service
  40. ansible.builtin.systemd_service:
  41. name: unbound
  42. state: restarted