Help In Refactoring Soon-To-Be-Depreciated Statment

Hi All,

Could someone please give me a hand in refactoring the below task as I’m getting the below warning message, and I can’t figure this out (and the provide (simplified) example doesn’t really help (me - brain went “duuurrr”)) :slight_smile:

The task:

- name: Install User Public Key
  ansible.posix.authorized_key:
     exclusive: true
     key: "{{lookup('file','public_keys/{{ USER_NAME }}_ssh_key.public')}}"

The warning:

[WARNING]: Jinja constant strings should not contain embedded templates. This feature will be disabled by default in ansible-core 2.23.

Use inline expressions, for example: `msg: "{{ lookup('env', '{{ a_var }}') }}"` becomes `msg:"{{ lookup('env', a_var) }}"`

Can you, for example, use the below, because if so then that (to me) is really un-intuitive?

key: "{{lookup('file','public_keys/USER_NAME_ssh_key.public')}}"

Thank you in advance

Cheers
Dulux-Oz

Using ~ to concatenate strings should work:

- name: Install User Public Key
  ansible.posix.authorized_key:
    exclusive: true
    key: "{{ lookup('file', 'public_keys/' ~ USER_NAME ~ '_ssh_key.public') }}"

See Template Designer Documentation — Jinja Documentation (3.1.x)

2 Likes

Thank you @mkrizek , I’ll give that a try

Cheers