Looping in hostvars












0














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



























    0














    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

























      0












      0








      0







      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 at 0:42









      Pete

      125




      125
























          1 Answer
          1






          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 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 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 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 at 2:31











          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

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          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 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 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 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 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 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 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 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 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 at 1:16









          Anant Naugai

          19029




          19029












          • 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 at 2:31


















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

          Feedback on college project

          Futebolista

          Albești (Vaslui)