library/roles/openvpn: Installs and configure a openvpn service.

This commit is contained in:
Andrea Dell'Amico 2016-06-11 16:57:29 +02:00
parent b465587c3c
commit 52fc34bd95
8 changed files with 337 additions and 0 deletions

49
openvpn/defaults/main.yml Normal file
View File

@ -0,0 +1,49 @@
---
openvpn_enabled: True
openvpn_enable_system_forward: True
openvpn_pkg_state: latest
openvpn_pkgs:
- openvpn
openvpn_radius_auth: False
openvpn_radius_pkg:
- openvpn-auth-radius
openvpn_ldap_auth: False
openvpn_ldap_pkg:
- openvpn-auth-ldap
openvpn_conf_dir: /etc/openvpn
openvpn_conf_name: openvpn.conf
openvpn_mode: server
openvpn_dev: tun
openvpn_port: 1194
openvpn_protocol: udp
openvpn_server_net: '192.168.254.0 255.255.255.0'
openvpn_push_routes:
- '192.168.253.0 255.255.255.0'
openvpn_tls_server: True
openvpn_dh: /etc/openvpn/dh2048.pem
openvpn_tls_auth: '/etc/openvpn/ta.key 0'
openvpn_install_alternative_ca: False
openvpn_alternative_ca_name: ca.pem
openvpn_ca: '/var/lib/acme/live/{{ ansible_fqdn }}/chain'
openvpn_cert: '/var/lib/acme/live/{{ ansible_fqdn }}/cert'
openvpn_key: '/var/lib/acme/live/{{ ansible_fqdn }}/privkey'
openvpn_compression_enabled: False
openvpn_keepalive: '10 120'
openvpn_cert_auth_enabled: True
openvpn_username_pam_auth: False
openvpn_max_clients: 50
openvpn_run_unprivileged: True
openvpn_unprivileged_user: nobody
openvpn_unprivileged_group: nogroup
openvpn_letsencrypt_managed: True
openvpn_verbosity_log: 3
openvpn_mute_after: 20

101
openvpn/files/ca.pem Normal file
View File

@ -0,0 +1,101 @@
-----BEGIN CERTIFICATE-----
MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/
MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT
DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDT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X4+U9m5/wAl0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNV
HQ8BAf8EBAMCAQYwHQYDVR0OBBYEFMSnsaR7LHH62+FLkHX/xBVghYkQMA0GCSqG
SIb3DQEBBQUAA4IBAQCjGiybFwBcqR7uKGY3Or+Dxz9LwwmglSBd49lZRNI+DT69
ikugdB/OEIKcdBodfpga3csTS7MgROSR6cz8faXbauX+5v3gTt23ADq1cEmv8uXr
AvHRAosZy5Q6XkjEGB5YGV8eAlrwDPGxrancWYaLbumR9YbK+rlmM6pZW87ipxZz
R8srzJmwN0jP41ZL9c8PDHIyh8bwRLtTcm1D9SZImlJnt1ir/md2cXjbDaJWFBM5
JDGFoqgCWjBH4d1QB7wCCZAA62RjYJsWvIjJEubSfZGL+T0yjWW06XyxV3bqxbYo
Ob8VZRzI9neWagqNdwvYkQsEjgfbKbYK7p2CNTUQ
-----END CERTIFICATE-----

View File

@ -0,0 +1,27 @@
#!/bin/bash
LE_SERVICES_SCRIPT_DIR=/usr/lib/acme/hooks
LE_CERTS_DIR=/var/lib/acme/live/$HOSTNAME
LE_LOG_DIR=/var/log/letsencrypt
DATE=$( date )
[ ! -d $LE_LOG_DIR ] && mkdir $LE_LOG_DIR
echo "$DATE" >> $LE_LOG_DIR/openvpn.log
if [ -f /etc/default/letsencrypt ] ; then
. /etc/default/letsencrypt
else
echo "No letsencrypt default file" >> $LE_LOG_DIR/openvpn.log
fi
echo "Reload the openvpn service" >> $LE_LOG_DIR/openvpn.log
if [ -x /bin/systemctl ] ; then
systemctl reload openvpn >> $LE_LOG_DIR/openvpn.log 2>&1
else
service openvpn reload >> $LE_LOG_DIR/openvpn.log 2>&1
fi
echo "Done." >> $LE_LOG_DIR/openvpn.log
exit 0

View File

@ -0,0 +1,5 @@
---
- name: Reload OpenVPN
service: name=openvpn state=reloaded
when: openvpn_enabled

View File

@ -0,0 +1,14 @@
---
- name: Create the acme hooks directory if it does not yet exist
file: dest={{ letsencrypt_acme_services_scripts_dir }} state=directory owner=root group=root
when:
- openvpn_letsencrypt_managed
- letsencrypt_acme_install
tags: [ 'openvpn', 'letsencrypt' ]
- name: Install a script that fix the letsencrypt certificate for openvpn and then reload the service
copy: src=openvpn-letsencrypt-acme.sh dest={{ letsencrypt_acme_services_scripts_dir }}/openvpn owner=root group=root mode=4555
when:
- openvpn_letsencrypt_managed
- letsencrypt_acme_install
tags: [ 'openvpn', 'letsencrypt' ]

4
openvpn/tasks/main.yml Normal file
View File

@ -0,0 +1,4 @@
---
- include: openvpn.yml
- include: letsencrypt-openvpn.yml
when: openvpn_letsencrypt_managed

84
openvpn/tasks/openvpn.yml Normal file
View File

@ -0,0 +1,84 @@
---
- name: Install the OpenVPN main packages
apt: pkg={{ item }} state={{ openvpn_pkg_state }} update_cache=yes
with_items: '{{ openvpn_pkgs }}'
tags: openvpn
- name: Install the OpenVPN radius auth plugin package
apt: pkg={{ item }} state={{ openvpn_pkg_state }}
with_items: '{{ openvpn_radius_pkg }}'
when: openvpn_radius_auth
tags: openvpn
- name: Install the OpenVPN ldap auth plugin package
apt: pkg={{ item }} state={{ openvpn_pkg_state }}
with_items: '{{ openvpn_ldap_pkg }}'
when: openvpn_ldap_auth
tags: openvpn
- name: Install the OpenVPN PAM auth plugin
shell: cp /usr/lib/openvpn/openvpn-plugin-auth-pam.so {{ openvpn_conf_dir }}/openvpn-plugin-auth-pam.so
args:
creates: '{{ openvpn_conf_dir }}/openvpn-plugin-auth-pam.so'
when: openvpn_username_pam_auth
tags: openvpn
- name: Remove the OpenVPN PSM auth plugin
file: dest={{ openvpn_conf_dir }}/openvpn-plugin-auth-pam.so state=absent
when: not openvpn_username_pam_auth
tags: openvpn
- name: Create the ipp and status subdirs
file: dest={{ openvpn_conf_dir }}/{{ item }} state=directory
with_items:
- ipp
- status
tags: openvpn
- name: Install the main OpenVPN configuration file
template: src=openvpn.conf.j2 dest={{ openvpn_conf_dir }}/{{ openvpn_conf_name }} owner=root group=root mode=0444
notify: Reload OpenVPN
tags: openvpn
- name: Create the dh file
shell: openssl dhparam -out {{ openvpn_conf_dir }}/dh2048.pem 2048 ; chmod 444 {{ openvpn_conf_dir }}/dh2048.pem
args:
creates: '{{ openvpn_conf_dir }}/dh2048.pem'
tags: openvpn
- name: Create the ta key
shell: cd {{ openvpn_conf_dir }} ; openvpn --genkey --secret ta.key ; chmod 400 {{ openvpn_conf_dir }}/ta.key
args:
creates: '{{ openvpn_conf_dir }}/ta.key'
tags: openvpn
- name: Install the alternate CA file
copy: src=ca.pem dest={{ openvpn_conf_dir }}/{{ openvpn_alternative_ca_name }}
when: openvpn_install_alternative_ca
tags: openvpn
- name: Ensure that the OpenVPN service is enabled and running
service: name=openvpn state=started enabled=yes
when: openvpn_enabled
tags: openvpn
- name: Ensure that the OpenVPN service is stopped and disabled
service: name=openvpn state=stopped enabled=no
when: not openvpn_enabled
tags: openvpn
- name: Enable kernel forwarding
sysctl: name={{ item }} value=1 reload=yes state=present
with_items:
- net.ipv4.ip_forward
# - net.ipv6.conf.all.forwarding
when: openvpn_enable_system_forward
tags: openvpn
- name: Disable kernel forwarding
sysctl: name={{ item }} value=0 reload=yes state=present
with_items:
- net.ipv4.ip_forward
# - net.ipv6.conf.all.forwarding
when: not openvpn_enable_system_forward
tags: openvpn

View File

@ -0,0 +1,53 @@
mode {{ openvpn_mode }}
dev {{ openvpn_dev }}
server {{ openvpn_server_net }}
ifconfig-pool-persist ipp/ipp.txt
{% for route in openvpn_push_routes %}
push "route {{ route }}"
{% endfor %}
port {{ openvpn_port }}
proto {{ openvpn_protocol }}
{% if openvpn_tls_server %}
tls-server
{% endif %}
dh {{ openvpn_dh }}
ca {{ openvpn_ca }}
cert {{ openvpn_cert }}
key {{ openvpn_key }}
tls-auth {{ openvpn_tls_auth }}
{% if openvpn_compression_enabled %}
comp-lzo
{% endif %}
keepalive {{ openvpn_keepalive }}
{% if not openvpn_cert_auth_enabled %}
# Disable cert-auth
client-cert-not-required
{% endif %}
{% if openvpn_username_pam_auth %}
username-as-common-name
# PAM login
plugin /etc/openvpn/openvpn-plugin-auth-pam.so login
{% endif %}
max-clients {{ openvpn_max_clients }}
persist-tun
persist-key
status status/openvpn-status.log
{% if openvpn_run_unprivileged %}
user {{ openvpn_unprivileged_user }}
group {{ openvpn_unprivileged_group }}
{% endif %}
verb {{ openvpn_verbosity_log }}
mute {{ openvpn_mute_after }}