How Can You Perform Variable Substitution using Kubectl?












0














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.










share|improve this question


















  • 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










  • But I'm using the namespace to also modify the name.
    – Muhammad Rehan Saeed
    Nov 14 at 10:32
















0














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.










share|improve this question


















  • 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










  • But I'm using the namespace to also modify the name.
    – Muhammad Rehan Saeed
    Nov 14 at 10:32














0












0








0







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.










share|improve this question













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.







kubernetes kubectl






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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: 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














  • 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










  • 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












2 Answers
2






active

oldest

votes


















2














You can do it in following way.





  1. Write Role file like this:



    kind: Role
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
    name: tiller-manager-${NAMESPACE}
    namespace: ${NAMESPACE}
    rules:
    - apiGroups: ["", "batch", "extensions", "apps"]
    resources: ["*"]
    verbs: ["*"]


  2. Set NAMESPACE environment variable to your desired value.



  3. Then create Role using following command



    envsubst < role.yaml | kubectl apply -f -







share|improve this answer





















  • 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 Mac check answer from this: stackoverflow.com/questions/23620827/…
    – Emruz Hossain
    Nov 13 at 17:09





















1














Another way you can use. With sed command you can replace {{namespace}} directly. without setting any environment variable or using envsubst.




  1. First save your role in a file say role.yaml which contains original content.

  2. Then use sed 's/{{namespace}}/your-namespace-name/g' role.yaml. which print in stdout the desired file. Replaced {{namespace}} with your-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






share|improve this answer





















    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    2














    You can do it in following way.





    1. Write Role file like this:



      kind: Role
      apiVersion: rbac.authorization.k8s.io/v1
      metadata:
      name: tiller-manager-${NAMESPACE}
      namespace: ${NAMESPACE}
      rules:
      - apiGroups: ["", "batch", "extensions", "apps"]
      resources: ["*"]
      verbs: ["*"]


    2. Set NAMESPACE environment variable to your desired value.



    3. Then create Role using following command



      envsubst < role.yaml | kubectl apply -f -







    share|improve this answer





















    • 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 Mac check answer from this: stackoverflow.com/questions/23620827/…
      – Emruz Hossain
      Nov 13 at 17:09


















    2














    You can do it in following way.





    1. Write Role file like this:



      kind: Role
      apiVersion: rbac.authorization.k8s.io/v1
      metadata:
      name: tiller-manager-${NAMESPACE}
      namespace: ${NAMESPACE}
      rules:
      - apiGroups: ["", "batch", "extensions", "apps"]
      resources: ["*"]
      verbs: ["*"]


    2. Set NAMESPACE environment variable to your desired value.



    3. Then create Role using following command



      envsubst < role.yaml | kubectl apply -f -







    share|improve this answer





















    • 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 Mac check answer from this: stackoverflow.com/questions/23620827/…
      – Emruz Hossain
      Nov 13 at 17:09
















    2












    2








    2






    You can do it in following way.





    1. Write Role file like this:



      kind: Role
      apiVersion: rbac.authorization.k8s.io/v1
      metadata:
      name: tiller-manager-${NAMESPACE}
      namespace: ${NAMESPACE}
      rules:
      - apiGroups: ["", "batch", "extensions", "apps"]
      resources: ["*"]
      verbs: ["*"]


    2. Set NAMESPACE environment variable to your desired value.



    3. Then create Role using following command



      envsubst < role.yaml | kubectl apply -f -







    share|improve this answer












    You can do it in following way.





    1. Write Role file like this:



      kind: Role
      apiVersion: rbac.authorization.k8s.io/v1
      metadata:
      name: tiller-manager-${NAMESPACE}
      namespace: ${NAMESPACE}
      rules:
      - apiGroups: ["", "batch", "extensions", "apps"]
      resources: ["*"]
      verbs: ["*"]


    2. Set NAMESPACE environment variable to your desired value.



    3. Then create Role using following command



      envsubst < role.yaml | kubectl apply -f -








    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 13 at 17:00









    Emruz Hossain

    1,084210




    1,084210












    • 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 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










    • 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















    1














    Another way you can use. With sed command you can replace {{namespace}} directly. without setting any environment variable or using envsubst.




    1. First save your role in a file say role.yaml which contains original content.

    2. Then use sed 's/{{namespace}}/your-namespace-name/g' role.yaml. which print in stdout the desired file. Replaced {{namespace}} with your-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






    share|improve this answer


























      1














      Another way you can use. With sed command you can replace {{namespace}} directly. without setting any environment variable or using envsubst.




      1. First save your role in a file say role.yaml which contains original content.

      2. Then use sed 's/{{namespace}}/your-namespace-name/g' role.yaml. which print in stdout the desired file. Replaced {{namespace}} with your-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






      share|improve this answer
























        1












        1








        1






        Another way you can use. With sed command you can replace {{namespace}} directly. without setting any environment variable or using envsubst.




        1. First save your role in a file say role.yaml which contains original content.

        2. Then use sed 's/{{namespace}}/your-namespace-name/g' role.yaml. which print in stdout the desired file. Replaced {{namespace}} with your-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






        share|improve this answer












        Another way you can use. With sed command you can replace {{namespace}} directly. without setting any environment variable or using envsubst.




        1. First save your role in a file say role.yaml which contains original content.

        2. Then use sed 's/{{namespace}}/your-namespace-name/g' role.yaml. which print in stdout the desired file. Replaced {{namespace}} with your-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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 at 6:40









        Abu Hanifa

        746922




        746922






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            How to pass form data using jquery Ajax to insert data in database?

            National Museum of Racing and Hall of Fame

            Guess what letter conforming each word