How to pass variables to included tasks in ansible?
You shouldn't need to explicitly pass my_var
to the include
. All variables including extra-vars should be directly available everywhere. So simply calling
ansible-playbook my_file.yml --extra-vars "my_var=1.2.3"
and using it as {{ my_var }}
in the tasks should work.
- name: My task
command: bash -c "curl -sSL http://x.com/file-{{ my_var }} > /tmp/file.deb"
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.