No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

openvpn.yml 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. ---
  2. # Installs the OpenVPN virtual private network server.
  3. # ref: https://library.linode.com/networking/openvpn/debian-6-squeeze
  4. - name: Install OpenVPN and dependencies from apt
  5. apt: pkg={{ item }} state=installed
  6. with_items:
  7. - openvpn
  8. - udev
  9. - dnsmasq
  10. - name: Generate RSA keys for the CA and Server
  11. command: openssl genrsa -out {{ item }}.key {{ openvpn_key_size }}
  12. chdir={{ openvpn_path }}
  13. creates={{ item }}.key
  14. with_items:
  15. - ca
  16. - server
  17. - name: Generate RSA keys for the clients
  18. command: openssl genrsa -out {{ item }}.key {{ openvpn_key_size }}
  19. chdir={{ openvpn_path }}
  20. creates={{ item }}.key
  21. with_items: openvpn_clients
  22. - name: Set the proper permissions on all RSA keys
  23. file: path={{ openvpn_path }}
  24. recurse=yes
  25. state=directory
  26. owner=root
  27. group=root
  28. mode=600
  29. - name: Generate CA certificate
  30. command: openssl req -nodes -batch -new -x509 -key {{ openvpn_ca }}.key -out {{ openvpn_ca }}.crt -days {{ openvpn_days_valid }} -subj "{{ openssl_request_subject }}/CN=ca-certificate"
  31. creates={{ openvpn_ca }}.crt
  32. - name: Generate the OpenSSL configuration that will be used for the Server certificate's req and ca commands
  33. # Properly sets the attributes that are described here:
  34. # openvpn.net/index.php/open-source/documentation/howto.html#mitm
  35. #
  36. # This is required in order for the 'ns-cert-type server' option to
  37. # work, which is enabled by default in most standard client.conf
  38. # files.
  39. template: src=openssl-server-certificate.cnf.j2
  40. dest={{ openvpn_path }}/openssl-server-certificate.cnf
  41. - name: Seed a blank database file that will be used when generating the Server's certificate
  42. command: touch {{ openvpn_path }}/index.txt
  43. creates={{ openvpn_path }}/index.txt
  44. - name: Seed a serial file that will be used when generating the Server's certificate
  45. shell: echo 01 > {{ openvpn_path }}/serial
  46. creates={{ openvpn_path }}/serial
  47. - name: Generate CSR for the Server
  48. command: openssl req -batch -extensions server -new -key server.key -out server.csr -config {{ openvpn_path }}/openssl-server-certificate.cnf
  49. chdir={{ openvpn_path }}
  50. creates=server.csr
  51. - name: Generate certificate for the Server
  52. command: openssl ca -batch -extensions server -in server.csr -out server.crt -config openssl-server-certificate.cnf
  53. chdir={{ openvpn_path }}
  54. creates=server.crt
  55. - name: Generate CSRs for the clients
  56. command: openssl req -new -key {{ item }}.key -out {{ item }}.csr -subj "{{ openssl_request_subject }}/CN={{ item }}"
  57. chdir={{ openvpn_path }}
  58. creates={{ item }}.csr
  59. with_items: openvpn_clients
  60. - name: Generate certificates for the clients
  61. command: openssl x509 -CA {{ openvpn_ca }}.crt -CAkey {{ openvpn_ca }}.key -CAcreateserial -req -days {{ openvpn_days_valid }} -in {{ item }}.csr -out {{ item }}.crt
  62. chdir={{ openvpn_path }}
  63. creates={{ item }}.crt
  64. with_items: openvpn_clients
  65. - name: Generate HMAC firewall key
  66. command: openvpn --genkey --secret {{ openvpn_hmac_firewall }}
  67. creates={{ openvpn_hmac_firewall }}
  68. - name: Generate Diffie–Hellman parameters (this will take a while)
  69. command: openssl dhparam -out {{ openvpn_dhparam }} {{ openvpn_key_size }}
  70. creates={{ openvpn_dhparam }}
  71. - name: Copy rc.local with firewall and dnsmasq rules into place
  72. copy: src=etc_rc.local dest=/etc/rc.local
  73. - name: Enable IPv4 traffic forwarding
  74. lineinfile: dest=/etc/sysctl.conf
  75. regexp="^#?net.ipv4.ip_forward"
  76. line="net.ipv4.ip_forward=1"
  77. - shell: echo 1 > /proc/sys/net/ipv4/ip_forward
  78. - name: Allow OpenVPN through the firewall
  79. command: "{{ item }}"
  80. with_items:
  81. - iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
  82. - iptables -A FORWARD -s 10.8.0.0/24 -j ACCEPT
  83. - iptables -A FORWARD -j REJECT
  84. - iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
  85. - name: Copy OpenVPN configuration file into place
  86. template: src=etc_openvpn_server.conf.j2 dest=/etc/openvpn/server.conf
  87. notify: restart openvpn
  88. # OpenVPN must restart first so the 10.8.0.0 interface is available
  89. # to dnsmasq
  90. - meta: flush_handlers
  91. - name: Copy dnsmasq configuration file into place
  92. copy: src=etc_dnsmasq.conf dest=/etc/dnsmasq.conf
  93. notify: restart dnsmasq
  94. - name: Retrieve the ca.crt and ta.key files that clients will need in order to connect to the OpenVPN server
  95. fetch: src={{ openvpn_path }}/{{ item }}
  96. dest=/tmp/sovereign-openvpn-files
  97. with_items:
  98. - ca.crt
  99. - ta.key
  100. - name: Retrieve the certificates that clients will need in order to connect to the OpenVPN server
  101. fetch: src={{ openvpn_path }}/{{ item }}.crt
  102. dest=/tmp/sovereign-openvpn-files
  103. with_items: openvpn_clients
  104. - name: Retrieve the keys that clients will need in order to connect to the OpenVPN server
  105. fetch: src={{ openvpn_path }}/{{ item }}.key
  106. dest=/tmp/sovereign-openvpn-files
  107. with_items: openvpn_clients
  108. - pause: prompt="You are ready to set up your OpenVPN clients. The files that you need are in /tmp/sovereign-openvpn-files. Make sure LZO compression is enabled and that you provide the ta.key file for the TLS-Auth option with a direction of '1'. Press any key to continue..."