Q: "Is there a possibility to unset a fact (variable) in Ansible?"
A: No. It is not possible to unset variables in Ansible. Instead, it's possible to define variables at various levels. Such variables won't be defined outside the particular level. See Playbook Keywords. You can define variables at
Create a role that will be used in various scenarios to solve the use cases: "It may be necessary to unset variables before execution, e.g. if a role is used multiple times via include_role or tasks are used multiple times via include_tasks."
For example, given the tree
shell> tree roles/
roles/
└── test_role
└── tasks
└── main.yml
and the role test_role with the single task
shell> cat roles/test_role/tasks/main.yml
- debug:
var: test_var
Use case 1. A role is used multiple times via include_role
For example, the playbook
- hosts: localhost
tasks:
- include_role:
name: test_role
vars:
test_var: first run
- include_role:
name: test_role
- include_role:
name: test_role
vars:
test_var: third run
gives (abridged)
test_var: first run
test_var: VARIABLE IS NOT DEFINED!
test_var: third run
Use case 2. Tasks are used multiple times via include_tasks
In the same way, the playbook below gives the same result
- hosts: localhost
tasks:
- include_tasks: roles/test_role/tasks/main.yml
vars:
test_var: first run
- include_tasks: roles/test_role/tasks/main.yml
- include_tasks: roles/test_role/tasks/main.yml
vars:
test_var: third run
Use case 3. Use roles
For example, the playbook below gives also the same result
- hosts: localhost
roles:
- role: test_role
test_var: first run
- role: test_role
- role: test_role
test_var: third run
Notes
- hosts: localhost
vars:
test_var: default_value
tasks:
- debug:
var: test_var
- set_fact:
test_var:
- debug:
msg: "test_var is defined: {{ test_var is defined }}"
- debug:
var: test_var
shows (abridged) that an empty variable is defined and null. You will receive the same results when you set the variable explicitly to null by test_var: !!null
TASK [debug] **********************************************************************************************
ok: [localhost] =>
test_var: default_value
TASK [set_fact] *******************************************************************************************
ok: [localhost]
TASK [debug] **********************************************************************************************
ok: [localhost] =>
msg: 'test_var is defined: True'
TASK [debug] **********************************************************************************************
ok: [localhost] =>
test_var: null
- It's also good to understand that default doesn't set the value when the variable is defined. For example,
- debug:
var: test_var
- debug:
var: test_var|default('default when undef')
shows that the filter default doesn't by default cares the value is null and doesn't set the value because the variable is defined
TASK [debug] **********************************************************************************************
ok: [localhost] =>
test_var: null
TASK [debug] **********************************************************************************************
ok: [localhost] =>
test_var|default('default when undef'): ''
- If you want to set the default value also when the variable is null set the second parameter of the filter
True. For example,
- debug:
var: test_var
- debug:
var: test_var|default('default when undef or null', true)
gives
TASK [debug] **********************************************************************************************
ok: [localhost] =>
test_var: null
TASK [debug] **********************************************************************************************
ok: [localhost] =>
test_var|default('default when undef or null', true): default when undef or null