Getting "mapping values are not allowed here" error in Ansible dictionary task

Hey everyone,

I’ve been going through the docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html and hit a strange issue that I can’t seem to sort out.

I’m trying to pass a dictionary of config options inside a task, and I keep getting a mapping values are not allowed here error. Here’s a trimmed-down version of what I’m working with:

vars:
  config_data: "{{ lookup('file', 'settings.json') }}"

tasks:
  - name: Apply configuration
    ansible.builtin.template:
      src: config.j2
      dest: "/etc/myapp/config.yaml"
      variables:
        settings: {
          mode: "advanced",
          enabled: true,
          threshold: 2.0
        }

I’ve double-checked the syntax, tried reformatting the dictionary, and even tested it with a YAML linter — everything seems fine. But Ansible still complains. I’m wondering if this could be a conflict between how Jinja expressions are evaluated inside YAML or something subtle I’m overlooking.

Has anyone else run into this? Any tips or direction would be appreciated!

Change variables to vars?

  - name: Apply configuration
    ansible.builtin.template:
      src: config.j2
      dest: "/etc/myapp/config.yaml"
      vars:
        settings:
          mode: advanced
          enabled: true
          threshold: 2.0

variables is not a valid template option, neither is vars, but you can add vars to the task level, just need tt outdent from @chris’ example

In any case you are getting a YAML error as you cannot mix ‘shorthand’ and go back to standard YAML:

valid:

variables: { mode: advance, enabled: true}

also valid:

variables:
    mode: advance
    enabled: true

invalid:

variables: {
  mode: advance,
  enabled: true,
}
1 Like

Thanks @bcoca I made a typo with the indention of vars :roll_eyes: this is want I wanted to suggest:

  - name: Apply configuration
    ansible.builtin.template:
      src: config.j2
      dest: "/etc/myapp/config.yaml"
    vars:
      settings:
        mode: advanced
        enabled: true
        threshold: 2.0