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

46 views
Skip to first unread message

Valentino Gagliardi

unread,
Aug 6, 2014, 6:58:18 AM8/6/14
to ansible...@googlegroups.com
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>

<Plugin "anotherplugin">
anothersomedirective
</Plugin>

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>

<Plugin "anotherplugin">
somedirective
anotherdirective
</Plugin>

Thanks in advance!

Michael DeHaan

unread,
Aug 7, 2014, 5:16:57 PM8/7/14
to ansible...@googlegroups.com
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.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7b0ca845-74bb-4b9b-b0c0-79c376c75f98%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Servermanaged

unread,
Aug 8, 2014, 4:02:11 AM8/8/14
to ansible...@googlegroups.com
Sounds interesting! Thanks for the suggestion!


--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/O_6gyS5DvJ8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages