This repository has been archived on 2023-12-26. You can view files and clone it, but cannot push or open issues or pull requests.
ansible-role-deploy-ssh-cer.../tasks/deploy_certificate.yml

64 lines
2 KiB
YAML
Raw Normal View History

2023-04-26 16:41:33 +00:00
- name: Generate key pair
openssh_keypair:
path: "/etc/ssh/{{ item.name }}"
type: "{{ item.key_type }}"
register: key_pair
- name: Check whether certificate exists
stat:
path: "/etc/ssh/{{ item.name }}-cert.pub"
register: cert_stat
- name: Generate SSH user certificate
command:
cmd: "ssh -o ConnectTimeout=3 -o ConnectionAttempts=2 {{ ssh_ca_host }} '{{ ssh_ca_script }} user {{ item.signing_key }} \"{{ key_pair.public_key }}\" {{ item.host }} \"{{ item.principals }}\"'"
2023-04-26 17:17:40 +00:00
register: user_cert
2023-04-26 16:41:33 +00:00
delegate_to: localhost
when: item.type == "user" and not cert_stat.stat.exists
- name: Generate SSH host certificate
command:
cmd: "ssh -o ConnectTimeout=3 -o ConnectionAttempts=2 {{ ssh_ca_host }} '{{ ssh_ca_script }} host {{ item.signing_key }} \"{{ key_pair.public_key }}\" {{ item.host }}'"
2023-04-26 17:17:40 +00:00
register: host_cert
2023-04-26 16:41:33 +00:00
delegate_to: localhost
when: item.type == "host" and not cert_stat.stat.exists
- name: Place certificate
copy:
dest: "/etc/ssh/{{ item.name }}-cert.pub"
2023-04-26 17:17:40 +00:00
content: "{{ (item.type == 'user') | ternary(user_cert.stdout, host_cert.stdout) }}"
2023-04-26 16:41:33 +00:00
mode: 0644
2023-04-26 16:51:27 +00:00
when: not cert_stat.stat.exists
2023-04-26 16:41:33 +00:00
- name: Enable user certificate
lineinfile:
path: /etc/ssh/ssh_config.d/certificates.conf
line: "CertificateFile /etc/ssh/{{ item.name }}-cert.pub"
2023-04-26 16:53:15 +00:00
create: true
2023-04-26 16:41:33 +00:00
when: item.type == "user"
2023-04-26 17:12:10 +00:00
notify: restart sshd
2023-04-26 16:41:33 +00:00
- name: Enable user identity
lineinfile:
path: /etc/ssh/ssh_config.d/certificates.conf
line: "IdentityFile /etc/ssh/{{ item.name }}"
2023-04-26 16:53:15 +00:00
create: true
2023-04-26 16:41:33 +00:00
when: item.type == "user"
2023-04-26 17:12:10 +00:00
notify: restart sshd
2023-04-26 16:41:33 +00:00
- name: Enable host certificate
lineinfile:
path: /etc/ssh/sshd_config.d/certificates.conf
line: "HostCertificate /etc/ssh/{{ item.name }}-cert.pub"
2023-04-26 16:53:15 +00:00
create: true
2023-04-26 16:41:33 +00:00
when: item.type == "host"
2023-04-26 17:12:10 +00:00
notify: restart sshd
2023-04-26 16:41:33 +00:00
- name: Enable host key
lineinfile:
path: /etc/ssh/sshd_config.d/certificates.conf
line: "HostKey /etc/ssh/{{ item.name }}"
2023-04-26 16:53:15 +00:00
create: true
2023-04-26 16:41:33 +00:00
when: item.type == "host"
2023-04-26 17:12:10 +00:00
notify: restart sshd