It seems like you are taking a wrong approach.
Take a look at the Ansible documentation concerning variable precedence. It is a built-in feature of Ansible to use the default variable if the variable is not defined.
In Ansible 2.x the variable precedence starts like this:
role defaults
inventory vars
So if you want to define a default value for a variable you should set it in role/defaults/main.yml
. Ansible will use that value only if the variable is not defined somewhere else.
Another option is to use a Jinja2 filter. With a Jinja filter you can set a default value for a variable like this:
{{ some_variable | default(5) }}