Skip to content
This repository was archived by the owner on Jan 5, 2025. It is now read-only.

Commit 92391f3

Browse files
committed
Update with community.crypto collection modules
1 parent 9bf495b commit 92391f3

File tree

11 files changed

+323
-115
lines changed

11 files changed

+323
-115
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/.vagrant/
2+
/certs/

Vagrantfile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# This guide is optimized for Vagrant 1.7 and above.
2+
# Although versions 1.6.x should behave very similarly, it is recommended
3+
# to upgrade instead of disabling the requirement below.
4+
Vagrant.require_version ">= 1.7.0"
5+
6+
Vagrant.configure(2) do |config|
7+
8+
config.vm.box = "debian/buster64"
9+
config.vm.synced_folder ".", "/vagrant", disabled: true
10+
# Disable the new default behavior introduced in Vagrant 1.7, to
11+
# ensure that all Vagrant machines will use the same SSH key pair.
12+
# See https://github.com/mitchellh/vagrant/issues/5005
13+
config.ssh.insert_key = false
14+
15+
config.vm.provider :libvirt do |lv|
16+
lv.cpus = 1
17+
lv.memory = 512
18+
end
19+
20+
config.vm.define "srv1" do |m|
21+
m.vm.hostname = "srv1"
22+
m.vm.network :private_network, ip: "192.168.123.30", libvirt__dhcp_enabled: false
23+
end
24+
config.vm.define "srv2" do |m|
25+
m.vm.hostname = "srv2"
26+
m.vm.network :private_network, ip: "192.168.123.31", libvirt__dhcp_enabled: false
27+
end
28+
29+
config.vm.provision "ansible" do |ansible|
30+
#ansible.become = true
31+
ansible.verbose = "v"
32+
ansible.playbook = "playbook.yml"
33+
ansible.inventory_path = "inventory.yml"
34+
end
35+
end

ansible.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[defaults]
2+
roles_path = /root/.ansible/roles/:../

certs/.gitkeep

Whitespace-only changes.

inventory.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
all:
3+
hosts:
4+
srv1:
5+
ansible_host: 192.168.123.30
6+
srv2:
7+
ansible_host: 192.168.123.31
8+
vars:
9+
cert_dir: ./certs
10+
generate_ca_cert: true
11+
generate_client_cert: true
12+
generate_server_cert: true
13+
tls_ca_email: me@example.org
14+
tls_ca_country: EU
15+
tls_ca_state: Italy
16+
tls_ca_locality: Rome
17+
tls_ca_organization: Example Inc.
18+
tls_ca_organizationalunit: SysAdmins

playbook.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
- name: Run role
3+
hosts: all
4+
roles:
5+
- role: generate-tls-certs

requirements.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
collections:
3+
- community.crypto

tasks/generate-ca-cert.yaml

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,65 @@
11
---
2-
- name: Generate CA private key
3-
local_action:
4-
module: openssl_privatekey
5-
path: "{{cert_dir}}/{{tls_ca_key}}"
6-
size: "{{tls_ca_key_size}}"
7-
run_once: true
2+
- name: Check if the CA private key exists
3+
delegate_to: localhost
4+
ansible.builtin.stat:
5+
path: "{{ cert_dir }}/{{ tls_ca_key }}"
6+
register: ca_key
87

9-
- name: Generate self-signed cert for CA
10-
local_action:
11-
module: |
12-
shell if [ ! -e {{cert_dir}}/{{tls_ca_cert}} ]
13-
then
14-
openssl req -x509 -new -days {{tls_ca_valid_days}} -sha256 -nodes -key {{cert_dir}}/{{tls_ca_key}} -out {{cert_dir}}/{{tls_ca_cert}} \
15-
-subj "{% if tls_ca_country is defined%}/C={{tls_ca_country}}{% endif %}{% if tls_ca_state is defined%}/ST={{tls_ca_state}}{% endif %}{% if tls_ca_locality is defined %}/L={{tls_ca_locality}}{% endif %}{% if tls_ca_organization is defined %}/O={{tls_ca_organization}}{% endif %}{% if tls_ca_organizationalunit is defined %}/OU={{tls_ca_organizationalunit}}{% endif %}/CN={{tls_ca_commonname}}{% if tls_ca_email is defined %}/emailAddress={{tls_ca_email}}{% endif %}"
16-
fi
17-
args:
18-
executable: /bin/bash
19-
ignore_errors: true
20-
run_once: true
8+
- name: Generate CA private key
9+
delegate_to: localhost
10+
community.crypto.openssl_privatekey:
11+
path: "{{ cert_dir }}/{{ tls_ca_key }}"
12+
size: "{{ tls_ca_key_size }}"
13+
run_once: true
14+
when: not ca_key.stat.exists
15+
16+
- name: Check if the CA CSR exists
17+
delegate_to: localhost
18+
stat:
19+
path: "{{ cert_dir }}/{{ tls_ca_csr }}"
20+
register: ca_csr
21+
22+
- name: Create CSR for CA
23+
delegate_to: localhost
24+
community.crypto.openssl_csr:
25+
path: "{{ cert_dir }}/{{ tls_ca_csr }}"
26+
privatekey_path: "{{ cert_dir }}/{{ tls_ca_key }}"
27+
basic_constraints:
28+
- "CA:TRUE"
29+
common_name: "{{ tls_ca_commonname|default('') }}"
30+
country_name: "{{ tls_ca_country|default('') }}"
31+
state_or_province_name: "{{ tls_ca_state|default('') }}"
32+
locality_name: "{{ tls_ca_locality|default('') }}"
33+
organization_name: "{{ tls_ca_organization|default('') }}"
34+
organizational_unit_name: "{{ tls_ca_organizationalunit|default('') }}"
35+
email_address: "{{ tls_ca_email }}"
36+
use_common_name_for_san: no
37+
when: not ca_csr.stat.exists
38+
39+
- name: Check if the CA cert exists
40+
delegate_to: localhost
41+
stat:
42+
path: "{{ cert_dir }}/{{ tls_ca_cert }}"
43+
register: ca_cert
44+
45+
- name: Create and sign server cert for CA
46+
delegate_to: localhost
47+
community.crypto.x509_certificate:
48+
path: "{{ cert_dir }}/{{ tls_ca_cert }}"
49+
privatekey_path: "{{ cert_dir }}/{{ tls_ca_key }}"
50+
csr_path: "{{ cert_dir }}/{{ tls_ca_csr }}"
51+
selfsigned_not_after: "+{{ tls_ca_valid_days }}d"
52+
provider: selfsigned
53+
when: not ca_cert.stat.exists
54+
register: ca_cert_file
55+
56+
- name: Copy the CA certificate to the remote machine
57+
copy:
58+
src: "{{ cert_dir }}/{{ tls_ca_cert }}"
59+
dest: /etc/ssl/certs/
60+
mode: 0644
61+
owner: root
62+
group: root
63+
force: yes
64+
backup: yes
65+
when: ca_cert_file.changed

tasks/generate-client-cert.yaml

Lines changed: 85 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,87 @@
11
---
2+
- name: Ensure the custom directories to host certificates are present
3+
become: yes
4+
file:
5+
state: directory
6+
recurse: yes
7+
path: "/etc/ssl/{{ item.path }}"
8+
mode: "{{ item.mode }}"
9+
owner: root
10+
group: root
11+
loop:
12+
- {path: local/certs, mode: "0755"}
13+
- {path: local/private, mode: "0700"}
214

3-
- name: Generate client private key
4-
local_action:
5-
module: openssl_privatekey
6-
path: "{{cert_dir}}/{{tls_client_key}}"
7-
size: "{{tls_client_key_size}}"
8-
run_once: true
9-
when: generate_client_cert
10-
11-
- name: Generate CSR and key for client cert
12-
local_action:
13-
module: |
14-
shell if [ ! -e {{cert_dir}}/{{tls_client_csr}} ]
15-
then
16-
openssl req -newkey rsa:{{tls_client_key_size}} -nodes -subj "/CN={{tls_client_commonname}}" \
17-
-keyout "{{cert_dir}}/{{tls_client_key}}" -out "{{cert_dir}}/{{tls_client_csr}}"
18-
fi
19-
args:
20-
executable: /bin/bash
21-
ignore_errors: true
22-
run_once: true
23-
when: generate_client_cert
24-
25-
- name: Add required extension for client authentication
26-
local_action:
27-
module: >
28-
shell echo extendedKeyUsage = clientAuth >> {{cert_dir}}/{{tls_client_extfile}}
29-
ignore_errors: true
30-
run_once: true
31-
when: generate_client_cert
32-
33-
# @AB TODO: using OpenSSL CA serial file does not always generate unique serial when running playbook against multiple hosts
34-
- name: Sign client cert request with CA
35-
local_action:
36-
module: |
37-
shell if [ ! -e {{cert_dir}}/{{tls_client_cert}} ]
38-
then
39-
openssl x509 -req -sha256 -days {{tls_client_valid_days}} -CA {{cert_dir}}/{{tls_ca_cert}} -CAkey {{cert_dir}}/{{tls_ca_key}} \
40-
-set_serial {{ 999999999 | random }} -in {{cert_dir}}/{{tls_client_csr}} -out {{cert_dir}}/{{tls_client_cert}} -extfile {{cert_dir}}/{{tls_client_extfile}}
41-
fi
42-
args:
43-
executable: /bin/bash
44-
ignore_errors: true
45-
run_once: true
46-
when: generate_client_cert
15+
- name: Check if the client private key exists
16+
delegate_to: localhost
17+
stat:
18+
path: "{{ cert_dir }}/{{ tls_client_key }}"
19+
register: client_key
20+
21+
- name: Generate client private key
22+
delegate_to: localhost
23+
community.crypto.openssl_privatekey:
24+
path: "{{ cert_dir }}/{{ tls_client_key }}"
25+
size: "{{ tls_client_key_size}}"
26+
when:
27+
- not client_key.stat.exists
28+
- generate_client_cert
29+
register: client_key_file
30+
31+
- name: Copy the key on the server
32+
become: yes
33+
copy:
34+
src: "{{ cert_dir }}/{{ tls_client_key}}"
35+
dest: /etc/ssl/local/certs/
36+
mode: 0644
37+
owner: root
38+
group: root
39+
when: client_key_file.changed
40+
41+
- name: Check if the client CSR exists
42+
delegate_to: localhost
43+
stat:
44+
path: "{{ cert_dir }}/{{ tls_client_csr }}"
45+
register: client_csr
46+
47+
- name: Generate CSR and key for client cert
48+
delegate_to: localhost
49+
community.crypto.openssl_csr:
50+
path: "{{ cert_dir }}/{{ tls_client_csr }}"
51+
privatekey_path: "{{ cert_dir }}/{{ tls_client_key }}"
52+
common_name: "{{ tls_client_commonname }}"
53+
extended_key_usage:
54+
- clientAuth
55+
when:
56+
- not client_csr.stat.exists
57+
- generate_client_cert
58+
59+
- name: Check if the client cert exists
60+
delegate_to: localhost
61+
stat:
62+
path: "{{ cert_dir }}/{{ tls_client_cert }}"
63+
register: client_crt
64+
65+
- name: Create and sign server cert request by CA
66+
delegate_to: localhost
67+
community.crypto.x509_certificate:
68+
path: "{{ cert_dir }}/{{ tls_client_cert }}"
69+
csr_path: "{{ cert_dir }}/{{ tls_client_csr }}"
70+
ownca_not_after: "+{{ tls_client_valid_days }}d"
71+
ownca_path: "{{ cert_dir }}/{{ tls_ca_cert }}"
72+
ownca_privatekey_path: "{{ cert_dir }}/{{ tls_ca_key }}"
73+
provider: ownca
74+
when:
75+
- not client_crt.stat.exists
76+
- generate_client_cert
77+
register: client_cert_file
78+
79+
- name: Copy the certificate to the remote machine
80+
become: yes
81+
copy:
82+
src: "{{ cert_dir }}/{{ tls_client_cert }}"
83+
dest: /etc/ssl/local/private
84+
mode: 0600
85+
owner: root
86+
group: root
87+
when: client_cert_file.changed

0 commit comments

Comments
 (0)