# {{ ansible_managed }}

concurrent = {{ gitlab_runner__concurrent }}
{% if gitlab_runner__metrics_server|d() %}
metrics_server = "{{ gitlab_runner__metrics_server }}"
{% endif %}

{% set gitlab_runner__tpl_instances = [] %}
{% set gitlab_runner__tpl_instance_tokens = [] %}
{% if gitlab_runner__register_new_instances|d() and gitlab_runner__register_new_instances.results %}
{%   for item in gitlab_runner__register_new_instances.results %}
{%     if item.item.name|d() and item.json|d() and item.json.token|d() %}
{%       set _ = gitlab_runner__tpl_instances.append(item.item.name) %}
{%       set _ = gitlab_runner__tpl_instance_tokens.append({ "name": item.item.name, "token": item.json.token }) %}
{%     endif %}
{%   endfor %}
{% endif %}
{% if ansible_local|d() and ansible_local.gitlab_runner|d() and ansible_local.gitlab_runner.instance_tokens|d() %}
{%   for item in ansible_local.gitlab_runner.instance_tokens %}
{%     if item.name not in gitlab_runner__tpl_instances %}
{%       for element in (gitlab_runner__default_instances + gitlab_runner__instances + gitlab_runner__group_instances + gitlab_runner__host_instances) %}
{%         if (item.name == element.name and (element.state is undefined or element.state != 'absent')) %}
{%           set _ = gitlab_runner__tpl_instances.append(item.name) %}
{%           set _ = gitlab_runner__tpl_instance_tokens.append({ "name": item.name, "token": item.token }) %}
{%         endif %}
{%       endfor %}
{%     endif %}
{%   endfor %}
{% endif %}
{% for runner in (gitlab_runner__default_instances + gitlab_runner__instances + gitlab_runner__group_instances + gitlab_runner__host_instances) %}
{%   set gitlab_runner__tpl_token = [] %}
{%   for item in gitlab_runner__tpl_instance_tokens %}
{%     if runner.name == item.name and item.token %}
{%       set _ = gitlab_runner__tpl_token.append(item.token) %}
{%     endif %}
{%   endfor %}
{%   if ((runner.state is undefined or runner.state != 'absent') and gitlab_runner__tpl_token) %}
[[runners]]
  name = "{{ runner.name }}"
  url = "{{ runner.api_url | d(gitlab_runner__api_url) }}"
  token = "{{ gitlab_runner__tpl_token[0] }}"
{%     set gitlab_runner__tpl_environment = [] %}
{%     if gitlab_runner__environment is mapping %}
{%       for key, value in gitlab_runner__environment.items() %}
{%         set _ = gitlab_runner__tpl_environment.append(key + "=" + value) %}
{%       endfor %}
{%     elif gitlab_runner__environment is string %}
{%       set _ = gitlab_runner__tpl_environment.append(gitlab_runner__environment) %}
{%     else %}
{%       for element in gitlab_runner__environment %}
{%         set _ = gitlab_runner__tpl_environment.append(element) %}
{%       endfor %}
{%     endif %}
{%     if runner.environment|d() %}
{%       if runner.environment is mapping %}
{%         for key, value in runner.environment.items() %}
{%           set _ = gitlab_runner__tpl_environment.append(key + "=" + value) %}
{%         endfor %}
{%       elif runner.environment is string %}
{%         set _ = gitlab_runner__tpl_environment.append(runner.environment) %}
{%       else %}
{%         for element in runner.environment %}
{%           set _ = gitlab_runner__tpl_environment.append(element) %}
{%         endfor %}
{%       endif %}
{%     endif %}
{%     if gitlab_runner__tpl_environment %}
  environment = [ "{{ gitlab_runner__tpl_environment | join('", "') }}" ]
{%     endif %}
{%     if runner.tls_ca_file|d() %}
  tls-ca-file = "{{ runner.tls_ca_file }}"
{%     endif %}
{%     if runner.shell|d() %}
  shell = "{{ runner.shell }}"
{%     endif %}
{%     if runner.limit|d() %}
  limit = {{ runner.limit }}
{%     endif %}
{%     if runner.output_limit|d() %}
  output_limit = {{ runner.output_limit }}
{%     endif %}
{%     if runner.builds_dir|d() %}
  builds_dir = "{{ runner.builds_dir }}"
{%     endif %}
{%     if runner.cache_dir|d() %}
  cache_dir = "{{ runner.cache_dir }}"
{%     endif %}
{%     if runner.disable_verbose|d() %}
  disable_verbose = {{ runner.disable_verbose | bool | lower }}
{%     endif %}
  executor = "{{ runner.executor | d(gitlab_runner__executor) }}"
{%     if ((runner.executor|d() and runner.executor in [ 'docker', 'docker-ssh', 'docker+machine', 'docker-ssh+machine' ]) or
           (gitlab_runner__executor in [ 'docker', 'docker-ssh', 'docker+machine', 'docker-ssh+machine' ])) %}
  [runners.docker]
    image = "{{ runner.docker_image | d(gitlab_runner__docker_image) }}"
    privileged = {{ (runner.docker_privileged | d(gitlab_runner__docker_privileged)) | bool | lower }}
    disable_cache = {{ (runner.docker_disable_cache | d(gitlab_runner__docker_disable_cache)) | bool | lower }}
{%       if (runner.docker_cache_dir|d() or gitlab_runner__docker_cache_dir) %}
    cache_dir = "{{ runner.docker_cache_dir | d(gitlab_runner__docker_cache_dir) }}"
{%       endif %}
{%       if (runner.docker_host|d() or gitlab_runner__docker_host) %}
    host = "{{ runner.docker_host | d(gitlab_runner__docker_host) }}"
{%       endif %}
{%       if runner.docker_hostname|d() %}
    hostname = "{{ runner.docker_hostname }}"
{%       endif %}
{%       if (runner.docker_tls_cert_path|d() or gitlab_runner__docker_tls_cert_path) %}
    tls_cert_path = "{{ runner.docker_tls_cert_path | d(gitlab_runner__docker_tls_cert_path) }}"
{%       endif %}
{%       if runner.docker_wait_for_services_timeout|d() %}
    wait_for_services_timeout = {{ runner.docker_wait_for_services_timeout }}
{%       endif %}
{%       if runner.docker_image_ttl|d() %}
    image_ttl = {{ runner.docker_image_ttl }}
{%       endif %}
{%       set gitlab_runner__tpl_docker_cap_add = [] %}
{%       if gitlab_runner__docker_cap_add %}
{%         if gitlab_runner__docker_cap_add is string %}
{%           set _ = gitlab_runner__tpl_docker_cap_add.append(gitlab_runner__docker_cap_add) %}
{%         else %}
{%           for capability in gitlab_runner__docker_cap_add %}
{%             set _ = gitlab_runner__tpl_docker_cap_add.append(capability) %}
{%           endfor %}
{%         endif %}
{%       endif %}
{%       if runner.docker_cap_add|d() %}
{%         if runner.docker_cap_add is string %}
{%           set _ = gitlab_runner__tpl_docker_cap_add.append(runner.docker_cap_add) %}
{%         else %}
{%           for capability in runner.docker_cap_add %}
{%             set _ = gitlab_runner__tpl_docker_cap_add.append(capability) %}
{%           endfor %}
{%         endif %}
{%       endif %}
{%       if gitlab_runner__tpl_docker_cap_add %}
    cap_add = [ "{{ gitlab_runner__tpl_docker_cap_add | join('", "') }}" ]
{%       endif %}
{%       set gitlab_runner__tpl_docker_cap_drop = [] %}
{%       if gitlab_runner__docker_cap_drop %}
{%         if gitlab_runner__docker_cap_drop is string %}
{%           set _ = gitlab_runner__tpl_docker_cap_drop.append(gitlab_runner__docker_cap_drop) %}
{%         else %}
{%           for capability in gitlab_runner__docker_cap_drop %}
{%             set _ = gitlab_runner__tpl_docker_cap_drop.append(capability) %}
{%           endfor %}
{%         endif %}
{%       endif %}
{%       if runner.docker_cap_drop|d() %}
{%         if runner.docker_cap_drop is string %}
{%           set _ = gitlab_runner__tpl_docker_cap_drop.append(runner.docker_cap_drop) %}
{%         else %}
{%           for capability in runner.docker_cap_drop %}
{%             set _ = gitlab_runner__tpl_docker_cap_drop.append(capability) %}
{%           endfor %}
{%         endif %}
{%       endif %}
{%       if gitlab_runner__tpl_docker_cap_drop %}
    cap_drop = [ "{{ gitlab_runner__tpl_docker_cap_drop | join('", "') }}" ]
{%       endif %}
{%       set gitlab_runner__tpl_docker_devices = [] %}
{%       if gitlab_runner__docker_devices %}
{%         if gitlab_runner__docker_devices is string %}
{%           set _ = gitlab_runner__tpl_docker_devices.append(gitlab_runner__docker_devices) %}
{%         else %}
{%           for device in gitlab_runner__docker_devices %}
{%             set _ = gitlab_runner__tpl_docker_devices.append(device) %}
{%           endfor %}
{%         endif %}
{%       endif %}
{%       if runner.docker_devices|d() %}
{%         if runner.docker_devices is string %}
{%           set _ = gitlab_runner__tpl_docker_devices.append(runner.docker_devices) %}
{%         else %}
{%           for device in runner.docker_devices %}
{%             set _ = gitlab_runner__tpl_docker_devices.append(device) %}
{%           endfor %}
{%         endif %}
{%       endif %}
{%       if gitlab_runner__tpl_docker_devices %}
    devices = [ "{{ gitlab_runner__tpl_docker_devices | join('", "') }}" ]
{%       endif %}
{%       set gitlab_runner__tpl_docker_extra_hosts = [] %}
{%       if gitlab_runner__docker_extra_hosts %}
{%         if gitlab_runner__docker_extra_hosts is string %}
{%           set _ = gitlab_runner__tpl_docker_extra_hosts.append(gitlab_runner__docker_extra_hosts) %}
{%         else %}
{%           for host in gitlab_runner__docker_extra_hosts %}
{%             set _ = gitlab_runner__tpl_docker_extra_hosts.append(host) %}
{%           endfor %}
{%         endif %}
{%       endif %}
{%       if runner.docker_extra_hosts|d() %}
{%         if runner.docker_extra_hosts is string %}
{%           set _ = gitlab_runner__tpl_docker_extra_hosts.append(runner.docker_extra_hosts) %}
{%         else %}
{%           for host in runner.docker_extra_hosts %}
{%             set _ = gitlab_runner__tpl_docker_extra_hosts.append(host) %}
{%           endfor %}
{%         endif %}
{%       endif %}
{%       if gitlab_runner__tpl_docker_extra_hosts %}
    extra_hosts = [ "{{ gitlab_runner__tpl_docker_extra_hosts | join('", "') }}" ]
{%       endif %}
{%       set gitlab_runner__tpl_docker_links = [] %}
{%       if gitlab_runner__docker_links %}
{%         if gitlab_runner__docker_links is string %}
{%           set _ = gitlab_runner__tpl_docker_links.append(gitlab_runner__docker_links) %}
{%         else %}
{%           for link in gitlab_runner__docker_links %}
{%             set _ = gitlab_runner__tpl_docker_links.append(link) %}
{%           endfor %}
{%         endif %}
{%       endif %}
{%       if runner.docker_links|d() %}
{%         if runner.docker_links is string %}
{%           set _ = gitlab_runner__tpl_docker_links.append(runner.docker_links) %}
{%         else %}
{%           for link in runner.docker_links %}
{%             set _ = gitlab_runner__tpl_docker_links.append(link) %}
{%           endfor %}
{%         endif %}
{%       endif %}
{%       if gitlab_runner__tpl_docker_links %}
    links = [ "{{ gitlab_runner__tpl_docker_links | join('", "') }}" ]
{%       endif %}
{%       set gitlab_runner__tpl_docker_services = [] %}
{%       if gitlab_runner__docker_services %}
{%         if gitlab_runner__docker_services is string %}
{%           set _ = gitlab_runner__tpl_docker_services.append(gitlab_runner__docker_services) %}
{%         else %}
{%           for service in gitlab_runner__docker_services %}
{%             set _ = gitlab_runner__tpl_docker_services.append(service) %}
{%           endfor %}
{%         endif %}
{%       endif %}
{%       if runner.docker_services|d() %}
{%         if runner.docker_services is string %}
{%           set _ = gitlab_runner__tpl_docker_services.append(runner.docker_services) %}
{%         else %}
{%           for service in runner.docker_services %}
{%             set _ = gitlab_runner__tpl_docker_services.append(service) %}
{%           endfor %}
{%         endif %}
{%       endif %}
{%       if gitlab_runner__tpl_docker_services %}
    services = [ "{{ gitlab_runner__tpl_docker_services | join('", "') }}" ]
{%       endif %}
{%       set gitlab_runner__tpl_docker_volumes = [] %}
{%       if gitlab_runner__docker_volumes %}
{%         if gitlab_runner__docker_volumes is string %}
{%           set _ = gitlab_runner__tpl_docker_volumes.append(gitlab_runner__docker_volumes) %}
{%         else %}
{%           for volume in gitlab_runner__docker_volumes %}
{%             set _ = gitlab_runner__tpl_docker_volumes.append(volume) %}
{%           endfor %}
{%         endif %}
{%       endif %}
{%       if runner.docker_volumes|d() %}
{%         if runner.docker_volumes is string %}
{%           set _ = gitlab_runner__tpl_docker_volumes.append(runner.docker_volumes) %}
{%         else %}
{%           for volume in runner.docker_volumes %}
{%             set _ = gitlab_runner__tpl_docker_volumes.append(volume) %}
{%           endfor %}
{%         endif %}
{%       endif %}
{%       if gitlab_runner__tpl_docker_volumes %}
    volumes = [ "{{ gitlab_runner__tpl_docker_volumes | join('", "') }}" ]
{%       endif %}
{%       set gitlab_runner__tpl_docker_allowed_images = [] %}
{%       if gitlab_runner__docker_allowed_images %}
{%         if gitlab_runner__docker_allowed_images is string %}
{%           set _ = gitlab_runner__tpl_docker_allowed_images.append(gitlab_runner__docker_allowed_images) %}
{%         else %}
{%           for image in gitlab_runner__docker_allowed_images %}
{%             set _ = gitlab_runner__tpl_docker_allowed_images.append(image) %}
{%           endfor %}
{%         endif %}
{%       endif %}
{%       if runner.docker_allowed_images|d() %}
{%         if runner.docker_allowed_images is string %}
{%           set _ = gitlab_runner__tpl_docker_allowed_images.append(runner.docker_allowed_images) %}
{%         else %}
{%           for image in runner.docker_allowed_images %}
{%             set _ = gitlab_runner__tpl_docker_allowed_images.append(image) %}
{%           endfor %}
{%         endif %}
{%       endif %}
{%       if gitlab_runner__tpl_docker_allowed_images %}
    allowed_images = [ "{{ gitlab_runner__tpl_docker_allowed_images | join('", "') }}" ]
{%       endif %}
{%       set gitlab_runner__tpl_docker_allowed_services = [] %}
{%       if gitlab_runner__docker_allowed_services %}
{%         if gitlab_runner__docker_allowed_services is string %}
{%           set _ = gitlab_runner__tpl_docker_allowed_services.append(gitlab_runner__docker_allowed_services) %}
{%         else %}
{%           for service in gitlab_runner__docker_allowed_services %}
{%             set _ = gitlab_runner__tpl_docker_allowed_services.append(service) %}
{%           endfor %}
{%         endif %}
{%       endif %}
{%       if runner.docker_allowed_services|d() %}
{%         if runner.docker_allowed_services is string %}
{%           set _ = gitlab_runner__tpl_docker_allowed_services.append(runner.docker_allowed_services) %}
{%         else %}
{%           for service in runner.docker_allowed_services %}
{%             set _ = gitlab_runner__tpl_docker_allowed_services.append(service) %}
{%           endfor %}
{%         endif %}
{%       endif %}
{%       if gitlab_runner__tpl_docker_allowed_services %}
    allowed_services = [ "{{ gitlab_runner__tpl_docker_allowed_services | join('", "') }}" ]
{%       endif %}
{%       if runner.docker_pull_policy|d() %}
    pull_policy = "{{ runner.docker_pull_policy }}"
{%       endif %}
{%       if runner.docker_tmpfs|d() %}
    [runners.docker_tmpfs]
{%         for directory, options in runner.docker_tmpfs.items() %}
      "{{ directory }}"= "{{ options }}"
{%         endfor %}
{%       endif %}
{%       if runner.docker_services_tmpfs|d() %}
    [runners.docker.services_tmpfs]
{%         for directory, options in runner.docker_services_tmpfs.items() %}
      "{{ directory }}"= "{{ options }}"
{%         endfor %}
{%       endif %}
{%    endif %}
{%     if ((runner.executor|d() and runner.executor in [ 'parallels' ]) or
           (gitlab_runner__executor in [ 'parallels' ])) %}
  [runners.parallels]
    base_name = "{{ runner.parallels_base_name }}"
{%       if runner.parallels_template_name|d() %}
    template_name = "{{ runner.parallels_template_name }}"
{%       endif %}
{%       if runner.parallels_disable_snapshots|d() %}
    disable_snapshots = {{ runner.parallels_disable_snapshots | bool | lower }}
{%       endif %}
{%     endif %}
{%     if ((runner.executor|d() and runner.executor in [ 'virtualbox' ]) or
           (gitlab_runner__executor in [ 'virtualbox' ])) %}
  [runners.virtualbox]
    base_name = "{{ runner.virtualbox_base_name }}"
{%       if runner.virtualbox_disable_snapshots|d() %}
    disable_snapshots = {{ runner.virtualbox_disable_snapshots | bool | lower }}
{%       endif %}
{%     endif %}
{%     if ((runner.executor|d() and runner.executor in [ 'ssh', 'docker-ssh', 'docker-ssh+machine', 'parallels', 'virtualbox' ]) or
           (gitlab_runner__executor in [ 'ssh', 'docker-ssh', 'docker-ssh+machine', 'parallels', 'virtualbox' ])) %}
  [runners.ssh]
    host = "{{ runner.ssh_host | d(gitlab_runner__ssh_host) }}"
    port = "{{ runner.ssh_port | d(gitlab_runner__ssh_port) }}"
    user = "{{ runner.ssh_user | d(gitlab_runner__ssh_user) }}"
{%       if (runner.ssh_password|d() or gitlab_runner__ssh_password) %}
    password = "{{ runner.ssh_password | d(gitlab_runner__ssh_password) }}"
{%       endif %}
{%       if (runner.ssh_identity_file|d() or gitlab_runner__ssh_identity_file) %}
    identity_file = "{{ runner.ssh_identity_file | d(gitlab_runner__ssh_identity_file) }}"
{%       endif %}
{%     endif %}
{%     if ((runner.executor|d() and runner.executor in [ 'docker+machine', 'docker-ssh+machine' ]) or
           (gitlab_runner__executor in [ 'docker+machine', 'docker-ssh+machine' ])) %}
  [runners.machine]
    IdleCount = {{ runner.machine_idle_count | d(gitlab_runner__machine_idle_count) }}
    IdleTime = {{ runner.machine_idle_time | d(gitlab_runner__machine_idle_time) }}
    MaxBuilds = {{ runner.machine_max_builds | d(gitlab_runner__machine_max_builds) }}
    MachineName = "{{ runner.machine_name | d(gitlab_runner__machine_name) }}"
    MachineDriver = "{{ runner.machine_driver | d(gitlab_runner__machine_driver) }}"
    {% if runner.machine_offpeakperiods|d() or gitlab_runner__machine_offpeakperiods %}
     OffPeakPeriods = [
         {%         if runner.machine_offpeakperiods|d() %}
         {%           for offpeakperiod in runner.machine_offpeakperiods %}
               "{{ offpeakperiod }}"{% if not loop.last %},
         {% endif %}
         {%           endfor %}
         {%         else %}
         {%           for offpeakperiod in gitlab_runner__machine_offpeakperiods %}
               "{{ offpeakperiod }}"{% if not loop.last %},
         {% endif %}
         {%           endfor %}
         {%         endif %}
     ]
 {% endif %}
 {% if runner.machine_offpeakidlecount|d() or gitlab_runner__machine_offpeakidlecount %}
     OffPeakIdleCount = {{ runner.machine_offpeakidlecount | d(gitlab_runner__machine_offpeakidlecount) }}
 {% endif %}
 {% if runner.machine_offpeakidletime|d() or gitlab_runner__machine_offpeakidletime %}
     OffPeakIdleTime = {{ runner.machine_offpeakidletime | d(gitlab_runner__machine_offpeakidletime) }}
 {% endif %}
{%       if runner.machine_options|d() or gitlab_runner__machine_options %}
    MachineOptions = [
{%         if runner.machine_options|d() %}
{%           for option in runner.machine_options %}
      "{{ option }}"{% if not loop.last %},
{% endif %}
{%           endfor %}
{%         else %}
{%           for option in gitlab_runner__machine_options %}
      "{{ option }}"{% if not loop.last %},
{% endif %}
{%           endfor %}
{%         endif %}

    ]
{%       endif %}
{%     endif %}
{%     if ((runner.cache|d() and runner.cache|bool) or gitlab_runner__cache|bool) %}
  [runners.cache]
    Type = "{{ runner.cache_type | d(gitlab_runner__cache_type) }}"
    ServerAddress = "{{ runner.cache_server_address | d(gitlab_runner__cache_server_address) }}"
    AccessKey = "{{ runner.cache_access_key | d(gitlab_runner__cache_access_key) }}"
    SecretKey = "{{ runner.cache_secret_key | d(gitlab_runner__cache_secret_key) }}"
    BucketName = "{{ runner.cache_bucket_name | d(gitlab_runner__cache_bucket_name) }}"
    BucketLocation = "{{ runner.cache_bucket_location | d(gitlab_runner__cache_bucket_location) }}"
    Insecure = {{ (runner.cache_insecure | d(gitlab_runner__cache_insecure)) | bool | lower }}
    Shared = {{ (runner.cache_shared | d(gitlab_runner__cache_shared)) | bool | lower }}
{%     endif %}

{%   endif %}
{% endfor %}
