我希望在 blockinfile block 中使用变量,所以我制作了这个剧本:
- name: New user
hosts: all
gather_facts: false
become: yes
become_user: root
vars:
nome_utente: pippo
dominio: pluto.it
gruppo: root
tasks:
- name: Add new user
blockinfile:
dest: /root/ansible/users.yml
backup: yes
block: |
'{{ nome_utente }}'@'{{ dominio }}':
gruppo: '{{ gruppo }}'
但我收到以下错误:
ERROR! Syntax Error while loading YAML.
found character that cannot start any token
The error appears to be in '/home/francesco/test.yml': line 16, column 27, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
block: |
'{{ nome_utente }}'@'{{ dominio }}':
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
抱歉,我在任何文档中都找不到 blockinfile 是否支持 block 模块内的 jinja 模板。
你有什么想法吗?
最佳答案
block 的缩进是错误的。请参阅examples并修复它
- name: Add new user
blockinfile:
dest: /root/ansible/users.yml
backup: yes
block: |
'{{ nome_utente }}'@'{{ dominio }}':
gruppo: '{{ gruppo }}'
参见Popular Editors支持自动缩进和语法突出显示。
关于ansible - blockinfile 模块内的 jinja 变量替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63336834/