2016-07-27 21:27:11 +00:00
|
|
|
---
|
|
|
|
|
2016-08-11 20:54:29 +00:00
|
|
|
- hosts: localhost
|
|
|
|
gather_facts: False
|
|
|
|
vars_files:
|
2016-08-16 03:32:44 +00:00
|
|
|
- config.cfg
|
2016-12-15 10:33:29 +00:00
|
|
|
|
2016-08-11 20:54:29 +00:00
|
|
|
tasks:
|
|
|
|
- name: Add the server to the vpn-host group
|
2016-08-16 03:32:44 +00:00
|
|
|
add_host:
|
2016-08-11 20:54:29 +00:00
|
|
|
hostname: "{{ server_ip }}"
|
|
|
|
groupname: vpn-host
|
|
|
|
ansible_ssh_user: "{{ server_user }}"
|
|
|
|
ansible_python_interpreter: "/usr/bin/python2.7"
|
2016-08-25 20:30:27 +00:00
|
|
|
ssh_tunneling_enabled: "{{ ssh_tunneling_enabled }}"
|
2016-12-15 10:33:29 +00:00
|
|
|
easyrsa_CA_password: "{{ easyrsa_CA_password }}"
|
2016-08-18 18:49:20 +00:00
|
|
|
IP_subject: "{{ IP_subject }}"
|
2017-02-03 19:24:02 +00:00
|
|
|
ansible_ssh_private_key_file: "{{ SSH_keys.private }}"
|
2016-08-11 20:54:29 +00:00
|
|
|
|
2016-12-13 18:58:45 +00:00
|
|
|
- name: Wait until SSH becomes ready...
|
|
|
|
local_action:
|
|
|
|
module: wait_for
|
|
|
|
port: 22
|
|
|
|
host: "{{ server_ip }}"
|
|
|
|
search_regex: "OpenSSH"
|
|
|
|
delay: 10
|
|
|
|
timeout: 320
|
|
|
|
state: present
|
2016-08-16 03:32:44 +00:00
|
|
|
become: false
|
2016-08-11 20:54:29 +00:00
|
|
|
|
2016-07-30 17:26:30 +00:00
|
|
|
- name: User management
|
2016-08-11 20:54:29 +00:00
|
|
|
hosts: vpn-host
|
2017-02-03 19:24:02 +00:00
|
|
|
gather_facts: true
|
2016-07-30 16:05:04 +00:00
|
|
|
become: true
|
2016-07-27 21:27:11 +00:00
|
|
|
vars_files:
|
2016-08-16 03:32:44 +00:00
|
|
|
- config.cfg
|
2016-08-25 20:59:16 +00:00
|
|
|
|
2016-08-18 18:49:20 +00:00
|
|
|
pre_tasks:
|
2017-03-18 09:22:07 +00:00
|
|
|
- name: Common pre-tasks
|
|
|
|
include: playbooks/common.yml
|
|
|
|
|
2016-08-18 18:49:20 +00:00
|
|
|
- set_fact:
|
|
|
|
IP_subject_alt_name: "{{ IP_subject }}"
|
2017-03-05 07:58:42 +00:00
|
|
|
easyrsa_p12_export_password: "{{ p12_export_password|default((ansible_date_time.iso8601_basic|sha1|to_uuid).split('-')[0]) }}"
|
2016-08-16 03:32:44 +00:00
|
|
|
|
2016-10-06 17:39:53 +00:00
|
|
|
roles:
|
|
|
|
- { role: ssh_tunneling, tags: [ 'ssh_tunneling' ], when: ssh_tunneling_enabled is defined and ssh_tunneling_enabled == "y" }
|
|
|
|
|
2016-07-27 21:27:11 +00:00
|
|
|
tasks:
|
2016-12-14 15:49:47 +00:00
|
|
|
|
|
|
|
- name: Gather Facts
|
|
|
|
setup:
|
|
|
|
|
2017-02-03 19:24:02 +00:00
|
|
|
- name: Cheking the signature algorithm
|
|
|
|
local_action: >
|
|
|
|
shell openssl x509 -text -in certs/{{ IP_subject_alt_name }}.crt | grep 'Signature Algorithm' | head -n1
|
|
|
|
become: no
|
|
|
|
register: sig_algo
|
|
|
|
args:
|
|
|
|
chdir: "configs/{{ IP_subject_alt_name }}/pki/"
|
|
|
|
|
|
|
|
- name: Change the algorithm to RSA
|
|
|
|
set_fact:
|
|
|
|
algo_params: "rsa:2048"
|
|
|
|
when: '"ecdsa" not in sig_algo.stdout'
|
2016-12-14 15:49:47 +00:00
|
|
|
|
2016-10-06 17:39:53 +00:00
|
|
|
- name: Build the client's pair
|
2017-02-03 19:24:02 +00:00
|
|
|
local_action: >
|
|
|
|
shell openssl req -utf8 -new -newkey {{ algo_params | default('ec:ecparams/prime256v1.pem') }} -config openssl.cnf -keyout private/{{ item }}.key -out reqs/{{ item }}.req -nodes -passin pass:"{{ easyrsa_CA_password }}" -subj "/CN={{ item }}" -batch &&
|
|
|
|
openssl ca -utf8 -in reqs/{{ item }}.req -out certs/{{ item }}.crt -config openssl.cnf -days 3650 -batch -passin pass:"{{ easyrsa_CA_password }}" -subj "/CN={{ item }}" &&
|
|
|
|
touch certs/{{ item }}_crt_generated
|
|
|
|
become: no
|
2016-10-06 17:39:53 +00:00
|
|
|
args:
|
2017-02-03 19:24:02 +00:00
|
|
|
chdir: "configs/{{ IP_subject_alt_name }}/pki/"
|
|
|
|
creates: certs/{{ item }}_crt_generated
|
|
|
|
environment:
|
|
|
|
subjectAltName: "DNS:{{ item }}"
|
2016-10-06 17:39:53 +00:00
|
|
|
with_items: "{{ users }}"
|
|
|
|
|
|
|
|
- name: Build the client's p12
|
2017-02-03 19:24:02 +00:00
|
|
|
local_action: >
|
|
|
|
shell openssl pkcs12 -in certs/{{ item }}.crt -inkey private/{{ item }}.key -export -name {{ item }} -out private/{{ item }}.p12 -certfile cacert.pem -passout pass:"{{ easyrsa_p12_export_password }}"
|
|
|
|
become: no
|
2016-10-06 17:39:53 +00:00
|
|
|
args:
|
2017-02-03 19:24:02 +00:00
|
|
|
chdir: "configs/{{ IP_subject_alt_name }}/pki/"
|
2016-10-06 17:39:53 +00:00
|
|
|
with_items: "{{ users }}"
|
|
|
|
|
2017-02-03 19:24:02 +00:00
|
|
|
- name: Copy the p12 certificates
|
|
|
|
local_action:
|
|
|
|
module: copy
|
|
|
|
src: "configs/{{ IP_subject_alt_name }}/pki/private/{{ item }}.p12"
|
|
|
|
dest: "configs/{{ IP_subject_alt_name }}/{{ item }}.p12"
|
|
|
|
mode: 0600
|
|
|
|
become: no
|
|
|
|
with_items:
|
|
|
|
- "{{ users }}"
|
|
|
|
|
2016-10-06 17:39:53 +00:00
|
|
|
- name: Get active users
|
2017-02-03 19:24:02 +00:00
|
|
|
local_action: >
|
|
|
|
shell grep ^V index.txt | grep -v "{{ IP_subject_alt_name }}" | awk '{print $5}' | sed 's/\/CN=//g'
|
|
|
|
become: no
|
2016-10-06 17:39:53 +00:00
|
|
|
args:
|
2017-02-03 19:24:02 +00:00
|
|
|
chdir: "configs/{{ IP_subject_alt_name }}/pki/"
|
2016-10-06 17:39:53 +00:00
|
|
|
register: valid_certs
|
|
|
|
|
|
|
|
- name: Revoke non-existing users
|
2017-02-03 19:24:02 +00:00
|
|
|
local_action: >
|
|
|
|
shell openssl ca -config openssl.cnf -passin pass:"{{ easyrsa_CA_password }}" -revoke certs/{{ item }}.crt &&
|
|
|
|
openssl ca -gencrl -config openssl.cnf -passin pass:"{{ easyrsa_CA_password }}" -revoke certs/{{ item }}.crt -out crl/{{ item }}.crt
|
|
|
|
touch crl/{{ item }}_revoked
|
|
|
|
become: no
|
2016-10-06 17:39:53 +00:00
|
|
|
args:
|
2017-02-03 19:24:02 +00:00
|
|
|
chdir: "configs/{{ IP_subject_alt_name }}/pki/"
|
|
|
|
creates: crl/{{ item }}_revoked
|
|
|
|
environment:
|
|
|
|
subjectAltName: "DNS:{{ item }}"
|
2016-10-06 17:39:53 +00:00
|
|
|
when: item not in users
|
|
|
|
with_items: "{{ valid_certs.stdout_lines }}"
|
|
|
|
|
2017-02-03 19:24:02 +00:00
|
|
|
- name: Copy the revoked certificates to the vpn server
|
|
|
|
copy:
|
|
|
|
src: configs/{{ IP_subject_alt_name }}/pki/crl/{{ item }}.crt
|
2017-03-18 09:22:07 +00:00
|
|
|
dest: "{{ config_prefix|default('/') }}etc/ipsec.d/crls/{{ item }}.crt"
|
2017-02-03 19:24:02 +00:00
|
|
|
when: item not in users
|
|
|
|
with_items: "{{ valid_certs.stdout_lines }}"
|
|
|
|
notify:
|
|
|
|
- rereadcrls
|
|
|
|
|
2016-10-06 17:39:53 +00:00
|
|
|
- name: Register p12 PayloadContent
|
2017-02-03 19:24:02 +00:00
|
|
|
local_action: >
|
|
|
|
shell cat private/{{ item }}.p12 | base64
|
2016-10-06 17:39:53 +00:00
|
|
|
register: PayloadContent
|
2017-02-03 19:24:02 +00:00
|
|
|
become: no
|
|
|
|
args:
|
|
|
|
chdir: "configs/{{ IP_subject_alt_name }}/pki/"
|
2016-10-06 17:39:53 +00:00
|
|
|
with_items: "{{ users }}"
|
|
|
|
|
2017-02-03 19:24:02 +00:00
|
|
|
- name: Set facts for mobileconfigs
|
|
|
|
set_fact:
|
|
|
|
proxy_enabled: false
|
|
|
|
PayloadContentCA: "{{ lookup('file' , 'configs/{{ IP_subject_alt_name }}/pki/cacert.pem')|b64encode }}"
|
2016-10-06 17:39:53 +00:00
|
|
|
|
|
|
|
- name: Build the mobileconfigs
|
2017-02-03 19:24:02 +00:00
|
|
|
local_action:
|
|
|
|
module: template
|
|
|
|
src: roles/vpn/templates/mobileconfig.j2
|
|
|
|
dest: configs/{{ IP_subject_alt_name }}/{{ item.0 }}.mobileconfig
|
|
|
|
mode: 0600
|
|
|
|
become: no
|
2016-10-06 17:39:53 +00:00
|
|
|
with_together:
|
|
|
|
- "{{ users }}"
|
|
|
|
- "{{ PayloadContent.results }}"
|
|
|
|
no_log: True
|
|
|
|
|
2017-02-03 19:24:02 +00:00
|
|
|
- name: Build the client ipsec config file
|
|
|
|
local_action:
|
|
|
|
module: template
|
|
|
|
src: roles/vpn/templates/client_ipsec.conf.j2
|
|
|
|
dest: configs/{{ IP_subject_alt_name }}/ipsec_{{ item }}.conf
|
|
|
|
mode: 0600
|
|
|
|
become: no
|
|
|
|
with_items:
|
|
|
|
- "{{ users }}"
|
2016-10-06 17:39:53 +00:00
|
|
|
|
2017-02-03 19:24:02 +00:00
|
|
|
- name: Build the client ipsec secret file
|
|
|
|
local_action:
|
|
|
|
module: template
|
|
|
|
src: roles/vpn/templates/client_ipsec.secrets.j2
|
|
|
|
dest: configs/{{ IP_subject_alt_name }}/ipsec_{{ item }}.secrets
|
|
|
|
mode: 0600
|
|
|
|
become: no
|
|
|
|
with_items:
|
|
|
|
- "{{ users }}"
|
2016-10-06 17:39:53 +00:00
|
|
|
|
2017-02-03 19:24:02 +00:00
|
|
|
- name: Build the windows client powershell script
|
|
|
|
local_action:
|
|
|
|
module: template
|
|
|
|
src: roles/vpn/templates/client_windows.ps1.j2
|
|
|
|
dest: configs/{{ IP_subject_alt_name }}/windows_{{ item }}.ps1
|
|
|
|
mode: 0600
|
|
|
|
become: no
|
|
|
|
when: Win10_Enabled is defined and Win10_Enabled == "Y"
|
|
|
|
with_items: "{{ users }}"
|
2016-10-06 17:39:53 +00:00
|
|
|
|
|
|
|
# SSH
|
|
|
|
|
|
|
|
- name: SSH | Get active system users
|
|
|
|
shell: >
|
|
|
|
getent group algo | cut -f4 -d: | sed "s/,/\n/g"
|
|
|
|
register: valid_users
|
|
|
|
when: ssh_tunneling_enabled is defined and ssh_tunneling_enabled == "y"
|
|
|
|
|
|
|
|
- name: SSH | Delete non-existing users
|
|
|
|
user:
|
|
|
|
name: "{{ item }}"
|
|
|
|
state: absent
|
|
|
|
remove: yes
|
|
|
|
force: yes
|
|
|
|
when: item not in users and ssh_tunneling_enabled is defined and ssh_tunneling_enabled == "y"
|
2016-12-06 17:14:08 +00:00
|
|
|
with_items: "{{ valid_users.stdout_lines | default('null') }}"
|
2016-10-06 17:39:53 +00:00
|
|
|
|
2016-12-14 15:49:47 +00:00
|
|
|
post_tasks:
|
2017-02-15 15:49:26 +00:00
|
|
|
- debug:
|
|
|
|
msg:
|
|
|
|
- "{{ congrats.common.split('\n') }}"
|
|
|
|
- " {{ congrats.p12_pass }}"
|
2016-12-14 15:49:47 +00:00
|
|
|
tags: always
|
2017-02-03 19:24:02 +00:00
|
|
|
|
|
|
|
handlers:
|
|
|
|
- name: rereadcrls
|
|
|
|
shell: ipsec rereadcrls
|