---
- name: Create a directory if it does not exist
ansible.builtin.file:
path: "{{ ca_dir }}"
state: directory
mode: '0755'
delegate_to: localhost
- name: add template ca-csr.json.j2
template:
src: ca-csr.json.j2
dest: "{{ ca_dir }}/ca-csr.json"
delegate_to: localhost
- name: add template ca-config.json
template:
src: ca-config.json.j2
dest: "{{ ca_dir }}/ca-config.json"
delegate_to: localhost
- name: add template kubernetes-csr.json
template:
src: kubernetes-csr.json.j2
dest: "{{ ca_dir }}/kubernetes-csr.json"
delegate_to: localhost
- name: add template admin-csr.json
template:
src: admin-csr.json.j2
dest: "{{ ca_dir }}/admin-csr.json"
delegate_to: localhost
- name: add template worker-csr.json
template:
src: worker-csr.json.j2
dest: "{{ ca_dir }}/{{ item | replace('[','') | replace(']','') | from_yaml }}-csr.json"
loop:
- "{{ groups['controller'] }}"
delegate_to: localhost
- name: add template kube-controller-manager-csr.json
template:
src: kube-controller-manager-csr.json.j2
dest: "{{ ca_dir }}/kube-controller-manager-csr.json"
delegate_to: localhost
- name: add template kube-proxy-csr.json
template:
src: kube-proxy-csr.json.j2
dest: "{{ ca_dir }}/kube-proxy-csr.json"
delegate_to: localhost
- name: add template kube-scheduler-csr.json
template:
src: kube-scheduler-csr.json.j2
dest: "{{ ca_dir }}/kube-scheduler-csr.json"
delegate_to: localhost
- name: add template service-account-csr.json
template:
src: service-account-csr.json.j2
dest: "{{ ca_dir }}/service-account-csr.json"
delegate_to: localhost
- name: Generate CA key ca-key.pem and certificate ca.pem. Certificate Authority
ansible.builtin.shell:
cmd: cfssl gencert -initca {{ ca_dir }}/ca-csr.json | cfssljson -bare ca
chdir: "{{ ca_dir }}/"
delegate_to: localhost
- name: Generate the key and certificate for the API server, which are by default saved into file kubernetes-key.pem and kubernetes.pem. Hostname in template add all hostname
ansible.builtin.shell:
cmd: cfssl gencert -ca={{ ca_dir }}/ca.pem -ca-key={{ ca_dir }}/ca-key.pem --config={{ ca_dir }}/ca-config.json -profile=kubernetes {{ ca_dir }}/kubernetes-csr.json | cfssljson -bare kubernetes
chdir: "{{ ca_dir }}/"
delegate_to: localhost
#../cfssl gencert -ca=ca.pem -ca-key=ca-key.pem --config=ca-config.json -profile=kubernetes kubernetes-csr.json | ../cfssljson -bare server
#- name: Distributing Self-Signed CA Certificate
# copy:
# src:
# dest:
- name: The Admin Client Certificate
ansible.builtin.shell:
cmd: cfssl gencert -ca={{ ca_dir }}/ca.pem -ca-key={{ ca_dir }}/ca-key.pem --config={{ ca_dir }}/ca-config.json -profile=kubernetes {{ ca_dir }}/admin-csr.json | cfssljson -bare admin
chdir: "{{ ca_dir }}/"
delegate_to: localhost
- name: The Kubelet Client Certificates
ansible.builtin.shell:
cmd: cfssl gencert -ca={{ ca_dir }}/ca.pem -ca-key={{ ca_dir }}/ca-key.pem --config={{ ca_dir }}/ca-config.json -hostname={{ item | replace('[','') | replace(']','') | from_yaml }},{{ ansible_default_ipv4.address }} -profile=kubernetes {{ item | replace('[','') | replace(']','') | from_yaml }}-csr.json | cfssljson -bare {{ item | replace('[','') | replace(']','') | from_yaml }}
chdir: "{{ ca_dir }}/"
delegate_to: localhost
loop:
- "{{ groups['controller'] }}"
- name: The Controller Manager Client Certificate
ansible.builtin.shell:
cmd: cfssl gencert -ca={{ ca_dir }}/ca.pem -ca-key={{ ca_dir }}/ca-key.pem --config={{ ca_dir }}/ca-config.json -profile=kubernetes kube-controller-manager-csr.json | cfssljson -bare kube-controller-manager
chdir: "{{ ca_dir }}/"
delegate_to: localhost
- name: The Kube Proxy Client Certificate
ansible.builtin.shell:
cmd: cfssl gencert -ca={{ ca_dir }}/ca.pem -ca-key={{ ca_dir }}/ca-key.pem --config={{ ca_dir }}/ca-config.json -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxy
chdir: "{{ ca_dir }}/"
delegate_to: localhost