Skip to content

Instantly share code, notes, and snippets.

@aworldofcode
Created December 15, 2021 12:26
Show Gist options
  • Save aworldofcode/35de280946fabcc66e2eea15c8a13dd8 to your computer and use it in GitHub Desktop.
Save aworldofcode/35de280946fabcc66e2eea15c8a13dd8 to your computer and use it in GitHub Desktop.
Unable to define NUll in Ansible set_fact
# mariadb sql table includes the following column
`date_of_removal` datetime DEFAULT NULL,
# The Key Dict is either
'main_dict.removal_info is defined': '2021-12-19 21:50:43' # a datetime properly formated
'main_dict.removal_info is defined': 'N/A' # A string
# The goal is to properly provide a Null when 'N/A' is present
# Meanwhile in Ansible set_fact
- name: Set the etl_dictionary Key Value Dictionary from main_dict request
set_fact:
etl_dictionary: >-
{
{% if main_dict.removal_info is defined -%}
{% if 'N/A' in main_dict.removal_info -%}
"date_of_removal": 'Null' ,
{% else %}
"date_of_removal": "{{main_dict.removal_info}}",
{% endif %}
{% endif %}
}
# SQL Statement
# Cannot execute SQL 'INSERT INTO .... 'N/A', 'No', 'null', 'N/A'
The expected behaviour to achieve is ... 'N/A', 'No', Null, 'N/A'
@aworldofcode
Copy link
Author

aworldofcode commented Dec 15, 2021

TASK [cert_discovery : Debug certs output] *********************************************************************************************************************************************************
Wednesday 15 December 2021  07:32:27 -0500 (0:00:00.058)       0:00:00.998 **** 
ok: [localhost] => {
    "etl_dictionary": {
        "date_of_removal": null
    }
}

TASK [cert_discovery : Print the etl_dictionary Columns and Keys] **********************************************************************************************************************************
Wednesday 15 December 2021  07:32:27 -0500 (0:00:00.050)       0:00:01.048 **** 
ok: [localhost] => {
    "msg": "(date_of_removal) VALUES (None)"
}


TASK [cert_discovery : Insert  etl_dictionary to our etl_in_cert_scanner DataBase] ***********************************************************************************************************
Wednesday 15 December 2021  07:32:27 -0500 (0:00:00.056)       0:00:01.105 **** 
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Cannot execute SQL 'INSERT INTO etl_in_cert_scanner ( date_of_removal ) VALUES ( 'None' )' args [None]: (1292, u\"Incorrect datetime value: 'None' for column `certmgmt_lst`.`etl_in_cert_scanner`.`date_of_removal` at row 1\")"}
...ignoring

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment