How to create multiple roles in a single YAML file

7/4/2018

I am trying to create multiple roles in Kubernetes in a single YAML file.
I am not sure if it possible.

This is what I tried:

cat > ~/tmp/Role.yaml <<EOF 
kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata: 
  namespace: default
  name: testRole1
rules: 
- apiGroups: ["", "extensions", "apps"]
  resources: ["pods"]
  verbs: ["get", "list"] 

kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata: 
  namespace: default
  name: testRole2
rules: 
- apiGroups: ["", "extensions", "apps"]
  resources: ["pods"]
  verbs: ["get"] 
EOF

kubectl create -f ~/tmp/Role.yaml

It creates me only the latter - testRole2.

Is it possible to do it in one YAML file or I need to create YAML file for each Role ?

-- E235
kubernetes
role
yaml

2 Answers

7/4/2018

I just received an answer from @liggitt.

Need to add a record separator between them (---).

-- E235
Source: StackOverflow

7/4/2018

FYI

You can write the yaml using List object as follows.

apiVersion: v1
kind: List
items:
- kind: Role
  apiVersion: rbac.authorization.k8s.io/v1beta1
  metadata: 
    namespace: default
    name: testRole1
  rules: 
  - apiGroups: ["", "extensions", "apps"]
    resources: ["pods"]
    verbs: ["get", "list"] 
- kind: Role
  apiVersion: rbac.authorization.k8s.io/v1beta1
  metadata: 
    namespace: default
    name: testRole2
  rules: 
  - apiGroups: ["", "extensions", "apps"]
    resources: ["pods"]
    verbs: ["get"] 
-- Daein Park
Source: StackOverflow