Looping in hostvars












-1















I'm wondering if it is possible to perform a loop in the hostvars folder when using Ansible?



Here is what I've tried but haven't had success in making it work - or is it just not possible to do?



---

list_pool: 'list ltm pool {{ items }}'
with_items:
- 'abc123'
- 'def456'


I would use the "list_pool" variable in a playbook afterward:



  - name: List pool
bigip_command:
server: "{{ some_server }}"
user: "{{ some_user }}"
password: "{{ some_password }}"
commands:
- "{{ list_pool }}"
validate_certs: no
delegate_to: localhost









share|improve this question



























    -1















    I'm wondering if it is possible to perform a loop in the hostvars folder when using Ansible?



    Here is what I've tried but haven't had success in making it work - or is it just not possible to do?



    ---

    list_pool: 'list ltm pool {{ items }}'
    with_items:
    - 'abc123'
    - 'def456'


    I would use the "list_pool" variable in a playbook afterward:



      - name: List pool
    bigip_command:
    server: "{{ some_server }}"
    user: "{{ some_user }}"
    password: "{{ some_password }}"
    commands:
    - "{{ list_pool }}"
    validate_certs: no
    delegate_to: localhost









    share|improve this question

























      -1












      -1








      -1








      I'm wondering if it is possible to perform a loop in the hostvars folder when using Ansible?



      Here is what I've tried but haven't had success in making it work - or is it just not possible to do?



      ---

      list_pool: 'list ltm pool {{ items }}'
      with_items:
      - 'abc123'
      - 'def456'


      I would use the "list_pool" variable in a playbook afterward:



        - name: List pool
      bigip_command:
      server: "{{ some_server }}"
      user: "{{ some_user }}"
      password: "{{ some_password }}"
      commands:
      - "{{ list_pool }}"
      validate_certs: no
      delegate_to: localhost









      share|improve this question














      I'm wondering if it is possible to perform a loop in the hostvars folder when using Ansible?



      Here is what I've tried but haven't had success in making it work - or is it just not possible to do?



      ---

      list_pool: 'list ltm pool {{ items }}'
      with_items:
      - 'abc123'
      - 'def456'


      I would use the "list_pool" variable in a playbook afterward:



        - name: List pool
      bigip_command:
      server: "{{ some_server }}"
      user: "{{ some_user }}"
      password: "{{ some_password }}"
      commands:
      - "{{ list_pool }}"
      validate_certs: no
      delegate_to: localhost






      ansible f5






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 0:42









      PetePete

      105




      105
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Not sure what you mean when you say you want to loop over hostvars folder.
          From what I can interpret from your tasks is: "You need to execute big-ip command list ltm <pool-name> for multiple pools in the list list_pool"



          If that's what you're after, this should work:



          - name: Set list_pool fact
          set_fact:
          list_pool: "{{ list_pool | default() + [item] }}"
          with_items:
          - 'abc123'
          - 'def456'

          - name: List pool
          bigip_command:
          server: "{{ some_server }}"
          user: "{{ some_user }}"
          password: "{{ some_password }}"
          commands:
          - "list ltm {{ item }}"
          validate_certs: no
          delegate_to: localhost
          with_items: "{{ list_pool }}"





          share|improve this answer
























          • Thanks for this. I'd like to have the "list_pool" variable placed in the hostvars folder, iterate over multiple pool names, and have that variable ("list_pool") referenced in a playbook with the task I outlined using the bigip_command module.

            – Pete
            Nov 21 '18 at 1:49











          • Do you also have multiple hosts? If yes then you can reference list_pools variable for a particular host by : "hostvars['inventory_hostname']['list_pools']" If not, then you can define this list variable in group_vars/all.yml and just use the variable as is.

            – Anant Naugai
            Nov 21 '18 at 2:40













          • So would I be placing "hostvars['inventory_hostname']['list_pools']" where I have '- "{{ list_pool }}"' above, in the "bigip_command" task?

            – Pete
            Nov 21 '18 at 14:50











          • Yes precisely. But, that will work only if you're executing this playbook for/on all the hosts you've defined this variable for.

            – Anant Naugai
            Nov 26 '18 at 2:31



















          0














          I got this working with the following solution:



          hostvars file would look like this:



          ---    
          pre_checks:
          checks:
          pool:
          - name: "pool_123"
          - name: "pool_456"
          ...


          And the play would look like this:



          --output truncated---

          - name: Fetch device host_vars
          set_fact:
          device_config: "{{ ((lookup('file','{{playbook_dir}}/host_vars/{{inventory_hostname}}.yml')) | from_yaml) }}"

          - name: Check pool
          bigip_command:
          server: "{{ inventory_hostname }}"
          user: "{{ remote_username }}"
          password: "{{ remote_passwd }}"
          commands:
          - "list ltm pool {{ item }}"
          validate_certs: no
          with_items:
          - "{{ device_config.pre_checks | json_query('checks.pool[*].name') }}"
          delegate_to: localhost
          when: "'active' in Active_LTM['stdout'][0]"
          register: Pre_Checks

          - name: Verify pool
          debug: var=item
          with_items: "{{ Pre_Checks.results | map(attribute='stdout_lines') | list }}"





          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%2f53403735%2flooping-in-hostvars%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









            0














            Not sure what you mean when you say you want to loop over hostvars folder.
            From what I can interpret from your tasks is: "You need to execute big-ip command list ltm <pool-name> for multiple pools in the list list_pool"



            If that's what you're after, this should work:



            - name: Set list_pool fact
            set_fact:
            list_pool: "{{ list_pool | default() + [item] }}"
            with_items:
            - 'abc123'
            - 'def456'

            - name: List pool
            bigip_command:
            server: "{{ some_server }}"
            user: "{{ some_user }}"
            password: "{{ some_password }}"
            commands:
            - "list ltm {{ item }}"
            validate_certs: no
            delegate_to: localhost
            with_items: "{{ list_pool }}"





            share|improve this answer
























            • Thanks for this. I'd like to have the "list_pool" variable placed in the hostvars folder, iterate over multiple pool names, and have that variable ("list_pool") referenced in a playbook with the task I outlined using the bigip_command module.

              – Pete
              Nov 21 '18 at 1:49











            • Do you also have multiple hosts? If yes then you can reference list_pools variable for a particular host by : "hostvars['inventory_hostname']['list_pools']" If not, then you can define this list variable in group_vars/all.yml and just use the variable as is.

              – Anant Naugai
              Nov 21 '18 at 2:40













            • So would I be placing "hostvars['inventory_hostname']['list_pools']" where I have '- "{{ list_pool }}"' above, in the "bigip_command" task?

              – Pete
              Nov 21 '18 at 14:50











            • Yes precisely. But, that will work only if you're executing this playbook for/on all the hosts you've defined this variable for.

              – Anant Naugai
              Nov 26 '18 at 2:31
















            0














            Not sure what you mean when you say you want to loop over hostvars folder.
            From what I can interpret from your tasks is: "You need to execute big-ip command list ltm <pool-name> for multiple pools in the list list_pool"



            If that's what you're after, this should work:



            - name: Set list_pool fact
            set_fact:
            list_pool: "{{ list_pool | default() + [item] }}"
            with_items:
            - 'abc123'
            - 'def456'

            - name: List pool
            bigip_command:
            server: "{{ some_server }}"
            user: "{{ some_user }}"
            password: "{{ some_password }}"
            commands:
            - "list ltm {{ item }}"
            validate_certs: no
            delegate_to: localhost
            with_items: "{{ list_pool }}"





            share|improve this answer
























            • Thanks for this. I'd like to have the "list_pool" variable placed in the hostvars folder, iterate over multiple pool names, and have that variable ("list_pool") referenced in a playbook with the task I outlined using the bigip_command module.

              – Pete
              Nov 21 '18 at 1:49











            • Do you also have multiple hosts? If yes then you can reference list_pools variable for a particular host by : "hostvars['inventory_hostname']['list_pools']" If not, then you can define this list variable in group_vars/all.yml and just use the variable as is.

              – Anant Naugai
              Nov 21 '18 at 2:40













            • So would I be placing "hostvars['inventory_hostname']['list_pools']" where I have '- "{{ list_pool }}"' above, in the "bigip_command" task?

              – Pete
              Nov 21 '18 at 14:50











            • Yes precisely. But, that will work only if you're executing this playbook for/on all the hosts you've defined this variable for.

              – Anant Naugai
              Nov 26 '18 at 2:31














            0












            0








            0







            Not sure what you mean when you say you want to loop over hostvars folder.
            From what I can interpret from your tasks is: "You need to execute big-ip command list ltm <pool-name> for multiple pools in the list list_pool"



            If that's what you're after, this should work:



            - name: Set list_pool fact
            set_fact:
            list_pool: "{{ list_pool | default() + [item] }}"
            with_items:
            - 'abc123'
            - 'def456'

            - name: List pool
            bigip_command:
            server: "{{ some_server }}"
            user: "{{ some_user }}"
            password: "{{ some_password }}"
            commands:
            - "list ltm {{ item }}"
            validate_certs: no
            delegate_to: localhost
            with_items: "{{ list_pool }}"





            share|improve this answer













            Not sure what you mean when you say you want to loop over hostvars folder.
            From what I can interpret from your tasks is: "You need to execute big-ip command list ltm <pool-name> for multiple pools in the list list_pool"



            If that's what you're after, this should work:



            - name: Set list_pool fact
            set_fact:
            list_pool: "{{ list_pool | default() + [item] }}"
            with_items:
            - 'abc123'
            - 'def456'

            - name: List pool
            bigip_command:
            server: "{{ some_server }}"
            user: "{{ some_user }}"
            password: "{{ some_password }}"
            commands:
            - "list ltm {{ item }}"
            validate_certs: no
            delegate_to: localhost
            with_items: "{{ list_pool }}"






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 '18 at 1:16









            Anant NaugaiAnant Naugai

            34039




            34039













            • Thanks for this. I'd like to have the "list_pool" variable placed in the hostvars folder, iterate over multiple pool names, and have that variable ("list_pool") referenced in a playbook with the task I outlined using the bigip_command module.

              – Pete
              Nov 21 '18 at 1:49











            • Do you also have multiple hosts? If yes then you can reference list_pools variable for a particular host by : "hostvars['inventory_hostname']['list_pools']" If not, then you can define this list variable in group_vars/all.yml and just use the variable as is.

              – Anant Naugai
              Nov 21 '18 at 2:40













            • So would I be placing "hostvars['inventory_hostname']['list_pools']" where I have '- "{{ list_pool }}"' above, in the "bigip_command" task?

              – Pete
              Nov 21 '18 at 14:50











            • Yes precisely. But, that will work only if you're executing this playbook for/on all the hosts you've defined this variable for.

              – Anant Naugai
              Nov 26 '18 at 2:31



















            • Thanks for this. I'd like to have the "list_pool" variable placed in the hostvars folder, iterate over multiple pool names, and have that variable ("list_pool") referenced in a playbook with the task I outlined using the bigip_command module.

              – Pete
              Nov 21 '18 at 1:49











            • Do you also have multiple hosts? If yes then you can reference list_pools variable for a particular host by : "hostvars['inventory_hostname']['list_pools']" If not, then you can define this list variable in group_vars/all.yml and just use the variable as is.

              – Anant Naugai
              Nov 21 '18 at 2:40













            • So would I be placing "hostvars['inventory_hostname']['list_pools']" where I have '- "{{ list_pool }}"' above, in the "bigip_command" task?

              – Pete
              Nov 21 '18 at 14:50











            • Yes precisely. But, that will work only if you're executing this playbook for/on all the hosts you've defined this variable for.

              – Anant Naugai
              Nov 26 '18 at 2:31

















            Thanks for this. I'd like to have the "list_pool" variable placed in the hostvars folder, iterate over multiple pool names, and have that variable ("list_pool") referenced in a playbook with the task I outlined using the bigip_command module.

            – Pete
            Nov 21 '18 at 1:49





            Thanks for this. I'd like to have the "list_pool" variable placed in the hostvars folder, iterate over multiple pool names, and have that variable ("list_pool") referenced in a playbook with the task I outlined using the bigip_command module.

            – Pete
            Nov 21 '18 at 1:49













            Do you also have multiple hosts? If yes then you can reference list_pools variable for a particular host by : "hostvars['inventory_hostname']['list_pools']" If not, then you can define this list variable in group_vars/all.yml and just use the variable as is.

            – Anant Naugai
            Nov 21 '18 at 2:40







            Do you also have multiple hosts? If yes then you can reference list_pools variable for a particular host by : "hostvars['inventory_hostname']['list_pools']" If not, then you can define this list variable in group_vars/all.yml and just use the variable as is.

            – Anant Naugai
            Nov 21 '18 at 2:40















            So would I be placing "hostvars['inventory_hostname']['list_pools']" where I have '- "{{ list_pool }}"' above, in the "bigip_command" task?

            – Pete
            Nov 21 '18 at 14:50





            So would I be placing "hostvars['inventory_hostname']['list_pools']" where I have '- "{{ list_pool }}"' above, in the "bigip_command" task?

            – Pete
            Nov 21 '18 at 14:50













            Yes precisely. But, that will work only if you're executing this playbook for/on all the hosts you've defined this variable for.

            – Anant Naugai
            Nov 26 '18 at 2:31





            Yes precisely. But, that will work only if you're executing this playbook for/on all the hosts you've defined this variable for.

            – Anant Naugai
            Nov 26 '18 at 2:31













            0














            I got this working with the following solution:



            hostvars file would look like this:



            ---    
            pre_checks:
            checks:
            pool:
            - name: "pool_123"
            - name: "pool_456"
            ...


            And the play would look like this:



            --output truncated---

            - name: Fetch device host_vars
            set_fact:
            device_config: "{{ ((lookup('file','{{playbook_dir}}/host_vars/{{inventory_hostname}}.yml')) | from_yaml) }}"

            - name: Check pool
            bigip_command:
            server: "{{ inventory_hostname }}"
            user: "{{ remote_username }}"
            password: "{{ remote_passwd }}"
            commands:
            - "list ltm pool {{ item }}"
            validate_certs: no
            with_items:
            - "{{ device_config.pre_checks | json_query('checks.pool[*].name') }}"
            delegate_to: localhost
            when: "'active' in Active_LTM['stdout'][0]"
            register: Pre_Checks

            - name: Verify pool
            debug: var=item
            with_items: "{{ Pre_Checks.results | map(attribute='stdout_lines') | list }}"





            share|improve this answer




























              0














              I got this working with the following solution:



              hostvars file would look like this:



              ---    
              pre_checks:
              checks:
              pool:
              - name: "pool_123"
              - name: "pool_456"
              ...


              And the play would look like this:



              --output truncated---

              - name: Fetch device host_vars
              set_fact:
              device_config: "{{ ((lookup('file','{{playbook_dir}}/host_vars/{{inventory_hostname}}.yml')) | from_yaml) }}"

              - name: Check pool
              bigip_command:
              server: "{{ inventory_hostname }}"
              user: "{{ remote_username }}"
              password: "{{ remote_passwd }}"
              commands:
              - "list ltm pool {{ item }}"
              validate_certs: no
              with_items:
              - "{{ device_config.pre_checks | json_query('checks.pool[*].name') }}"
              delegate_to: localhost
              when: "'active' in Active_LTM['stdout'][0]"
              register: Pre_Checks

              - name: Verify pool
              debug: var=item
              with_items: "{{ Pre_Checks.results | map(attribute='stdout_lines') | list }}"





              share|improve this answer


























                0












                0








                0







                I got this working with the following solution:



                hostvars file would look like this:



                ---    
                pre_checks:
                checks:
                pool:
                - name: "pool_123"
                - name: "pool_456"
                ...


                And the play would look like this:



                --output truncated---

                - name: Fetch device host_vars
                set_fact:
                device_config: "{{ ((lookup('file','{{playbook_dir}}/host_vars/{{inventory_hostname}}.yml')) | from_yaml) }}"

                - name: Check pool
                bigip_command:
                server: "{{ inventory_hostname }}"
                user: "{{ remote_username }}"
                password: "{{ remote_passwd }}"
                commands:
                - "list ltm pool {{ item }}"
                validate_certs: no
                with_items:
                - "{{ device_config.pre_checks | json_query('checks.pool[*].name') }}"
                delegate_to: localhost
                when: "'active' in Active_LTM['stdout'][0]"
                register: Pre_Checks

                - name: Verify pool
                debug: var=item
                with_items: "{{ Pre_Checks.results | map(attribute='stdout_lines') | list }}"





                share|improve this answer













                I got this working with the following solution:



                hostvars file would look like this:



                ---    
                pre_checks:
                checks:
                pool:
                - name: "pool_123"
                - name: "pool_456"
                ...


                And the play would look like this:



                --output truncated---

                - name: Fetch device host_vars
                set_fact:
                device_config: "{{ ((lookup('file','{{playbook_dir}}/host_vars/{{inventory_hostname}}.yml')) | from_yaml) }}"

                - name: Check pool
                bigip_command:
                server: "{{ inventory_hostname }}"
                user: "{{ remote_username }}"
                password: "{{ remote_passwd }}"
                commands:
                - "list ltm pool {{ item }}"
                validate_certs: no
                with_items:
                - "{{ device_config.pre_checks | json_query('checks.pool[*].name') }}"
                delegate_to: localhost
                when: "'active' in Active_LTM['stdout'][0]"
                register: Pre_Checks

                - name: Verify pool
                debug: var=item
                with_items: "{{ Pre_Checks.results | map(attribute='stdout_lines') | list }}"






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 15 at 19:47









                PetePete

                105




                105






























                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403735%2flooping-in-hostvars%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

                    鏡平學校

                    ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

                    Why https connections are so slow when debugging (stepping over) in Java?