'Line too long: Ansible lint
This is my ansible code
- name: no need to import it.
ansible.builtin.uri:
url: >
https://{{ vertex_region }}-aiplatform.googleapis.com/v1/projects/{{ project }}/locations/{{ vertex_region }}/datasets/{{ dataset_id }}/dataItems
method: GET
headers:
Content-Type: "application/json"
Authorization: Bearer "{{ gcloud_auth }}"
register: images
While checking for Ansible lint, it spills out line too long (151 > 120 characters) (line-length)
The error is for the uri part of the code. I already used > to break down the uri, not sure how can I reduce it even more to fit in line constrain given by ansible lint ?
Solution 1:[1]
If you want to obey the lint line length rule, you need to split your url on several lines.
> is the yaml folded scalar block indicator: new lines will be replaced by spaces. This is not what you want.
The best solution here is to use a double quoted flow scalar where you can escape new lines so that they are not converted to white spaces, e.g.:
url: "https://{{ vertex_region }}-aiplatform.googleapis.com/v1/projects/\
{{ project }}/locations/{{ vertex_region }}/datasets/{{ dataset_id }}/dataItems"
You can add as many escaped new lines as you whish if this is still too long.
https://yaml-multiline.info/ is a good ressource to learn all possibilities for multiline strings in yaml.
Solution 2:[2]
Since it is an URL and that the spaces should already be URL encoded, you could use a combination of YAML folded style — > — with a clip block chomping indicator — - — along with Jinja whitespace control — {{- ... -}}.
All this together could be split in multiple line like:
- ansible.builtin.uri:
url: >-
https://
{{- vertex_region -}}
-aiplatform.googleapis.com/v1/projects/
{{- project -}}
/locations/
{{- vertex_region -}}
/datasets/
{{- dataset_id -}}
/dataItems
For long line that do not contains any Jinja statement or expression, see @Zeitounator's answer, or, use Jinja comment blocks along with whitespace control:
- ansible.builtin.uri:
url: >-
https://this_is_a_super_long_url_
{#- -#}
that_looks_like_it_cannot_be_split_
{#- -#}
into_multiples_lines_is_it_
{#- -#}
question_mark.co.uk
Given the tasks:
- name: Demo debug with variable
ansible.builtin.debug:
msg: >-
https://
{{- vertex_region -}}
-aiplatform.googleapis.com/v1/projects/
{{- project -}}
/locations/
{{- vertex_region -}}
/datasets/
{{- dataset_id -}}
/dataItems
vars:
vertex_region: foo_region
dataset_id: bar_id
project: bar_project
- name: Demo debug without variable
ansible.builtin.debug:
msg: >-
https://this_is_a_super_long_url_
{#- -#}
that_looks_like_it_cannot_be_split_
{#- -#}
into_multiples_lines_is_it_
{#- -#}
question_mark.co.uk
This gives:
TASK [Demo debug with variable] ******************************************************************
ok: [localhost] =>
msg: https://foo_region-aiplatform.googleapis.com/v1/projects/bar_project/locations/foo_region/datasets/bar_id/dataItems
TASK [Demo debug without variable] ***************************************************************
ok: [localhost] =>
msg: https://this_is_a_super_long_url_that_looks_like_it_cannot_be_split_into_multiples_lines_is_it_question_mark.co.uk
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Zeitounator |
| Solution 2 |
