How to do it...

  1. Create a new playbook called ansible_loops.yml inside the ch1_ansible folder.
  2. Inside the group_vars/cisco.yml file, incorporate the following content:
snmp_servers:
- 10.1.1.1
- 10.2.1.1
  1. Inside the group_vars/juniper.yml file, incorporate the following content:
users:
admin: admin123
oper: oper123
  1. Inside the ansible_loops.yml file, incorporate the following content:
---
- name: Ansible Loop over a List
hosts: cisco
gather_facts: no
tasks:
- name: Loop over SNMP Servers
debug:
msg: "Router {{ hostname }} with snmp server {{ item }}"
loop: "{{ snmp_servers}}"

- name: Ansible Loop over a Dictionary
hosts: juniper
gather_facts: no
tasks:
- name: Loop over Username and Passowrds
debug:
msg: "Router {{ hostname }} with user {{ item.key }} password {{ item.value }}"
with_dict: "{{ users}}"
  1. Run the playbook as shown here:
$ ansible-playbook ansible_loops.yml -i hosts