Task/playbook overwriting of vars

Hi! I have a situation that requires to have some default vars defined in the actual task definition.
Can i count of these to be overwritten if in the playbook (that includes the said task) i define some other values for those variables?
Thank you!

No. It’s not possible. See Understanding variable precedence. Playbook vars (precedence 12-14) do not override the task vars (precedence 17).

- debug:
    var: x
  vars:
    x: foo

Do not use task vars if you want to override them at the play level. Instead, use the default filter. For example,

- debug:
    var: x | d('foo')

As a sidenote: There are two more important use cases:

  • Use the filter default to override a module option’s defaults. For example,
- lineinfile:
    create: "{{ create | d(true) }}"
    ...
  • Use the special variable omit to ‘enable’ the option (and keep the module option’s defaults).
- lineinfile:
    create: "{{ create | d(omit) }}"
    ...
1 Like