How Do You Find the Data Type of a Variable in an Ansible Playbook?

Problem scenario
Through the register keyword you assign a variable.  You want to know what variable type it is (e.g., a string, a dictionary or a JSON).  You know the data type is not a Boolean.  What do you do?

Solution
If you have a variable (e.g., foobar) and you want to know what data type it is, do this:

debug:
  msg:
when:  foobar.stdout == ""

The playbook will tell you if foobar is a dictionary with an error.  If there is no error, it is a string.  If foobar is a JSON you will see a message when you run the playbook about "ansible.utils.unsafe.proxy.AnsibleUnsafeText"

(The above will not help you if it is a Boolean.  You should compare it to "TRUE" or "FALSE" instead of "" if you think the value may be a Boolean.)

Leave a comment

Your email address will not be published. Required fields are marked *