Is there a way to destroy the variable in Ansible?

Actually, I have a {{version}} variable being used in my all roles for respective packages. When I run multiple roles, the version value of one role is passed to another - this is due to for some role I am not giving version value so that it can install the default version of that package available for respective m/c like ubuntu/redhat etc.

Here is my role template. The value of {{version}} from mysql is being passed to rabbitmq.

- { role: mysql }
- { role: rabbitmq}

If I can destory/delete the value of version in every role, it should solve the problem, I believe.

5 Answers 5


As already pointed out it is not possible to unset a variable in Ansible.

Avoid this situation by adding a prefix to your variable names like rabbitmq_version and so on. IMHO this a best practice.

Beside avoiding the situation you ran into, this will add clarity to your host_vars and group_vars.


No, there is no way to unset a variable (top level) in Ansible.

The only thing you can do, is to create a dictionary and store the variable as a key in this dictionary. Clearing the "parent" dictionary will essentially make the dictionary.key is defined conditional expression work.


To unset a variable, try running a set_fact task setting the variable to null, like:

- name: Unset variables

If you have a full dictionary that could just override the dict with null, like:

- name: Set dict
      rabbitmq_version: 1
      other_version: 2

- name: override dict to null

Something like other_var: just is "other_var": null in JSON. That is how you can unset variables in Ansible. Have a nice day.

  • I don't know what you mean by null, but your first example sets those facts to the empty string, not undefined (as I just discovered). May 18, 2021 at 16:43
  • FWIW this approach combined with omit filter could allow the null or empty string to be sensed to drop the version key. It would need to be done whereever the variable is used though, rather than where the variable is set. Oct 11, 2021 at 3:50

you should use variable per role instead:

    - role: mysql
      version: mysql_version
    - role: rabbitmq
      version: rabbitmq_version


    - { role: mysql, version: mysql_version }
    - { role: rabbitmq, version: rabbitmq_version }

You can set it to nothing. I'm currently using it like that:

variable_name: ''
  • 2
    This makes the variable's value an empty string, but it does not unset the variable. Jul 29, 2020 at 14:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .