tafazzi87
tafazzi87

Reputation: 449

jinja variable substitution inside blockinfile module

I'm looking to use variables inside blockinfile block and so I made this playbook:

- 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 }}'

but I get the following error:

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 }}"

Sorry but I cannot find in any documentation if blockinfile supports jinja templating inside block module.

Do you have any ideas?

Upvotes: 2

Views: 2680

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68074

The indentation of the block is wrong. See examples and fix it

    - name: Add new user
      blockinfile:
       dest: /root/ansible/users.yml
       backup: yes
       block: |
         '{{ nome_utente }}'@'{{ dominio }}':
                 gruppo: '{{ gruppo }}'

See Popular Editors that support auto-indentation and syntax highlighting.

Upvotes: 3

Related Questions