kubeasz/roles/deploy/tasks/create-kubectl-kubeconfig.yml

48 lines
1.9 KiB
YAML
Raw Normal View History

- name: 删除原有kubeconfig
file: path=/root/.kube/config state=absent
ignore_errors: true
- name: 下载 group:read rbac 文件
copy: src=read-group-rbac.yaml dest=/tmp/read-group-rbac.yaml
when: USER_NAME == "read"
- name: 创建group:read rbac 绑定
shell: "{{ base_dir }}/bin/kubectl apply -f /tmp/read-group-rbac.yaml"
when: USER_NAME == "read"
- name: 准备kubectl使用的{{ USER_NAME }}证书签名请求
2020-12-25 11:53:00 +08:00
template: src={{ USER_NAME }}-csr.json.j2 dest={{ cluster_dir }}/ssl/{{ USER_NAME }}-csr.json
- name: 创建{{ USER_NAME }}证书与私钥
2020-12-25 11:53:00 +08:00
shell: "cd {{ cluster_dir }}/ssl && {{ base_dir }}/bin/cfssl gencert \
-ca=ca.pem \
-ca-key=ca-key.pem \
-config=ca-config.json \
-profile=kubernetes {{ USER_NAME }}-csr.json | {{ base_dir }}/bin/cfssljson -bare {{ USER_NAME }}"
- name: 设置集群参数
shell: "{{ base_dir }}/bin/kubectl config set-cluster {{ CLUSTER_NAME }} \
2020-12-25 11:53:00 +08:00
--certificate-authority={{ cluster_dir }}/ssl/ca.pem \
--embed-certs=true \
2021-01-07 09:30:50 +08:00
--server={{ KUBE_APISERVER }} \
--kubeconfig={{ cluster_dir }}/kubectl.kubeconfig"
- name: 设置客户端认证参数
shell: "{{ base_dir }}/bin/kubectl config set-credentials {{ USER_NAME }} \
2020-12-25 11:53:00 +08:00
--client-certificate={{ cluster_dir }}/ssl/{{ USER_NAME }}.pem \
--embed-certs=true \
2021-01-07 09:30:50 +08:00
--client-key={{ cluster_dir }}/ssl/{{ USER_NAME }}-key.pem \
--kubeconfig={{ cluster_dir }}/kubectl.kubeconfig"
- name: 设置上下文参数
shell: "{{ base_dir }}/bin/kubectl config set-context {{ CONTEXT_NAME }} \
2021-01-07 09:30:50 +08:00
--cluster={{ CLUSTER_NAME }} --user={{ USER_NAME }} \
--kubeconfig={{ cluster_dir }}/kubectl.kubeconfig"
- name: 选择默认上下文
2021-01-07 09:30:50 +08:00
shell: "{{ base_dir }}/bin/kubectl config use-context {{ CONTEXT_NAME }} \
--kubeconfig={{ cluster_dir }}/kubectl.kubeconfig"
- name: 安装kubeconfig
copy: src={{ cluster_dir }}/kubectl.kubeconfig dest=~/.kube/config