for and if in template

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?

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.