An Ansible Interview Question: How to Access Variables in Templates

Hello all,

I’m having an issue with trying to access a variable in a template when using Ansible. I’m using a template in a task and I’m trying to access a variable I’ve defined, but it doesn’t seem to be working. Here’s the code I’m using:

- name: My Template
  template:
    src: "mytemplate.j2"
    dest: "{{ output_file }}"
    vars:
      myvar: "{{ myvar }}"

I’ve defined myvar in my playbook, so I’m not sure why it’s not being accessed in the template. Has anyone else encountered this problem before? Any help is much appreciated.

Thanks in advance!