title: How to Iterate through YAML list/dictionary items while rending Jinja2 template in Python
body: <p>I seem to get stuck either with code or concept and need some help in iterating through YAML based dict/list while rendering Jinja2 Template in Python.
  I am a bit new in programming (3 weeks) so apologies in advance for any blunders.</p>
  
  <p>For instance I have the following dictionary defined in YAML.</p>
  
  <pre><code>---
  - hostname: R1
    interfaces:
      - name: f0/0
        description: This is FastEth 0/0 connected to R2 FastEth 0/0
      - name: f0/1
        description: This is FastEth 0/1 connected to Local Host Loopback
  - hostname: R2
    interfaces:
      - name: f0/0
        description: This is FastEth 0/0 connected to R2 FastEth 0/0
      - name: f0/1
        description: This is FastEth 0/1 connected to Local Host Loopback
  </code></pre>
  
  <p>and the following is the Jinja Template I am rendering:</p>
  
  <pre><code>{% for iface in config.interfaces %}
  int {{ config.name }}
  description {{ config.description }}
  {% endfor %}
  </code></pre>
  
  <p>....
  I have two routers R1 and R2 on which I want to send the rendered configs based on their dictionaries only. I want to generate two config sets, one for each router.
  Hence I thought of doing this is Python but with no luck.</p>
  
  <pre><code>env = Environment(loader=FileSystemLoader('./templates'),trim_blocks=True)
  with open('./YAML/configuration.yml') as _:
      config_commands_var = yaml.load(_)
  
  for device in range(len(devices)):
      print "\nStart time: " + str(datetime.now())
      username = devices[device]['username']
      password = devices[device]['password']
      ip = devices[device]['ip']
      device_type = devices[device]['device_type']
      secret = devices[device]['secret']
      hostname = devices[device]['hostname']
  
      config_commands = template.render(config=config_commands_var)
      push_config_commands(username, password, ip, device_type, secret, config_commands)
  </code></pre>
  
  <p>Here "devices" is a list of dictionaries of devices. and "push_config_commands" function send list of commands at once to any device.</p>
  
  <p>I hope to really get my problem solved here, so will appreciate any sort of help.
  Thanks in advance.</p>