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 in various scopes. Such variables won't be defined above the particular scope. See Playbook Keywords. You can define variables in this hierarchy of scopes
There are various scenarios on how 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, create a role
shell> tree roles/
roles/
└── test_role
└── tasks
└── main.yml
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
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