Probably everyone using Ansible on OpenBSD figured this out already, but I thought it was quite a nifty application of Jinja templating.

The way pkg.conf is built when using multiple installpath lines is:

installpath = mirror1
installpath += mirror2

The template I settled on is:

{# Magic for handling the '=' vs '+=' #}
{% if 'installpath' in base_openbsd_pkgconf %}
{% set first = True -%}
{% for i in base_openbsd_pkgconf['installpath'] %}
installpath {% if not first %}+{% endif %}= {{ i }}
{% set first = False -%}
{% endfor -%}
{% endif -%}

Where base_openbsd_conf['installpath'] is an array of mirror addresses:

base_openbsd_pkgconf:
  installpath: [ ftp.nluug.nl, mirror.meerval.net ]

Which then nicely renders into the following (surprise!):

installpath = ftp.nluug.nl
installpath += mirror.meerval.net

Given the many surprisingly useful filters that Jinja2 has I wonder if there’s an easier way to write this template still.