How Can You Perform Variable Substitution using Kubectl?
I am trying to create a Role and RoleBinding so I can use Helm. I want to use variable substitution somehow to replace {{namespace}} with something when I run an apply command.
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: tiller-manager-{{namespace}}
namespace: {{namespace}}
rules:
- apiGroups: ["", "batch", "extensions", "apps"]
resources: ["*"]
verbs: ["*"]
I want to pass the namespace something like this:
kubectl apply --file role.yaml --namespace foo
I have seen that kubectl apply has a --template parameter but I can't see much information about how it might be used.
add a comment |
I am trying to create a Role and RoleBinding so I can use Helm. I want to use variable substitution somehow to replace {{namespace}} with something when I run an apply command.
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: tiller-manager-{{namespace}}
namespace: {{namespace}}
rules:
- apiGroups: ["", "batch", "extensions", "apps"]
resources: ["*"]
verbs: ["*"]
I want to pass the namespace something like this:
kubectl apply --file role.yaml --namespace foo
I have seen that kubectl apply has a --template parameter but I can't see much information about how it might be used.
1
For what you’ve shown here, you don’t need to specify the namespace in the YAML file:kubectlhas a--namespaceoption to fill that item in, and since resource names are namespace-scoped, you can have the same role name in multiple namespaces safely.
– David Maze
Nov 13 at 18:53
But I'm using the namespace to also modify the name.
– Muhammad Rehan Saeed
Nov 14 at 10:32
add a comment |
I am trying to create a Role and RoleBinding so I can use Helm. I want to use variable substitution somehow to replace {{namespace}} with something when I run an apply command.
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: tiller-manager-{{namespace}}
namespace: {{namespace}}
rules:
- apiGroups: ["", "batch", "extensions", "apps"]
resources: ["*"]
verbs: ["*"]
I want to pass the namespace something like this:
kubectl apply --file role.yaml --namespace foo
I have seen that kubectl apply has a --template parameter but I can't see much information about how it might be used.
I am trying to create a Role and RoleBinding so I can use Helm. I want to use variable substitution somehow to replace {{namespace}} with something when I run an apply command.
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: tiller-manager-{{namespace}}
namespace: {{namespace}}
rules:
- apiGroups: ["", "batch", "extensions", "apps"]
resources: ["*"]
verbs: ["*"]
I want to pass the namespace something like this:
kubectl apply --file role.yaml --namespace foo
I have seen that kubectl apply has a --template parameter but I can't see much information about how it might be used.
asked Nov 13 at 16:49
Muhammad Rehan Saeed
11.6k9108182
11.6k9108182
1
For what you’ve shown here, you don’t need to specify the namespace in the YAML file:kubectlhas a--namespaceoption to fill that item in, and since resource names are namespace-scoped, you can have the same role name in multiple namespaces safely.
– David Maze
Nov 13 at 18:53
But I'm using the namespace to also modify the name.
– Muhammad Rehan Saeed
Nov 14 at 10:32
add a comment |
1
For what you’ve shown here, you don’t need to specify the namespace in the YAML file:kubectlhas a--namespaceoption to fill that item in, and since resource names are namespace-scoped, you can have the same role name in multiple namespaces safely.
– David Maze
Nov 13 at 18:53
But I'm using the namespace to also modify the name.
– Muhammad Rehan Saeed
Nov 14 at 10:32
1
1
For what you’ve shown here, you don’t need to specify the namespace in the YAML file:
kubectl has a --namespace option to fill that item in, and since resource names are namespace-scoped, you can have the same role name in multiple namespaces safely.– David Maze
Nov 13 at 18:53
For what you’ve shown here, you don’t need to specify the namespace in the YAML file:
kubectl has a --namespace option to fill that item in, and since resource names are namespace-scoped, you can have the same role name in multiple namespaces safely.– David Maze
Nov 13 at 18:53
But I'm using the namespace to also modify the name.
– Muhammad Rehan Saeed
Nov 14 at 10:32
But I'm using the namespace to also modify the name.
– Muhammad Rehan Saeed
Nov 14 at 10:32
add a comment |
2 Answers
2
active
oldest
votes
You can do it in following way.
Write
Rolefile like this:
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: tiller-manager-${NAMESPACE}
namespace: ${NAMESPACE}
rules:
- apiGroups: ["", "batch", "extensions", "apps"]
resources: ["*"]
verbs: ["*"]
Set
NAMESPACEenvironment variable to your desired value.
Then create Role using following command
envsubst < role.yaml | kubectl apply -f -
I guess I have to installenvsubstthough first.
– Muhammad Rehan Saeed
Nov 13 at 17:04
If you are using linux, it is already installed.
– Emruz Hossain
Nov 13 at 17:05
If you are using Mac check answer from this: stackoverflow.com/questions/23620827/…
– Emruz Hossain
Nov 13 at 17:09
add a comment |
Another way you can use. With sed command you can replace {{namespace}} directly. without setting any environment variable or using envsubst.
- First save your role in a file say
role.yamlwhich contains original content. - Then use
sed 's/{{namespace}}/your-namespace-name/g' role.yaml. which print instdoutthe desired file. Replaced{{namespace}}withyour-namespace-name.
N.B: you can also save it in a file using sed 's/{{namespace}}/your-namespace-name/g' role.yaml > new-role.yaml
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285851%2fhow-can-you-perform-variable-substitution-using-kubectl%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can do it in following way.
Write
Rolefile like this:
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: tiller-manager-${NAMESPACE}
namespace: ${NAMESPACE}
rules:
- apiGroups: ["", "batch", "extensions", "apps"]
resources: ["*"]
verbs: ["*"]
Set
NAMESPACEenvironment variable to your desired value.
Then create Role using following command
envsubst < role.yaml | kubectl apply -f -
I guess I have to installenvsubstthough first.
– Muhammad Rehan Saeed
Nov 13 at 17:04
If you are using linux, it is already installed.
– Emruz Hossain
Nov 13 at 17:05
If you are using Mac check answer from this: stackoverflow.com/questions/23620827/…
– Emruz Hossain
Nov 13 at 17:09
add a comment |
You can do it in following way.
Write
Rolefile like this:
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: tiller-manager-${NAMESPACE}
namespace: ${NAMESPACE}
rules:
- apiGroups: ["", "batch", "extensions", "apps"]
resources: ["*"]
verbs: ["*"]
Set
NAMESPACEenvironment variable to your desired value.
Then create Role using following command
envsubst < role.yaml | kubectl apply -f -
I guess I have to installenvsubstthough first.
– Muhammad Rehan Saeed
Nov 13 at 17:04
If you are using linux, it is already installed.
– Emruz Hossain
Nov 13 at 17:05
If you are using Mac check answer from this: stackoverflow.com/questions/23620827/…
– Emruz Hossain
Nov 13 at 17:09
add a comment |
You can do it in following way.
Write
Rolefile like this:
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: tiller-manager-${NAMESPACE}
namespace: ${NAMESPACE}
rules:
- apiGroups: ["", "batch", "extensions", "apps"]
resources: ["*"]
verbs: ["*"]
Set
NAMESPACEenvironment variable to your desired value.
Then create Role using following command
envsubst < role.yaml | kubectl apply -f -
You can do it in following way.
Write
Rolefile like this:
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: tiller-manager-${NAMESPACE}
namespace: ${NAMESPACE}
rules:
- apiGroups: ["", "batch", "extensions", "apps"]
resources: ["*"]
verbs: ["*"]
Set
NAMESPACEenvironment variable to your desired value.
Then create Role using following command
envsubst < role.yaml | kubectl apply -f -
answered Nov 13 at 17:00
Emruz Hossain
1,084210
1,084210
I guess I have to installenvsubstthough first.
– Muhammad Rehan Saeed
Nov 13 at 17:04
If you are using linux, it is already installed.
– Emruz Hossain
Nov 13 at 17:05
If you are using Mac check answer from this: stackoverflow.com/questions/23620827/…
– Emruz Hossain
Nov 13 at 17:09
add a comment |
I guess I have to installenvsubstthough first.
– Muhammad Rehan Saeed
Nov 13 at 17:04
If you are using linux, it is already installed.
– Emruz Hossain
Nov 13 at 17:05
If you are using Mac check answer from this: stackoverflow.com/questions/23620827/…
– Emruz Hossain
Nov 13 at 17:09
I guess I have to install
envsubst though first.– Muhammad Rehan Saeed
Nov 13 at 17:04
I guess I have to install
envsubst though first.– Muhammad Rehan Saeed
Nov 13 at 17:04
If you are using linux, it is already installed.
– Emruz Hossain
Nov 13 at 17:05
If you are using linux, it is already installed.
– Emruz Hossain
Nov 13 at 17:05
If you are using Mac check answer from this: stackoverflow.com/questions/23620827/…
– Emruz Hossain
Nov 13 at 17:09
If you are using Mac check answer from this: stackoverflow.com/questions/23620827/…
– Emruz Hossain
Nov 13 at 17:09
add a comment |
Another way you can use. With sed command you can replace {{namespace}} directly. without setting any environment variable or using envsubst.
- First save your role in a file say
role.yamlwhich contains original content. - Then use
sed 's/{{namespace}}/your-namespace-name/g' role.yaml. which print instdoutthe desired file. Replaced{{namespace}}withyour-namespace-name.
N.B: you can also save it in a file using sed 's/{{namespace}}/your-namespace-name/g' role.yaml > new-role.yaml
add a comment |
Another way you can use. With sed command you can replace {{namespace}} directly. without setting any environment variable or using envsubst.
- First save your role in a file say
role.yamlwhich contains original content. - Then use
sed 's/{{namespace}}/your-namespace-name/g' role.yaml. which print instdoutthe desired file. Replaced{{namespace}}withyour-namespace-name.
N.B: you can also save it in a file using sed 's/{{namespace}}/your-namespace-name/g' role.yaml > new-role.yaml
add a comment |
Another way you can use. With sed command you can replace {{namespace}} directly. without setting any environment variable or using envsubst.
- First save your role in a file say
role.yamlwhich contains original content. - Then use
sed 's/{{namespace}}/your-namespace-name/g' role.yaml. which print instdoutthe desired file. Replaced{{namespace}}withyour-namespace-name.
N.B: you can also save it in a file using sed 's/{{namespace}}/your-namespace-name/g' role.yaml > new-role.yaml
Another way you can use. With sed command you can replace {{namespace}} directly. without setting any environment variable or using envsubst.
- First save your role in a file say
role.yamlwhich contains original content. - Then use
sed 's/{{namespace}}/your-namespace-name/g' role.yaml. which print instdoutthe desired file. Replaced{{namespace}}withyour-namespace-name.
N.B: you can also save it in a file using sed 's/{{namespace}}/your-namespace-name/g' role.yaml > new-role.yaml
answered Nov 15 at 6:40
Abu Hanifa
746922
746922
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285851%2fhow-can-you-perform-variable-substitution-using-kubectl%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
For what you’ve shown here, you don’t need to specify the namespace in the YAML file:
kubectlhas a--namespaceoption to fill that item in, and since resource names are namespace-scoped, you can have the same role name in multiple namespaces safely.– David Maze
Nov 13 at 18:53
But I'm using the namespace to also modify the name.
– Muhammad Rehan Saeed
Nov 14 at 10:32