Recently, I started working on more repositories on GitLab. One of the common items in my Ansible testing is the use of URL lookups in the templating of my Dockerfiles in Molecule. There’s a completely different method which requires the use of the GitLab API endpoints that require different formatting and token auth. The details for this can be found here: https://docs.gitlab.com/ee/api/repository_files.html#get-raw-file-from-repository
Searching around I did find that you can pass the token via the private_token
parameter to the url.
Because you need to include the folder directory as an encoded value, I had to do lots of trial and error to figure out how to do complicated strings.
Formats like this, DO NOT WORK:
1 |
{{ lookup('url', 'https://gitlab.com/api/v4/projects/xxxxxx/repository/files/' ~ 'dockerfiles/{}/Dockerfile'.format(item.image)|urlencode|regex_replace('/','%2F') ~ '/raw?ref=main&private_token=' ~ lookup('ansible.builtin.env', 'GITLAB_TOKEN'), split_lines=False) }} |
But after a series of attempts, THIS WORKS:
1 2 3 |
{% set file_path = 'dockerfiles/{}/Dockerfile'.format(item.image) %} {% set encoded_path = file_path|urlencode|regex_replace('/','%2F') %} {{ lookup('url', 'https://gitlab.com/api/v4/projects/xxxxxx/repository/files/' ~ encoded_path ~ '/raw?ref=main&private_token=' ~ lookup('ansible.builtin.env', 'GITLAB_TOKEN'), split_lines=False) }} |
Some explanations of my findings urlencode
filter did not work when used inline in the lookup, it made no changes to the file path. To separate, I had to split it out into a jinja set
to set the var to a string that included the value using format()
jinja filter, then take the result and create an encoded path to meet the encoded requirements of GitLab’s API.