Added seed code for caas-kubernetes.
[ta/caas-kubernetes.git] / ansible / roles / kubeconfig / tasks / main.yml
1 ---
2 # Copyright 2019 Nokia
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 - name: create directory
17   file:
18     name: "{{ config.path | dirname }}"
19     state: directory
20     mode: 0755
21     owner: "{{ config.owner | default('root') }}"
22     group: "{{ config.group | default('root') }}"
23
24 - name: create kubeconfig
25   command: "/usr/bin/kubectl config {{ cmd }} --kubeconfig={{ config.path }}"
26   with_items:
27     - "set-cluster kubernetes --certificate-authority=/etc/openssl/ca.pem --embed-certs=true --server=https://{{ config.apiserver }}:{{ config.apiserver_port }}"
28     - "set-context default --cluster=kubernetes --user={{ config.user }}"
29     - "use-context default"
30   loop_control:
31     loop_var: cmd
32
33 - name: set user auth with token
34   command: "/usr/bin/kubectl config set-credentials {{ config.user }} --token={{ config.token }} --kubeconfig={{ config.path }}"
35   when: config.token is defined and config.token
36
37 - name: set user auth with certs
38   command: "/usr/bin/kubectl config set-credentials {{ config.user }} --client-certificate={{ config.cert }} --client-key={{ config.key }} --embed-certs=true --kubeconfig={{ config.path }}"
39   when: not (config.token is defined and config.token)
40
41 - name: changing permissions of kubeconfig
42   file:
43     path: "{{ config.path }}"
44     mode: "{{ config.restricted | default(true) | ternary('0640', '0644') }}"
45     owner: "{{ config.owner | default('root') }}"
46     group: "{{ config.group | default('root') }}"
47
48 - name: allowing users to access kubeconfig
49   acl:
50     name: "{{ config.path }}"
51     entity: "{{ user }}"
52     etype: user
53     permissions: "r"
54     state: present
55   with_items: "{{ config.add_users | default([]) }}"
56   loop_control:
57     loop_var: user
58
59 - name: adding read permission to kubeconfig dir
60   acl:
61     name: "{{ config.path | dirname }}"
62     entity: "{{ user }}"
63     etype: user
64     permissions: "rx"
65     state: present
66   with_items: "{{ config.add_users | default([]) }}"
67   loop_control:
68     loop_var: user