ansible

[WARNING]: The value 1 (type int) in a string field was converted to u’1′ (type string)

I have seen this error multiple times during my Ansible playbooks execution but always ignored it because I always got the expected result.

Eventually, I wanted to get rid of any errors/warnings from my final version of the playbook and wanted to take care of this error as well. Hence I had to do little readings in this error/warning.

[WARNING]: The value 1 (type int) in a string field was converted to u'1' (type string). If this does not look like what you expect, quote the entire value to ensure it does
not change.

As per the Ansible official documentation this is the Default behavior of starting Ansible version 2.8. Ansible will warn if a module expects a string, but a non-string value is passed and automatically converted to a string.

For example – If your playbook inputs a value version number 1.10 (parsed as float value) would be converted to '1.1'. Such conversions can result in unexpected behavior depending on context and also will/might change playbook’s expected outcomes

There are two solutions to address this issue.

1. ANSIBLE_STRING_CONVERSION_ACTION Environment Variable

This behavior can be changed to be an error or to be ignored by setting the ANSIBLE_STRING_CONVERSION_ACTION environment variable, or by setting the string_conversion_action configuration in the defaults section of ansible.cfg.

You can refer to this link to change the string_conversion_action parameter in defaults section of Ansible.cfg. This will make sure Ansible will not change the input to String

2. Quote the input values in playbook

I personally prefer this method instead of editing Ansible.cfg file. To make sure you’re not getting string field conversion Warning message you can simply quote the input value. Below is the example for the same.

  - name: Sample sysctl task
    sysctl:
     name: net.bridge.bridge-nf-call-iptables
     value: '1'
     state: present

Below are the playbook screen captures of before and after quoting the values in playbook

The value 1 (type int) in a string field was converted to u'1' (type string) - Before Solution
The value 1 (type int) in a string field was converted to u’1′ (type string) –
Before making changes
The value 1 (type int) in a string field was converted to u'1' (type string) - After Solution
The value 1 (type int) in a string field was converted to u’1′ (type string) – After making changes

I hope this helps everyone.

Categories: ansible

Tagged as: , , , , ,

2 replies »

    • There’s an Ansible documentation link in this post which lists the details around “String_Conversion_Action” parameter. This will change default error starting v2.12 (currently warn). If you change the setting from warn to error then playbook task will stop for any string value which is not quoted. This will make sure any unquoted string values aren’t going unnoticed with just warning and eventually change the expected outcome.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s