Templates, loops, dynamically list 1 or more lines

36 views
Skip to first unread message

jepper

unread,
Jul 3, 2014, 7:46:26 AM7/3/14
to ansible...@googlegroups.com

I have to write a template so that I can produce the following - queue physicalName can vary in quantity, and may have 1 2 or 3 entries typically. This seems to be the challenge. I can do a more static version with - { 'x':'y', 'z':'a' } but then I can't figure out how to dynamically list more than 1 entry for physicalName without affecting every line in my output - I have 30 queues to set up like this.

                    <compositeQueue name="1P.EMAIL" forwardOnly="false">
                        <forwardTo>
                            <queue physicalName="1P.LOGSTASH"/>
                        </forwardTo>
                    </compositeQueue>
                    <compositeQueue name="1P.IP_EVT" forwardOnly="false">
                        <forwardTo>
                            <queue physicalName="1P.LOGSTASH"/>
                            <queue physicalName="1P.SOMETHING"/>
                        </forwardTo>
                    </compositeQueue>

                    <compositeQueue name="1P.IP_BLAH" forwardOnly="false">
                        <forwardTo>
                            <queue physicalName="1P.LOGSTASH"/>
                            <queue physicalName="1P.FOO"/>
                            <queue physicalName="1P.BAR"/>
                        </forwardTo>
                    </compositeQueue>

I'm at loss how to do this. I could paste numerous examples of what I've done, but I feel it's a tad pointless as I feel I'm so far away from having something remotely credible. Sorry!

Michael DeHaan

unread,
Jul 3, 2014, 9:46:51 AM7/3/14
to ansible...@googlegroups.com
Jinja2 questions should be pretty easy, though to understand better, what are the variable inputs coming into the template?




--
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/827ccfb5-5751-4fb7-892e-09f0aea5db71%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jepper

unread,
Jul 3, 2014, 9:57:06 AM7/3/14
to ansible...@googlegroups.com
I'm frequently finding myself at lost in jinja2. Petros example worked ( i can't see it here!?!) , and I'm back on track - thanks!

The composite queues are fuse mq queues our infrastructure can subscribe to. Physical queues forward copies of messages, logstash or mule for example. 

Michael DeHaan

unread,
Jul 3, 2014, 3:59:56 PM7/3/14
to ansible...@googlegroups.com
Without some examples of what the variables are I'm afraid I can't provide further help.

The looping and such is pretty straightforward so it would be easy to offer examples.




jepper

unread,
Jul 3, 2014, 4:07:21 PM7/3/14
to ansible...@googlegroups.com
It seems the response I'm referring to is not on the thread - here it is:

"You can define your data like this:

queues:
  logstash:
    name: 1P.LOGSTASH
  something:
    name: 1P.SOMETHING
  foo:
    name: 1P.FOO
  bar:
    name: 1P.BAR

composite_queues:
  - name: 1P.EMAIL
    queues: 
      - logstash
  - name: 1P.IP_EVT
    queues:
      - logstash
      - something
  - name: 1P.IP_BLAH
    queues:
      - logstash
      - foo
      - bar

Then your template would look like this:

{% for composite_queue in composite_queues %}
<compositeQueue name="{{ composite_queue.name }}" forwardOnly="false">
  <forwardTo>
  {% for queue in composite_queue.queues %}
    
<queue physicalName="{{ queues[queue].name }}"/>
  {% endfor %}
  </forwardTo>
</compositeQueue>
{% endfor %}

Reply all
Reply to author
Forward
0 new messages