name: copy public keys to users- for other environments
authorized_key: user={{ item.username }}
key="{{ lookup('file', '../files/{{ item.username }}.pub') }}"
path='/home/{{ item.username }}/.ssh/authorized_keys'
manage_dir=no
with_items:
- { username: 'xxxxxxxx' }
- { username: 'xxxxxxxx' }
- { username: 'xxxxxxxx' }
运行上述 ansible 代码时出现以下错误。
fatal: [xxxxxx.xxxx.com] => Failed to template user={{ item.username }}
key="{{ lookup('file', '../files/{{ item.username }}.pub') }}": could not
locate file in lookup: ../files/{{ item.username }}.pub
答案1
不要使用嵌套的 Jinja2 表达式。只需按原样使用变量名:
{{ lookup('file', '../files/'+item.username+'.pub') }}