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.


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.


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 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.