self-hosting/roles/kubernetes-ca/tasks/main.yml

143 lines
4.9 KiB
YAML
Raw Normal View History

2018-07-31 17:33:26 +02:00
---
- name: Generate list of IP addresses and hostnames needed for Kubernetes API server certificate
set_fact:
tmpK8sHosts: |
{% set comma = joiner(",") %}
{% for item in groups["k8s_master"] -%}
2018-08-02 19:40:43 +02:00
{{ comma() }}{{ hostvars[item].private_ip }}{{ comma() }}{{ hostvars[item]["ansible_"+hostvars[item]["peervpn_conf_interface"]].ipv4.address }}{{ comma() }}{{ hostvars[item]["public_ip"] }}{{ comma() }}{{ hostvars[item].ansible_hostname }}
2018-07-31 17:33:26 +02:00
{%- endfor %}
{% for item in groups["k8s_worker"] -%}
2018-08-02 19:40:43 +02:00
{{ comma() }}{{ hostvars[item].private_ip}}{{ comma() }}{{ hostvars[item]["ansible_"+hostvars[item]["peervpn_conf_interface"]].ipv4.address }}{{ comma() }}{{ hostvars[item]["public_ip"] }}{{ comma() }}{{ hostvars[item].ansible_hostname }}
2018-07-31 17:33:26 +02:00
{%- endfor %}
{% for item in k8s_apiserver_cert_hosts -%}
{{ comma() }}{{item}}
{%- endfor %}
tags:
- kubernetes-ca
- name: Remove newline from controller hosts list
set_fact:
k8sHosts: "{{tmpK8sHosts |replace('\n', '')}}"
tags:
- kubernetes-ca
- name: Output of hostnames/IPs used for Kubernetes API server certificate
debug: var=k8sHosts
tags:
- kubernetes-ca
- name: Generate list of IP addresses and hostnames needed for etcd certificate
set_fact:
tmpEtcdHosts: |
{% set comma = joiner(",") %}
{% for item in groups["k8s_etcd"] -%}
2018-08-02 19:40:43 +02:00
{{ comma() }}{{ hostvars[item].private_ip}}{{ comma() }}{{ hostvars[item]["ansible_"+hostvars[item]["peervpn_conf_interface"]].ipv4.address }}{{ comma() }}{{hostvars[item]["public_ip"]}}{{ comma() }}{{ hostvars[item].ansible_hostname }}
2018-07-31 17:33:26 +02:00
{%- endfor %}
{% for item in etcd_cert_hosts -%}
{{ comma() }}{{item}}
{%- endfor %}
tags:
- kubernetes-ca
- kubernetes-ca-etcd
- name: Remove newline from etcd hosts list
set_fact:
etcdHosts: "{{tmpEtcdHosts |replace('\n', '')}}"
tags:
- kubernetes-ca
- kubernetes-ca-etcd
- name: Output of hostnames/IPs used for etcd certificate
debug: var=etcdHosts
tags:
- kubernetes-ca
- kubernetes-ca-etcd
- name: Create directory for CA and certificate files
file:
path: "{{k8s_ca_conf_directory}}"
owner: "{{k8s_ca_certificate_owner}}"
group: "{{k8s_ca_certificate_group}}"
mode: 0755
state: directory
tags:
- kubernetes-ca
2018-08-02 19:40:43 +02:00
- name: Create CA configuration file
2018-07-31 17:33:26 +02:00
template:
2018-08-02 19:40:43 +02:00
src: "ca-config.json.j2"
dest: "{{k8s_ca_conf_directory}}/ca-config.json"
2018-07-31 17:33:26 +02:00
owner: "{{k8s_ca_certificate_owner}}"
group: "{{k8s_ca_certificate_group}}"
mode: 0600
tags:
- kubernetes-ca
2018-08-02 19:40:43 +02:00
- name: Create the CSR files
2018-07-31 17:33:26 +02:00
template:
2018-08-02 19:40:43 +02:00
src: "csr.json.j2"
dest: "{{k8s_ca_conf_directory}}/{{ item.name }}-csr.json"
2018-07-31 17:33:26 +02:00
owner: "{{k8s_ca_certificate_owner}}"
group: "{{k8s_ca_certificate_group}}"
mode: 0600
tags:
- kubernetes-ca
2018-08-02 19:40:43 +02:00
loop: "{{ k8s_csr.master|flatten(levels=1)}}"
2018-08-02 21:03:31 +02:00
loop_control:
label: "{{ item.name }}"
2018-07-31 17:33:26 +02:00
2018-08-02 19:40:43 +02:00
- name: Generate CA and private key
shell: cfssl gencert -initca ca-csr.json | cfssljson -bare ca
2018-07-31 17:33:26 +02:00
args:
chdir: "{{k8s_ca_conf_directory}}"
2018-08-02 19:40:43 +02:00
creates: "{{k8s_ca_conf_directory}}/ca-key.pem"
2018-07-31 17:33:26 +02:00
tags:
- kubernetes-ca
2018-08-02 19:40:43 +02:00
- name: Create the worker CSR files
2018-07-31 17:33:26 +02:00
template:
src: "cert-worker-csr.json.j2"
2018-08-02 19:40:43 +02:00
dest: "{{k8s_ca_conf_directory}}/{{item}}-csr.json"
2018-07-31 17:33:26 +02:00
owner: "{{k8s_ca_certificate_owner}}"
group: "{{k8s_ca_certificate_group}}"
mode: 0600
with_inventory_hostnames:
- k8s_worker
vars:
- workerHost: "{{item}}"
tags:
- kubernetes-ca
2018-08-02 21:03:31 +02:00
- name: Generate TLS certificates whith hostname
shell: "cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -hostname={{item.hostnames}} -profile=kubernetes {{item.name}}-csr.json | cfssljson -bare {{item.name}}"
2018-07-31 17:33:26 +02:00
args:
chdir: "{{k8s_ca_conf_directory}}"
2018-08-02 21:03:31 +02:00
creates: "{{k8s_ca_conf_directory}}/{{item.name}}-key.pem"
2018-07-31 17:33:26 +02:00
tags:
- kubernetes-ca
2018-08-02 21:03:31 +02:00
loop: "{{ k8s_csr.master|flatten(levels=1)}}"
loop_control:
label: "{{ item.name }}"
when: item.hostnames is defined
2018-07-31 17:33:26 +02:00
2018-08-02 21:03:31 +02:00
- name: Generate TLS certificates whithout hostname
2018-08-02 19:40:43 +02:00
shell: "cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes {{item.name}}-csr.json | cfssljson -bare {{item.name}}"
2018-07-31 17:33:26 +02:00
args:
chdir: "{{k8s_ca_conf_directory}}"
2018-08-02 19:40:43 +02:00
creates: "{{k8s_ca_conf_directory}}/{{item.name}}-key.pem"
2018-07-31 17:33:26 +02:00
tags:
- kubernetes-ca
2018-08-02 19:40:43 +02:00
loop: "{{ k8s_csr.master|flatten(levels=1)}}"
2018-08-02 21:03:31 +02:00
loop_control:
label: "{{ item.name }}"
when: item.hostnames is not defined
2018-07-31 17:33:26 +02:00
- name: Generate TLS certificates for Kubernetes worker hosts
2018-08-02 19:40:43 +02:00
shell: "cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -hostname={{hostvars[item]['ansible_hostname']}},{{hostvars[item]['ansible_default_ipv4']['address']}},{{hostvars[item]['ansible_'+hostvars[item]['peervpn_conf_interface']].ipv4.address}} -profile=kubernetes {{item}}-csr.json | cfssljson -bare {{item}}"
2018-07-31 17:33:26 +02:00
args:
chdir: "{{k8s_ca_conf_directory}}"
2018-08-02 21:03:31 +02:00
creates: "{{k8s_ca_conf_directory}}/{{item}}-key.pem"
2018-07-31 17:33:26 +02:00
with_inventory_hostnames:
- k8s_worker
tags:
- kubernetes-ca