Ansible-lint name[template] with multiple vars

Hello,

I ask an help to avoid this error:

name[template]: Jinja templates should only be at the end of 'name'
roles/role_name/tasks/main.yml:39 Task/Handler: Copy file in {{ role_name_conf.dest }} of {{ role_name_server.name }}

Does this rule implicitly says that only a single one var can be added in the task name?

Is there a way to add mutiple vars without trigger this violation?

Of course, I can add a lint skip, but I would like to write plays without skipping rules.

Thank you very much for every hints.

# ansible-lint --version
ansible-lint 24.7.0 using ansible-core:2.16.9 ansible-compat:24.7.0 ruamel-yaml:0.18.6 ruamel-yaml-clib:0.2.8

Kind Regards
Marco

You can work around this in a way that also results in the name being templated when it wouldn’t otherwise be (for example when a task is skipped?), for example:

- name: "{{ task_name }}"
  ansible.foo.bar:
  vars:
    task_name: "{{ foo }} {{ bar }}"
1 Like

Or simply use a noqa to disable this rule locally, or even disable it globally if you disagree with this rule.

3 Likes

The two above answers are very good solutions for me.
I forgot the # noqa, which is very easy for these locally workarounds.

Thank you very much @chris and @felixfontein :top:

Marco

1 Like

Yet another solution

Copy file in {{ role_name_conf.dest ~ ' of '  ~ role_name_server.name }}

or

Copy file in {{ ' of '.join( [role_name_conf.dest, role_name_server.name]) }}

The rationale behind that rule is to simplify finding the task in the playbook based on the task’s name. All workarounds make it harder to find the corresponding task, or make it harder to read the template code.

So either rewrite the task’s title (and not the way you template it) so you only need to template in one place (namely at the end), or use noqa / disable the rule. Every other solution makes the situation worse and goes against the spirit of the linting rule.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.