Line too long: Ansible lint

14.4k views Asked by At

This is my Ansible task

- 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 url parameter of the task. I already used > to break down the url, not sure how I can reduce it even more to fit in line constrain given by Ansible lint?

2

There are 2 answers

0
Zeitounator On BEST ANSWER

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.

0
β.εηοιτ.βε On

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.example.com

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.example.com

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.example.com