ansible - blockinfile 模块内的 jinja 变量替换

标签 ansible jinja2

我希望在 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/

相关文章:

ssh - 如何将公钥对 .pem 文件用于 ansible 剧本?

ansible - 使用嵌套的 delegate_to 将文件从一个远程服务器复制到另一个远程服务器

azure-devops - Ansible 和 Azure 资源管理器模板有什么区别?

python - 如何更改主页上的特色群组/组织?

python - 如何扩展 jinja2 内置标签?

ansible - 所有 Ansible 剧本属性

variables - 是否可以组合 Ansible 中单独的 vars 文件中提到的相同变量名的值

python - Flask 路由顺序很重要吗?

python - Ansible - 按最后一个参数(IP 地址)对命令列表进行排序

python - 提供存储在 SQLAlchemy LargeBinary 列中的图像