for and if in template

18 views
Skip to first unread message

Joachim Friberg

unread,
May 28, 2014, 8:12:46 AM5/28/14
to ansible...@googlegroups.com
Hi,

I'm trying to get this:
define hostgroup{
  hostgroup_name  
TableswitchesSweden
 members       dlg
-prd-office-tbs01,dlg-prd-office-tbs02,dlg-prd-office-tbs03,dlg-prd-office-tbs04,dlg-prd-office-tbs05,dlg-prd-office-tbs07,dlg-prd-office-tbs08,dlg-prd-office-tbs10,dlg-prd-office-tbs11,dlg-prd-office-tbs12,dlg-prd-office-tbs13,dlg-prd-office-tbs14,dlg-prd-office-tbs15,dlg-prd-office-tbs16,dlg-prd-office-tbs17,dlg-prd-office-tbs18,dlg-prd-office-tbs19,dlg-prd-office-tbs20
}

From this:

extra_hosts:
  - file_name: dlg-prd-office-tbs22
    host_check_name: tbs22
    host_checks: 'http,generic-host,base-notify'
    host_check_ip: 10.0.49.71
    icon_set: network_service
    type: switches
  - file_name: dlg-prd-office-tbs23
    host_check_name: tbs23
    host_checks: 'http,generic-host,base-notify'
    host_check_ip: 10.0.49.72
    icon_set: network_service
    type: switches
  - file_name: dlg-prd-office-tbs24
    host_check_name: tbs24
    host_checks: 'http,generic-host,base-notify'
    host_check_ip: 10.0.49.73
    icon_set: network_service
    type: switches
  - file_name: dlg-prd-office-tbs25
    host_check_name: tbs25
    host_checks: 'http,generic-host,base-notify'
    host_check_ip: 10.0.49.74
    icon_set: network_service
    type: switches

I was thinking about something similar to
...
memebers     {% for unit in extra_hosts[0:] %}{% if "tbs" in unit.
host_check_name %}{{ extra_hosts.file_name }}, {% endif %} {% endfor %}
...


How wrong might I be?

Michael DeHaan

unread,
May 28, 2014, 8:39:57 AM5/28/14
to ansible...@googlegroups.com
It's probably fine.

Jinja2 has some filters like "map" and "reject" but it seems difficult to do something like an "in" query and does not allow Python list comprehensions.

It would be cleaner in most people's templates if it could be done on more than one line, but it seems you cannot.

Others may leverage templates more and may have some other suggestions.




--
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/bace54f9-af6b-4ccd-9dd0-adeb318ea26a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages