-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
Pete
  • 35
  • 2
  • 9

2 Answers2

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 }}"
Anant Naugai
  • 538
  • 4
  • 14
  • 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 01: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 02: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 02: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 }}"
Pete
  • 35
  • 2
  • 9