Search code examples
network-programmingansibleciscoansible-inventory

Create a list within Ansible var file, and have all items be called for reference in plays


Sorry If this has been asked before. I've looked all over and can't quite seem to find anything on it. I'm relatively new to the ansible/coding world, so I apologize ahead of time if my terminology is incorrect.

So, I want to take a list of IP addresses, and assign them all to one variable. As an example, write a host_var file, create one variable, and have a bunch of items called to that one variable.

prefixes:  
  - 192.168.1.1/24  
  - 192.168.1.2/24  
  - 192.168.1.3/24

Then, within the playbook, I want to run a command, and have the output of that command be compared to every line within that {{ prefixes }} variable.

Is this possible? How would this be done? If so, can you also limit it to specific items within that variable?

Thank you!


Solution

  • You already defined the variable in your example.

    Just continue your playbook and use {{ with_items }}. Also read the documentation here.

    ---
      - name: Test Playbook
        hosts: localhost
        gather_facts: false
    
        vars:
          prefixes:
            - 192.168.1.1/24
            - 192.168.1.2/24
            - 192.168.1.3/24
    
        tasks:
          - name: Debug prefixes
            debug:
              msg: "{{ item }}"
            with_items: "{{ prefixes }}"