Nested for-loops inside Service apply rules

Hi.

Is there a way to iterate over multiple vars/have nested for loops in an apply rule (Service)?

I have a check for HTTP/HTTPS redirect, which expects different locations depending on the useragent. Also the HTTP/HTTPS check should check multiple domains.
Inside the domain/vhost dict the redirects and useragents are defined, but also the timeouts etc. which I use inside of the Service.
So it would be something like:
apply Service "HTTP useragent" for (var vhost => var config in host.vars.nginx) for (var useragent => var redirect in config.user_agent) { ... }

This does not work obviously.

Thanks in advance

Hello @bxn-lmi!

Have you tried the following?

function useragents(host) {
  var ua = {}

  for (var vhost => var config in host.vars.nginx) {
    for (var useragent => var redirect in config.user_agent) {
      ua[vhost + "-" + useragent + "-" + redirect] = { "vhost" = vhost; "useragent" = useragent }
    }
  }

  return ua
}

apply Service "HTTP useragent" for (var suffix => var config in useragents(host)) { ...

Best,
AK

1 Like