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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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: Create directories for clients
  18. file: path={{ openvpn_path}}/{{ item }} state=directory
  19. with_items: openvpn_clients
  20. - name: Generate RSA keys for the clients
  21. command: openssl genrsa -out client.key {{ openvpn_key_size }}
  22. chdir={{ openvpn_path }}/{{ item }}
  23. creates=client.key
  24. with_items: openvpn_clients
  25. - name: Set the proper permissions on all RSA keys
  26. file: path={{ openvpn_path }}
  27. recurse=yes
  28. state=directory
  29. owner=root
  30. group=root
  31. mode=600
  32. - name: Generate CA certificate
  33. 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"
  34. creates={{ openvpn_ca }}.crt
  35. - name: Generate the OpenSSL configuration that will be used for the Server certificate's req and ca commands
  36. # Properly sets the attributes that are described here:
  37. # openvpn.net/index.php/open-source/documentation/howto.html#mitm
  38. #
  39. # This is required in order for the 'ns-cert-type server' option to
  40. # work, which is enabled by default in most standard client.conf
  41. # files.
  42. template: src=openssl-server-certificate.cnf.j2
  43. dest={{ openvpn_path }}/openssl-server-certificate.cnf
  44. - name: Seed a blank database file that will be used when generating the Server's certificate
  45. command: touch {{ openvpn_path }}/index.txt
  46. creates={{ openvpn_path }}/index.txt
  47. - name: Seed a serial file that will be used when generating the Server's certificate
  48. shell: echo 01 > {{ openvpn_path }}/serial
  49. creates={{ openvpn_path }}/serial
  50. - name: Generate CSR for the Server
  51. command: openssl req -batch -extensions server -new -key server.key -out server.csr -config {{ openvpn_path }}/openssl-server-certificate.cnf
  52. chdir={{ openvpn_path }}
  53. creates=server.csr
  54. - name: Generate certificate for the Server
  55. command: openssl ca -batch -extensions server -in server.csr -out server.crt -config openssl-server-certificate.cnf
  56. chdir={{ openvpn_path }}
  57. creates=server.crt
  58. - name: Generate CSRs for the clients
  59. command: openssl req -new -key client.key -out client.csr -subj "{{ openssl_request_subject }}/CN={{ item }}"
  60. chdir={{ openvpn_path }}/{{ item }}
  61. creates=client.csr
  62. with_items: openvpn_clients
  63. - name: Generate certificates for the clients
  64. command: openssl x509 -CA {{ openvpn_ca }}.crt -CAkey {{ openvpn_ca }}.key -CAcreateserial -req -days {{ openvpn_days_valid }} -in client.csr -out client.crt
  65. chdir={{ openvpn_path }}/{{ item }}
  66. creates=client.crt
  67. with_items: openvpn_clients
  68. - name: Create the client configs
  69. template: src=client.cnf.j2
  70. dest={{ openvpn_path }}/{{ item }}/{{ openvpn_server }}.ovpn
  71. with_items: openvpn_clients
  72. - name: Generate HMAC firewall key
  73. command: openvpn --genkey --secret {{ openvpn_hmac_firewall }}
  74. creates={{ openvpn_hmac_firewall }}
  75. - name: Generate Diffie-Hellman parameters (this will take a while)
  76. command: openssl dhparam -out {{ openvpn_dhparam }} {{ openvpn_key_size }}
  77. creates={{ openvpn_dhparam }}
  78. - name: Copy rc.local with firewall and dnsmasq rules into place
  79. template: src=etc_rc.local dest=/etc/rc.local
  80. - name: Enable IPv4 traffic forwarding
  81. sysctl: name=net.ipv4.ip_forward value=1
  82. - name: Allow OpenVPN through the firewall
  83. command: "{{ item }}"
  84. with_items:
  85. - iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
  86. - iptables -A FORWARD -s 10.8.0.0/24 -j ACCEPT
  87. - iptables -A FORWARD -j REJECT
  88. - iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o {{ ansible_default_ipv4.interface }} -j MASQUERADE
  89. - name: Copy OpenVPN configuration file into place
  90. template: src=etc_openvpn_server.conf.j2 dest=/etc/openvpn/server.conf
  91. notify: restart openvpn
  92. # OpenVPN must restart first so the 10.8.0.0 interface is available
  93. # to dnsmasq
  94. - meta: flush_handlers
  95. - name: Copy dnsmasq configuration file into place
  96. copy: src=etc_dnsmasq.conf dest=/etc/dnsmasq.conf
  97. notify: restart dnsmasq
  98. - name: Copy the ca.crt and ta.key files that clients will need in order to connect to the OpenVPN server
  99. command: cp {{ openvpn_path }}/{{ item[1] }} {{ openvpn_path }}/{{ item[0] }}
  100. with_nested:
  101. - openvpn_clients
  102. - ["ca.crt", "ta.key"]
  103. - name: Retrieve the files that clients will need in order to connect to the OpenVPN server
  104. fetch: src={{ openvpn_path }}/{{ item[0] }}/{{ item[1] }}
  105. dest=/tmp/sovereign-openvpn-files
  106. with_nested:
  107. - openvpn_clients
  108. - ["client.crt", "client.key", "ca.crt", "ta.key", "{{ openvpn_server }}.ovpn"]
  109. - pause: seconds=5
  110. 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..."