I fear your question is too wide to get really good answers. Why don't
you give an example of what you are trying to achieve? Or which code
works/does not work for you?
Generally, variables set in host_vars, group_vars, vars-sections of
plays and similar are set and can be used within a play or role or task.
What I really wanted to understand it is just the generic method or methods of defining and passing variables to a module in a playbook. I am at the beginning of learning ansible and that is one of the questions I would like to have clarified.