How to loop over all elements of a list when elements are not known in advance?

Hello friends

I’ve some lists of elements which contains values like plugin: “someplugin”, directive: “somedirective”. For reference see

Variables: https://github.com/valentinogagliardi/collectd/blob/master/README.md#example-playbook

Template: https://github.com/valentinogagliardi/collectd/blob/master/templates/collectd.conf.j2

At this moment I’m able to generate a configuration which take this form:

<Plugin “someplugin”>
somedirective

<Plugin “anotherplugin”>
anothersomedirective

What if I want to add more directives to the list?

plugins:

  • { plugin: “someplugin”, directive: “somedirective” }
  • { plugin: “anotherplugin”, directive: “somedirective”, directive_x: “anotherdirective” }

Is not clear to me how to loop over all elements when elements are not known in advance, in order to generate a configuration like this:

<Plugin “someplugin”>
somedirective

<Plugin “anotherplugin”>
somedirective
anotherdirective

Thanks in advance!

For dictionaries, you can use “.keys()” to get all of them. There is also “.iteritems()” which returns a tuple of both the key and the value.

Sounds interesting! Thanks for the suggestion!