Ansible nested loops












0














I have a variable that looks like this:



device:
- sdb: 2
- sdc: 3
- sdd: 4


How can I produce the result with ansible loops:



sdb 1
sdb 2
sdc 1
sdc 2
sdc 3
sdd 1
sdd 2
sdd 3
sdd 4


I tried with_sequence and loop_control but it didn't work.










share|improve this question



























    0














    I have a variable that looks like this:



    device:
    - sdb: 2
    - sdc: 3
    - sdd: 4


    How can I produce the result with ansible loops:



    sdb 1
    sdb 2
    sdc 1
    sdc 2
    sdc 3
    sdd 1
    sdd 2
    sdd 3
    sdd 4


    I tried with_sequence and loop_control but it didn't work.










    share|improve this question

























      0












      0








      0







      I have a variable that looks like this:



      device:
      - sdb: 2
      - sdc: 3
      - sdd: 4


      How can I produce the result with ansible loops:



      sdb 1
      sdb 2
      sdc 1
      sdc 2
      sdc 3
      sdd 1
      sdd 2
      sdd 3
      sdd 4


      I tried with_sequence and loop_control but it didn't work.










      share|improve this question













      I have a variable that looks like this:



      device:
      - sdb: 2
      - sdc: 3
      - sdd: 4


      How can I produce the result with ansible loops:



      sdb 1
      sdb 2
      sdc 1
      sdc 2
      sdc 3
      sdd 1
      sdd 2
      sdd 3
      sdd 4


      I tried with_sequence and loop_control but it didn't work.







      loops ansible






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 at 2:35









      Cheng Shuai

      12




      12
























          3 Answers
          3






          active

          oldest

          votes


















          0














          Wrote a debug task with ansible loop and jinja which should give you the desired result. Refactor as required.



          - name: Debug device var
          debug:
          msg: "{% for key, value in item.iteritems() %}{% for i in range(value) %} {{ key }} {{ loop.index }} {% endfor %}{% endfor %}"
          loop: "{{ device }}"





          share|improve this answer





















          • Actually, I need use the result in the command. So how could I get the result without jinjia?
            – Cheng Shuai
            Nov 21 at 5:29












          • Yeah that's the correct output, isn't it ?
            – Anant Naugai
            Nov 21 at 5:32










          • The output should be "sdc 1" "sdc 2" "sdc 3" instead of " sdc 1 sdc 2 sdc 3 " in each command line.
            – Cheng Shuai
            Nov 21 at 5:40










          • If you just need quotes around the values then use this (quotes need to be escaped for debug msg): "{% for key, value in item.iteritems() %}{% for i in range(value) %}"{{ key }} {{ loop.index }}" {% endfor %}{% endfor %}"
            – Anant Naugai
            Nov 21 at 5:47












          • I mean I need separated lines in each command, not just need the quotes. Maybe jinja isn't suitable for the case.
            – Cheng Shuai
            Nov 21 at 6:13





















          0














          Finally, I got the solution but I changed the format of the variable, however. The variable is changed to:



          device:
          sdb: 2
          sdc: 3
          sdd: 4


          The result is:



          ok: [ceph-host-2] => (item=1) => {
          "msg": "sdd 1"
          }
          ok: [ceph-host-2] => (item=2) => {
          "msg": "sdd 2"
          }
          ok: [ceph-host-2] => (item=3) => {
          "msg": "sdd 3"
          }
          ok: [ceph-host-2] => (item=4) => {
          "msg": "sdd 4"
          }

          TASK [osd : debug]
          ok: [ceph-host-2] => (item=1) => {
          "msg": "sdb 1"
          }
          ok: [ceph-host-2] => (item=2) => {
          "msg": "sdb 2"
          }

          TASK [osd : debug]
          ok: [ceph-host-2] => (item=1) => {
          "msg": "sdc 1"
          }
          ok: [ceph-host-2] => (item=2) => {
          "msg": "sdc 2"
          }
          ok: [ceph-host-2] => (item=3) => {
          "msg": "sdc 3"
          }


          main.yml



          - include_tasks: inner.yml
          loop: "{{ device.keys() }}"
          loop_control:
          loop_var: outer_item


          inner.yml



          - debug:
          msg: "{{ outer_item }} {{ item }}"
          with_sequence: count={{ device[outer_item] }}





          share|improve this answer





























            0














            The variable is:



            device:
            - sdb: 2
            - sdc: 3
            - sdd: 4


            main.yml



            - include_tasks: inner.yml
            loop: "{{ device }}"
            loop_control:
            loop_var: outer_item


            inner.yml



            - debug:
            msg: "{{ outer_item.keys()[0] }} {{ item }}"
            with_sequence: count={{ outer_item.values()[0] }}





            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%2f53404545%2fansible-nested-loops%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              Wrote a debug task with ansible loop and jinja which should give you the desired result. Refactor as required.



              - name: Debug device var
              debug:
              msg: "{% for key, value in item.iteritems() %}{% for i in range(value) %} {{ key }} {{ loop.index }} {% endfor %}{% endfor %}"
              loop: "{{ device }}"





              share|improve this answer





















              • Actually, I need use the result in the command. So how could I get the result without jinjia?
                – Cheng Shuai
                Nov 21 at 5:29












              • Yeah that's the correct output, isn't it ?
                – Anant Naugai
                Nov 21 at 5:32










              • The output should be "sdc 1" "sdc 2" "sdc 3" instead of " sdc 1 sdc 2 sdc 3 " in each command line.
                – Cheng Shuai
                Nov 21 at 5:40










              • If you just need quotes around the values then use this (quotes need to be escaped for debug msg): "{% for key, value in item.iteritems() %}{% for i in range(value) %}"{{ key }} {{ loop.index }}" {% endfor %}{% endfor %}"
                – Anant Naugai
                Nov 21 at 5:47












              • I mean I need separated lines in each command, not just need the quotes. Maybe jinja isn't suitable for the case.
                – Cheng Shuai
                Nov 21 at 6:13


















              0














              Wrote a debug task with ansible loop and jinja which should give you the desired result. Refactor as required.



              - name: Debug device var
              debug:
              msg: "{% for key, value in item.iteritems() %}{% for i in range(value) %} {{ key }} {{ loop.index }} {% endfor %}{% endfor %}"
              loop: "{{ device }}"





              share|improve this answer





















              • Actually, I need use the result in the command. So how could I get the result without jinjia?
                – Cheng Shuai
                Nov 21 at 5:29












              • Yeah that's the correct output, isn't it ?
                – Anant Naugai
                Nov 21 at 5:32










              • The output should be "sdc 1" "sdc 2" "sdc 3" instead of " sdc 1 sdc 2 sdc 3 " in each command line.
                – Cheng Shuai
                Nov 21 at 5:40










              • If you just need quotes around the values then use this (quotes need to be escaped for debug msg): "{% for key, value in item.iteritems() %}{% for i in range(value) %}"{{ key }} {{ loop.index }}" {% endfor %}{% endfor %}"
                – Anant Naugai
                Nov 21 at 5:47












              • I mean I need separated lines in each command, not just need the quotes. Maybe jinja isn't suitable for the case.
                – Cheng Shuai
                Nov 21 at 6:13
















              0












              0








              0






              Wrote a debug task with ansible loop and jinja which should give you the desired result. Refactor as required.



              - name: Debug device var
              debug:
              msg: "{% for key, value in item.iteritems() %}{% for i in range(value) %} {{ key }} {{ loop.index }} {% endfor %}{% endfor %}"
              loop: "{{ device }}"





              share|improve this answer












              Wrote a debug task with ansible loop and jinja which should give you the desired result. Refactor as required.



              - name: Debug device var
              debug:
              msg: "{% for key, value in item.iteritems() %}{% for i in range(value) %} {{ key }} {{ loop.index }} {% endfor %}{% endfor %}"
              loop: "{{ device }}"






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 21 at 3:21









              Anant Naugai

              19029




              19029












              • Actually, I need use the result in the command. So how could I get the result without jinjia?
                – Cheng Shuai
                Nov 21 at 5:29












              • Yeah that's the correct output, isn't it ?
                – Anant Naugai
                Nov 21 at 5:32










              • The output should be "sdc 1" "sdc 2" "sdc 3" instead of " sdc 1 sdc 2 sdc 3 " in each command line.
                – Cheng Shuai
                Nov 21 at 5:40










              • If you just need quotes around the values then use this (quotes need to be escaped for debug msg): "{% for key, value in item.iteritems() %}{% for i in range(value) %}"{{ key }} {{ loop.index }}" {% endfor %}{% endfor %}"
                – Anant Naugai
                Nov 21 at 5:47












              • I mean I need separated lines in each command, not just need the quotes. Maybe jinja isn't suitable for the case.
                – Cheng Shuai
                Nov 21 at 6:13




















              • Actually, I need use the result in the command. So how could I get the result without jinjia?
                – Cheng Shuai
                Nov 21 at 5:29












              • Yeah that's the correct output, isn't it ?
                – Anant Naugai
                Nov 21 at 5:32










              • The output should be "sdc 1" "sdc 2" "sdc 3" instead of " sdc 1 sdc 2 sdc 3 " in each command line.
                – Cheng Shuai
                Nov 21 at 5:40










              • If you just need quotes around the values then use this (quotes need to be escaped for debug msg): "{% for key, value in item.iteritems() %}{% for i in range(value) %}"{{ key }} {{ loop.index }}" {% endfor %}{% endfor %}"
                – Anant Naugai
                Nov 21 at 5:47












              • I mean I need separated lines in each command, not just need the quotes. Maybe jinja isn't suitable for the case.
                – Cheng Shuai
                Nov 21 at 6:13


















              Actually, I need use the result in the command. So how could I get the result without jinjia?
              – Cheng Shuai
              Nov 21 at 5:29






              Actually, I need use the result in the command. So how could I get the result without jinjia?
              – Cheng Shuai
              Nov 21 at 5:29














              Yeah that's the correct output, isn't it ?
              – Anant Naugai
              Nov 21 at 5:32




              Yeah that's the correct output, isn't it ?
              – Anant Naugai
              Nov 21 at 5:32












              The output should be "sdc 1" "sdc 2" "sdc 3" instead of " sdc 1 sdc 2 sdc 3 " in each command line.
              – Cheng Shuai
              Nov 21 at 5:40




              The output should be "sdc 1" "sdc 2" "sdc 3" instead of " sdc 1 sdc 2 sdc 3 " in each command line.
              – Cheng Shuai
              Nov 21 at 5:40












              If you just need quotes around the values then use this (quotes need to be escaped for debug msg): "{% for key, value in item.iteritems() %}{% for i in range(value) %}"{{ key }} {{ loop.index }}" {% endfor %}{% endfor %}"
              – Anant Naugai
              Nov 21 at 5:47






              If you just need quotes around the values then use this (quotes need to be escaped for debug msg): "{% for key, value in item.iteritems() %}{% for i in range(value) %}"{{ key }} {{ loop.index }}" {% endfor %}{% endfor %}"
              – Anant Naugai
              Nov 21 at 5:47














              I mean I need separated lines in each command, not just need the quotes. Maybe jinja isn't suitable for the case.
              – Cheng Shuai
              Nov 21 at 6:13






              I mean I need separated lines in each command, not just need the quotes. Maybe jinja isn't suitable for the case.
              – Cheng Shuai
              Nov 21 at 6:13















              0














              Finally, I got the solution but I changed the format of the variable, however. The variable is changed to:



              device:
              sdb: 2
              sdc: 3
              sdd: 4


              The result is:



              ok: [ceph-host-2] => (item=1) => {
              "msg": "sdd 1"
              }
              ok: [ceph-host-2] => (item=2) => {
              "msg": "sdd 2"
              }
              ok: [ceph-host-2] => (item=3) => {
              "msg": "sdd 3"
              }
              ok: [ceph-host-2] => (item=4) => {
              "msg": "sdd 4"
              }

              TASK [osd : debug]
              ok: [ceph-host-2] => (item=1) => {
              "msg": "sdb 1"
              }
              ok: [ceph-host-2] => (item=2) => {
              "msg": "sdb 2"
              }

              TASK [osd : debug]
              ok: [ceph-host-2] => (item=1) => {
              "msg": "sdc 1"
              }
              ok: [ceph-host-2] => (item=2) => {
              "msg": "sdc 2"
              }
              ok: [ceph-host-2] => (item=3) => {
              "msg": "sdc 3"
              }


              main.yml



              - include_tasks: inner.yml
              loop: "{{ device.keys() }}"
              loop_control:
              loop_var: outer_item


              inner.yml



              - debug:
              msg: "{{ outer_item }} {{ item }}"
              with_sequence: count={{ device[outer_item] }}





              share|improve this answer


























                0














                Finally, I got the solution but I changed the format of the variable, however. The variable is changed to:



                device:
                sdb: 2
                sdc: 3
                sdd: 4


                The result is:



                ok: [ceph-host-2] => (item=1) => {
                "msg": "sdd 1"
                }
                ok: [ceph-host-2] => (item=2) => {
                "msg": "sdd 2"
                }
                ok: [ceph-host-2] => (item=3) => {
                "msg": "sdd 3"
                }
                ok: [ceph-host-2] => (item=4) => {
                "msg": "sdd 4"
                }

                TASK [osd : debug]
                ok: [ceph-host-2] => (item=1) => {
                "msg": "sdb 1"
                }
                ok: [ceph-host-2] => (item=2) => {
                "msg": "sdb 2"
                }

                TASK [osd : debug]
                ok: [ceph-host-2] => (item=1) => {
                "msg": "sdc 1"
                }
                ok: [ceph-host-2] => (item=2) => {
                "msg": "sdc 2"
                }
                ok: [ceph-host-2] => (item=3) => {
                "msg": "sdc 3"
                }


                main.yml



                - include_tasks: inner.yml
                loop: "{{ device.keys() }}"
                loop_control:
                loop_var: outer_item


                inner.yml



                - debug:
                msg: "{{ outer_item }} {{ item }}"
                with_sequence: count={{ device[outer_item] }}





                share|improve this answer
























                  0












                  0








                  0






                  Finally, I got the solution but I changed the format of the variable, however. The variable is changed to:



                  device:
                  sdb: 2
                  sdc: 3
                  sdd: 4


                  The result is:



                  ok: [ceph-host-2] => (item=1) => {
                  "msg": "sdd 1"
                  }
                  ok: [ceph-host-2] => (item=2) => {
                  "msg": "sdd 2"
                  }
                  ok: [ceph-host-2] => (item=3) => {
                  "msg": "sdd 3"
                  }
                  ok: [ceph-host-2] => (item=4) => {
                  "msg": "sdd 4"
                  }

                  TASK [osd : debug]
                  ok: [ceph-host-2] => (item=1) => {
                  "msg": "sdb 1"
                  }
                  ok: [ceph-host-2] => (item=2) => {
                  "msg": "sdb 2"
                  }

                  TASK [osd : debug]
                  ok: [ceph-host-2] => (item=1) => {
                  "msg": "sdc 1"
                  }
                  ok: [ceph-host-2] => (item=2) => {
                  "msg": "sdc 2"
                  }
                  ok: [ceph-host-2] => (item=3) => {
                  "msg": "sdc 3"
                  }


                  main.yml



                  - include_tasks: inner.yml
                  loop: "{{ device.keys() }}"
                  loop_control:
                  loop_var: outer_item


                  inner.yml



                  - debug:
                  msg: "{{ outer_item }} {{ item }}"
                  with_sequence: count={{ device[outer_item] }}





                  share|improve this answer












                  Finally, I got the solution but I changed the format of the variable, however. The variable is changed to:



                  device:
                  sdb: 2
                  sdc: 3
                  sdd: 4


                  The result is:



                  ok: [ceph-host-2] => (item=1) => {
                  "msg": "sdd 1"
                  }
                  ok: [ceph-host-2] => (item=2) => {
                  "msg": "sdd 2"
                  }
                  ok: [ceph-host-2] => (item=3) => {
                  "msg": "sdd 3"
                  }
                  ok: [ceph-host-2] => (item=4) => {
                  "msg": "sdd 4"
                  }

                  TASK [osd : debug]
                  ok: [ceph-host-2] => (item=1) => {
                  "msg": "sdb 1"
                  }
                  ok: [ceph-host-2] => (item=2) => {
                  "msg": "sdb 2"
                  }

                  TASK [osd : debug]
                  ok: [ceph-host-2] => (item=1) => {
                  "msg": "sdc 1"
                  }
                  ok: [ceph-host-2] => (item=2) => {
                  "msg": "sdc 2"
                  }
                  ok: [ceph-host-2] => (item=3) => {
                  "msg": "sdc 3"
                  }


                  main.yml



                  - include_tasks: inner.yml
                  loop: "{{ device.keys() }}"
                  loop_control:
                  loop_var: outer_item


                  inner.yml



                  - debug:
                  msg: "{{ outer_item }} {{ item }}"
                  with_sequence: count={{ device[outer_item] }}






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 at 8:49









                  Cheng Shuai

                  12




                  12























                      0














                      The variable is:



                      device:
                      - sdb: 2
                      - sdc: 3
                      - sdd: 4


                      main.yml



                      - include_tasks: inner.yml
                      loop: "{{ device }}"
                      loop_control:
                      loop_var: outer_item


                      inner.yml



                      - debug:
                      msg: "{{ outer_item.keys()[0] }} {{ item }}"
                      with_sequence: count={{ outer_item.values()[0] }}





                      share|improve this answer


























                        0














                        The variable is:



                        device:
                        - sdb: 2
                        - sdc: 3
                        - sdd: 4


                        main.yml



                        - include_tasks: inner.yml
                        loop: "{{ device }}"
                        loop_control:
                        loop_var: outer_item


                        inner.yml



                        - debug:
                        msg: "{{ outer_item.keys()[0] }} {{ item }}"
                        with_sequence: count={{ outer_item.values()[0] }}





                        share|improve this answer
























                          0












                          0








                          0






                          The variable is:



                          device:
                          - sdb: 2
                          - sdc: 3
                          - sdd: 4


                          main.yml



                          - include_tasks: inner.yml
                          loop: "{{ device }}"
                          loop_control:
                          loop_var: outer_item


                          inner.yml



                          - debug:
                          msg: "{{ outer_item.keys()[0] }} {{ item }}"
                          with_sequence: count={{ outer_item.values()[0] }}





                          share|improve this answer












                          The variable is:



                          device:
                          - sdb: 2
                          - sdc: 3
                          - sdd: 4


                          main.yml



                          - include_tasks: inner.yml
                          loop: "{{ device }}"
                          loop_control:
                          loop_var: outer_item


                          inner.yml



                          - debug:
                          msg: "{{ outer_item.keys()[0] }} {{ item }}"
                          with_sequence: count={{ outer_item.values()[0] }}






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 21 at 11:52









                          Cheng Shuai

                          12




                          12






























                              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%2f53404545%2fansible-nested-loops%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

                              404 Error Contact Form 7 ajax form submitting

                              How to know if a Active Directory user can login interactively

                              Refactoring coordinates for Minecraft Pi buildings written in Python