Update ansible roles to automate nordix openvpn user management
[infra/tools.git] / infra / openvpn_automation / playbooks / roles / revoke-user-ovpn / tasks / main.yaml
1 ---
2 - name: Check {{ user_name }} cert existence in CA
3   stat:
4     path: "{{ nordix_ca_easyrsa_path }}/pki/issued//{{ user_name }}.crt"
5   register: client_cert
6
7 - name: Revoke {{ user_name }} cert in CA
8   command: ./easyrsa --batch revoke {{ user_name }}
9   args:
10     chdir: "{{ nordix_ca_easyrsa_path }}"
11   when: client_cert.stat.exists == true
12   register: revoke_result
13
14 - name: Run gen-crl in CA
15   command: ./easyrsa gen-crl
16   args:
17     chdir: "{{ nordix_ca_easyrsa_path }}"
18   when: revoke_result.rc == 0
19
20 - name: Check updated crl file existence in CA
21   stat:
22     path: "{{ nordix_ca_easyrsa_path }}/pki/crl.pem"
23   register: crl_existence
24
25 - name: Fetch updated crl from CA to Openvpn /tmp dir
26   fetch:
27     src: "{{ nordix_ca_easyrsa_path }}/pki/crl.pem"
28     dest: "/tmp/"
29     flat: true
30   when: crl_existence.stat.exists == true
31   changed_when: true
32
33 - name: Move updated crl to /etc/openvpn/ dir
34   copy:
35     src: /tmp/crl.pem
36     dest: /etc/openvpn/
37   delegate_to: jumphost
38   register: updated_crl
39   changed_when: true
40   become: true
41
42 - name: Restart Openvpn Service
43   systemd:
44     name: openvpn@server
45     state: restarted
46     daemon_reload: true
47     enabled: true
48   delegate_to: jumphost
49   become: true
50
51 - name: Remove {{ user_name }} files from OpenVPN server
52   file:
53     path: "{{ item }}"
54     state: absent
55   with_items:
56     - "{{ nordix_openvpn_easyrsa_path }}/pki/private/{{ user_name }}.key"
57     - "{{ nordix_openvpn_easyrsa_path }}/pki/reqs/{{ user_name }}.req"
58     - "{{ nordix_openvpn_clientconfig }}/keys/{{ user_name }}.crt"
59     - "{{ nordix_openvpn_clientconfig }}/keys/{{ user_name }}.key"
60     - "{{ nordix_openvpn_clientconfig }}/files/{{ user_name }}.ovpn"
61   delegate_to: jumphost
62   register: user_files_removal
63
64 - name: Log user specific ovpn files location on the console
65   debug:
66     msg:
67       - ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
68       - "The OpenVPN access has been revoked for the user {{ user_name }}"
69       - ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
70       - ''
71   when: '"All items completed" in user_files_removal.msg'
72   delegate_to: localhost