How to pass variables to included tasks in ansible?

Faced the same issue in my project. It turns out that the variable name in the playbook and the task have to be different.

---
- name: The name
  hosts: all
  vars:
    my_var_play: "I need to send this value to the task"
    some_other_var: "This is directly accessible in task"
  tasks:
    - include:my_tasks.yml
      vars:
          my_var: "{{ my_var_play }}"

Also on a sidenote, all the variables in the playbook is accessible in the task. Just use {{ some_other_var }} in task and it should work fine.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)