Ansible loop till the condition matches.












1














I want to make the series of API calls, after each call check for a particular parameter in the result, if it greater than particular value save it in register and continue further playbook execution.



Basically I am making an API call to RHEV to check the storage domain. Then I want to check if storage domain have the sufficient space, if yes, store that storage domain id in register to create the disk on the storage domain.



Below is the snippet how I do it for single API call to storagedomain.



 - name: Get Free Storage Domain On RHEV
uri:
url: "{{ rhevurl }}/storagedomains/7649aea2-d87c-4066-acca-4399d5261ade"
method: GET
user: "{{ rhevusername }}"
password: "{{ rhevpassword }}"
return_content: yes
headers:
Version: 4
Accept: "application/xml"
Content-type: "application/xml"
register: storagedomain
tags: storagedomain


- name: Retriving size.
xml:
xmlstring: "{{ storagedomain.content }}"
xpath: /storage_domain/available
content: text
register: availablesize
tags: storagedomain


- name: storage_domain size
debug:
var: availablesize.matches[0].available
tags: storagedomain


Now I want to do this process for multiple storage domain, and loop should break when it gets storagedomain with available space.



Something like below.



- name: Get Free Storage Domain On RHEV
uri:
url: "{{ rhevurl }}/storagedomains/{{ item }}"
method: GET
user: "{{ rhevusername }}"
password: "{{ rhevpassword }}"
return_content: yes
headers:
Version: 4
Accept: "application/xml"
Content-type: "application/xml"
loop:
- 7649aea2-d87c-4066-acca-4399d5261ade
- 40cceee7-a8d3-45af-a2d0-70c414be32cc
- a81411b0-4ddb-4467-a4c6-ac9364905248
- b288c547-231c-44b9-8329-98adcbdfc726
- 8cdef991-3edc-4c35-9228-feeef8f29004
- 837a2e1b-6365-4309-a526-0cd05801fe43
- 8981bf82-a1da-405e-a7f5-d84f2c94d71d
- 7a9e3904-e37b-48fd-b850-0f026dc5cde9


In the loop how should I parse xml using xml module and then check for condition of available space greater the particular size










share|improve this question



























    1














    I want to make the series of API calls, after each call check for a particular parameter in the result, if it greater than particular value save it in register and continue further playbook execution.



    Basically I am making an API call to RHEV to check the storage domain. Then I want to check if storage domain have the sufficient space, if yes, store that storage domain id in register to create the disk on the storage domain.



    Below is the snippet how I do it for single API call to storagedomain.



     - name: Get Free Storage Domain On RHEV
    uri:
    url: "{{ rhevurl }}/storagedomains/7649aea2-d87c-4066-acca-4399d5261ade"
    method: GET
    user: "{{ rhevusername }}"
    password: "{{ rhevpassword }}"
    return_content: yes
    headers:
    Version: 4
    Accept: "application/xml"
    Content-type: "application/xml"
    register: storagedomain
    tags: storagedomain


    - name: Retriving size.
    xml:
    xmlstring: "{{ storagedomain.content }}"
    xpath: /storage_domain/available
    content: text
    register: availablesize
    tags: storagedomain


    - name: storage_domain size
    debug:
    var: availablesize.matches[0].available
    tags: storagedomain


    Now I want to do this process for multiple storage domain, and loop should break when it gets storagedomain with available space.



    Something like below.



    - name: Get Free Storage Domain On RHEV
    uri:
    url: "{{ rhevurl }}/storagedomains/{{ item }}"
    method: GET
    user: "{{ rhevusername }}"
    password: "{{ rhevpassword }}"
    return_content: yes
    headers:
    Version: 4
    Accept: "application/xml"
    Content-type: "application/xml"
    loop:
    - 7649aea2-d87c-4066-acca-4399d5261ade
    - 40cceee7-a8d3-45af-a2d0-70c414be32cc
    - a81411b0-4ddb-4467-a4c6-ac9364905248
    - b288c547-231c-44b9-8329-98adcbdfc726
    - 8cdef991-3edc-4c35-9228-feeef8f29004
    - 837a2e1b-6365-4309-a526-0cd05801fe43
    - 8981bf82-a1da-405e-a7f5-d84f2c94d71d
    - 7a9e3904-e37b-48fd-b850-0f026dc5cde9


    In the loop how should I parse xml using xml module and then check for condition of available space greater the particular size










    share|improve this question

























      1












      1








      1







      I want to make the series of API calls, after each call check for a particular parameter in the result, if it greater than particular value save it in register and continue further playbook execution.



      Basically I am making an API call to RHEV to check the storage domain. Then I want to check if storage domain have the sufficient space, if yes, store that storage domain id in register to create the disk on the storage domain.



      Below is the snippet how I do it for single API call to storagedomain.



       - name: Get Free Storage Domain On RHEV
      uri:
      url: "{{ rhevurl }}/storagedomains/7649aea2-d87c-4066-acca-4399d5261ade"
      method: GET
      user: "{{ rhevusername }}"
      password: "{{ rhevpassword }}"
      return_content: yes
      headers:
      Version: 4
      Accept: "application/xml"
      Content-type: "application/xml"
      register: storagedomain
      tags: storagedomain


      - name: Retriving size.
      xml:
      xmlstring: "{{ storagedomain.content }}"
      xpath: /storage_domain/available
      content: text
      register: availablesize
      tags: storagedomain


      - name: storage_domain size
      debug:
      var: availablesize.matches[0].available
      tags: storagedomain


      Now I want to do this process for multiple storage domain, and loop should break when it gets storagedomain with available space.



      Something like below.



      - name: Get Free Storage Domain On RHEV
      uri:
      url: "{{ rhevurl }}/storagedomains/{{ item }}"
      method: GET
      user: "{{ rhevusername }}"
      password: "{{ rhevpassword }}"
      return_content: yes
      headers:
      Version: 4
      Accept: "application/xml"
      Content-type: "application/xml"
      loop:
      - 7649aea2-d87c-4066-acca-4399d5261ade
      - 40cceee7-a8d3-45af-a2d0-70c414be32cc
      - a81411b0-4ddb-4467-a4c6-ac9364905248
      - b288c547-231c-44b9-8329-98adcbdfc726
      - 8cdef991-3edc-4c35-9228-feeef8f29004
      - 837a2e1b-6365-4309-a526-0cd05801fe43
      - 8981bf82-a1da-405e-a7f5-d84f2c94d71d
      - 7a9e3904-e37b-48fd-b850-0f026dc5cde9


      In the loop how should I parse xml using xml module and then check for condition of available space greater the particular size










      share|improve this question













      I want to make the series of API calls, after each call check for a particular parameter in the result, if it greater than particular value save it in register and continue further playbook execution.



      Basically I am making an API call to RHEV to check the storage domain. Then I want to check if storage domain have the sufficient space, if yes, store that storage domain id in register to create the disk on the storage domain.



      Below is the snippet how I do it for single API call to storagedomain.



       - name: Get Free Storage Domain On RHEV
      uri:
      url: "{{ rhevurl }}/storagedomains/7649aea2-d87c-4066-acca-4399d5261ade"
      method: GET
      user: "{{ rhevusername }}"
      password: "{{ rhevpassword }}"
      return_content: yes
      headers:
      Version: 4
      Accept: "application/xml"
      Content-type: "application/xml"
      register: storagedomain
      tags: storagedomain


      - name: Retriving size.
      xml:
      xmlstring: "{{ storagedomain.content }}"
      xpath: /storage_domain/available
      content: text
      register: availablesize
      tags: storagedomain


      - name: storage_domain size
      debug:
      var: availablesize.matches[0].available
      tags: storagedomain


      Now I want to do this process for multiple storage domain, and loop should break when it gets storagedomain with available space.



      Something like below.



      - name: Get Free Storage Domain On RHEV
      uri:
      url: "{{ rhevurl }}/storagedomains/{{ item }}"
      method: GET
      user: "{{ rhevusername }}"
      password: "{{ rhevpassword }}"
      return_content: yes
      headers:
      Version: 4
      Accept: "application/xml"
      Content-type: "application/xml"
      loop:
      - 7649aea2-d87c-4066-acca-4399d5261ade
      - 40cceee7-a8d3-45af-a2d0-70c414be32cc
      - a81411b0-4ddb-4467-a4c6-ac9364905248
      - b288c547-231c-44b9-8329-98adcbdfc726
      - 8cdef991-3edc-4c35-9228-feeef8f29004
      - 837a2e1b-6365-4309-a526-0cd05801fe43
      - 8981bf82-a1da-405e-a7f5-d84f2c94d71d
      - 7a9e3904-e37b-48fd-b850-0f026dc5cde9


      In the loop how should I parse xml using xml module and then check for condition of available space greater the particular size







      ansible






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 0:20









      Suraj Patil

      62




      62
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You cannot break the loop, but can skip the loop execution if your condition satisfy at any item. check out the below example.



          test.yml this playbook will execute shell module with ignore errors & set var1 variable. but the block module will only execute until var1 is not defined.



          - block:
          - shell: expr {{item}} + 1
          ignore_errors: yes
          register: cmd_stat

          - set_fact: var1={{item}}
          when: cmd_stat.rc == 0
          when: var1 is not defined


          sites.yml this play includes test.yml playbook multiple times based on your loops items.



          ---
          - hosts: localhost
          connection: local
          vars:
          tasks:
          - include: test.yml
          loop: ["abc","def", "ghi",1, "jkl"]

          - name: increase var1 variable by 1 and write to text file
          shell: expr {{var1}} + 1 > text


          So, using the same logic you can implement in your playbook. like if url get 200 status then set storage variable & use it with when condition.



          I Hope you'll be able to understand the example.






          share|improve this answer





















          • Thank you. I guess your logic will work for me. I will test it and will let you know.
            – Suraj Patil
            Nov 14 '18 at 9:49



















          0














          until: with retries: (and perhaps delay:) or async: (which has its own section in the fine manual) with poll: might do what you want






          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%2f53291393%2fansible-loop-till-the-condition-matches%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









            1














            You cannot break the loop, but can skip the loop execution if your condition satisfy at any item. check out the below example.



            test.yml this playbook will execute shell module with ignore errors & set var1 variable. but the block module will only execute until var1 is not defined.



            - block:
            - shell: expr {{item}} + 1
            ignore_errors: yes
            register: cmd_stat

            - set_fact: var1={{item}}
            when: cmd_stat.rc == 0
            when: var1 is not defined


            sites.yml this play includes test.yml playbook multiple times based on your loops items.



            ---
            - hosts: localhost
            connection: local
            vars:
            tasks:
            - include: test.yml
            loop: ["abc","def", "ghi",1, "jkl"]

            - name: increase var1 variable by 1 and write to text file
            shell: expr {{var1}} + 1 > text


            So, using the same logic you can implement in your playbook. like if url get 200 status then set storage variable & use it with when condition.



            I Hope you'll be able to understand the example.






            share|improve this answer





















            • Thank you. I guess your logic will work for me. I will test it and will let you know.
              – Suraj Patil
              Nov 14 '18 at 9:49
















            1














            You cannot break the loop, but can skip the loop execution if your condition satisfy at any item. check out the below example.



            test.yml this playbook will execute shell module with ignore errors & set var1 variable. but the block module will only execute until var1 is not defined.



            - block:
            - shell: expr {{item}} + 1
            ignore_errors: yes
            register: cmd_stat

            - set_fact: var1={{item}}
            when: cmd_stat.rc == 0
            when: var1 is not defined


            sites.yml this play includes test.yml playbook multiple times based on your loops items.



            ---
            - hosts: localhost
            connection: local
            vars:
            tasks:
            - include: test.yml
            loop: ["abc","def", "ghi",1, "jkl"]

            - name: increase var1 variable by 1 and write to text file
            shell: expr {{var1}} + 1 > text


            So, using the same logic you can implement in your playbook. like if url get 200 status then set storage variable & use it with when condition.



            I Hope you'll be able to understand the example.






            share|improve this answer





















            • Thank you. I guess your logic will work for me. I will test it and will let you know.
              – Suraj Patil
              Nov 14 '18 at 9:49














            1












            1








            1






            You cannot break the loop, but can skip the loop execution if your condition satisfy at any item. check out the below example.



            test.yml this playbook will execute shell module with ignore errors & set var1 variable. but the block module will only execute until var1 is not defined.



            - block:
            - shell: expr {{item}} + 1
            ignore_errors: yes
            register: cmd_stat

            - set_fact: var1={{item}}
            when: cmd_stat.rc == 0
            when: var1 is not defined


            sites.yml this play includes test.yml playbook multiple times based on your loops items.



            ---
            - hosts: localhost
            connection: local
            vars:
            tasks:
            - include: test.yml
            loop: ["abc","def", "ghi",1, "jkl"]

            - name: increase var1 variable by 1 and write to text file
            shell: expr {{var1}} + 1 > text


            So, using the same logic you can implement in your playbook. like if url get 200 status then set storage variable & use it with when condition.



            I Hope you'll be able to understand the example.






            share|improve this answer












            You cannot break the loop, but can skip the loop execution if your condition satisfy at any item. check out the below example.



            test.yml this playbook will execute shell module with ignore errors & set var1 variable. but the block module will only execute until var1 is not defined.



            - block:
            - shell: expr {{item}} + 1
            ignore_errors: yes
            register: cmd_stat

            - set_fact: var1={{item}}
            when: cmd_stat.rc == 0
            when: var1 is not defined


            sites.yml this play includes test.yml playbook multiple times based on your loops items.



            ---
            - hosts: localhost
            connection: local
            vars:
            tasks:
            - include: test.yml
            loop: ["abc","def", "ghi",1, "jkl"]

            - name: increase var1 variable by 1 and write to text file
            shell: expr {{var1}} + 1 > text


            So, using the same logic you can implement in your playbook. like if url get 200 status then set storage variable & use it with when condition.



            I Hope you'll be able to understand the example.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 14 '18 at 7:29









            user2983509

            345




            345












            • Thank you. I guess your logic will work for me. I will test it and will let you know.
              – Suraj Patil
              Nov 14 '18 at 9:49


















            • Thank you. I guess your logic will work for me. I will test it and will let you know.
              – Suraj Patil
              Nov 14 '18 at 9:49
















            Thank you. I guess your logic will work for me. I will test it and will let you know.
            – Suraj Patil
            Nov 14 '18 at 9:49




            Thank you. I guess your logic will work for me. I will test it and will let you know.
            – Suraj Patil
            Nov 14 '18 at 9:49













            0














            until: with retries: (and perhaps delay:) or async: (which has its own section in the fine manual) with poll: might do what you want






            share|improve this answer


























              0














              until: with retries: (and perhaps delay:) or async: (which has its own section in the fine manual) with poll: might do what you want






              share|improve this answer
























                0












                0








                0






                until: with retries: (and perhaps delay:) or async: (which has its own section in the fine manual) with poll: might do what you want






                share|improve this answer












                until: with retries: (and perhaps delay:) or async: (which has its own section in the fine manual) with poll: might do what you want







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 6:00









                Matthew L Daniel

                7,62112326




                7,62112326






























                    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%2f53291393%2fansible-loop-till-the-condition-matches%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