blob: fe8aeea448a70b45e44ed85a6fb2758aeac6637f [file] [log] [blame]
Zack Williamsc047c872017-01-11 08:38:15 -07001---
2# pki-ca/tasks/main.yml
3
Andy Bavier1cac0012017-03-13 10:06:18 -04004- name: Create PKI directory
5 become: yes
6 file:
7 dest: "{{ pki_dir }}"
8 state: directory
9 owner: "{{ ansible_user_id }}"
10 mode: 0755
11
Zack Williamsc047c872017-01-11 08:38:15 -070012- name: Create intermediate CA directory
Zack Williams43d62b52017-01-23 07:34:45 -070013 become: yes
Zack Williamsc047c872017-01-11 08:38:15 -070014 file:
15 dest: "{{ pki_dir }}/intermediate_ca"
16 state: directory
Zack Williams43d62b52017-01-23 07:34:45 -070017 owner: "{{ ansible_user_id }}"
18 mode: 0755
Zack Williamsc047c872017-01-11 08:38:15 -070019
20- name: Create intermediate CA openssl.cnf from template
21 template:
22 src: openssl_im.cnf.j2
23 dest: "{{ pki_dir }}/intermediate_ca/openssl.cnf"
24 force: no
25
26- name: Create subdirs for intermediate CA
27 file:
28 dest: "{{ pki_dir }}/intermediate_ca/{{ item }}"
29 state: directory
30 with_items:
31 - certs
32 - crl
33 - csr
34 - newcerts
35
36- name: Create private CA directory
37 file:
38 dest: "{{ pki_dir }}/intermediate_ca/private"
39 state: directory
40 mode: 0700
41
42- name: Create serial file
43 copy:
44 dest: "{{ pki_dir }}/intermediate_ca/serial"
45 content: "01"
46 force: no
47
48- name: Create empty index file if it doesn't exist
49 copy:
50 dest: "{{ pki_dir }}/intermediate_ca/index.txt"
51 content: ""
52 force: no
53
54- name: Save intermediate passphrase to intermediate_ca/private/ca_im_phrase
55 copy:
56 dest: "{{ pki_dir }}/intermediate_ca/private/ca_im_phrase"
57 content: "{{ ca_im_phrase }}"
58 mode: 0400
59
60- name: Generate intermediate key
61 command: >
62 openssl genrsa -aes256
63 -out {{ pki_dir }}/intermediate_ca/private/im_key.pem
64 -passout file:{{ pki_dir }}/intermediate_ca/private/ca_im_phrase
65 {{ ca_size }}
66 args:
67 creates: "{{ pki_dir }}/intermediate_ca/private/im_key.pem"
68
69- name: Set permissions on intermediate key
70 file:
71 dest: "{{ pki_dir }}/intermediate_ca/private/im_key.pem"
72 mode: 0400
73
74- name: Create intermediate CSR
75 command: >
76 openssl req -config {{ pki_dir }}/intermediate_ca/openssl.cnf
77 -key {{ pki_dir }}/intermediate_ca/private/im_key.pem
78 -passin file:{{ pki_dir }}/intermediate_ca/private/ca_im_phrase
79 -new -sha256 -subj "{{ ca_im_subj }}"
80 -out {{ pki_dir }}/intermediate_ca/csr/intermediate_ca_csr.pem
81 args:
82 creates: "{{ pki_dir }}/intermediate_ca/certs/intermediate_ca_csr.pem"
83 environment:
84 KEY_ALTNAMES: ""
85
86- name: Create intermediate cert from CSR with root CA
87 command: >
88 openssl ca -config {{ pki_dir }}/root_ca/openssl.cnf -batch
89 -extensions v3_intermediate_ca
90 -passin file:{{ pki_dir }}/root_ca/private/ca_root_phrase
91 -days {{ ca_im_days }} -md {{ ca_digest }}
92 -in {{ pki_dir }}/intermediate_ca/csr/intermediate_ca_csr.pem
93 -out {{ pki_dir }}/intermediate_ca/certs/im_cert.pem
94 args:
95 creates: "{{ pki_dir }}/intermediate_ca/certs/im_cert.pem"
96
97- name: Verify intemediate cert
98 command: >
99 openssl verify
100 -CAfile {{ pki_dir }}/root_ca/certs/ca_cert.pem
101 {{ pki_dir }}/intermediate_ca/certs/im_cert.pem
102 register: im_verify
103 tags:
104 - skip_ansible_lint # diagnostic command
105
106- name: Assert that verify of intermediate cert succeeded
107 assert:
108 that: "'OK' in '{{ im_verify.stdout }}'"
109
110- name: Get the root cert into ca_cert var
111 command: >
112 openssl x509 -in {{ pki_dir }}/root_ca/certs/ca_cert.pem
113 register: ca_cert
114 tags:
115 - skip_ansible_lint # concat of files
116
117- name: Get the intermediate cert into im_cert var
118 command: >
119 openssl x509 -in {{ pki_dir }}/intermediate_ca/certs/im_cert.pem
120 register: im_cert
121 tags:
122 - skip_ansible_lint # concat of files
123
124- name: Create intermediate cert chain
125 copy:
126 dest: "{{ pki_dir }}/intermediate_ca/certs/im_cert_chain.pem"
127 content: "{{ im_cert.stdout }}\n{{ ca_cert.stdout }}"