# Json and local facts.

**URL:** <https://forum.ansible.com/t/json-and-local-facts/14918>\
**Category:** Ansible Project\
**Created:** [April 2, 2014, 12:12pm UTC](https://forum.ansible.com/t/json-and-local-facts/14918 "2014-04-02T12:12:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcin\_Praczko](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Marcin\_Praczko](https://forum.ansible.com/u/Marcin_Praczko)\
**Post date:** [April 2, 2014, 12:12pm UTC](https://forum.ansible.com/t/json-and-local-facts/14918/1 "2014-04-02T12:12:13Z")

</div>

Hi,

I am working on some tasks related with local facts and not sure whether followig issue is a bug or is designed.  
Following ansible documentation localfacts can be in JSON or INI format. I’ve choosen JSON format - is much more easier to write variables like this:

module:  
name: “apache”  
version: “1.0.4”  
status: “Installed”

Saving this with following task brings result:

- name: Write variable to json file  
copy: content=“{{ module }}” dest=/etc/ansible/facts.d/{{ module.name }}.fact  
owner=root group=root mode=0644

Result:  
{‘status’: ‘Installed’, ‘version’: ‘1.0.4’, ‘name’: ‘apache’}

However python is not able read this anymore - including ansible:

ok: [IP-HERE] =\> {  
“msg”: “work - variable: {u’apache’: u’error loading fact - please check content’}”  
}

Command from python:

# cat apache.fact | python -mjson.tool

Expecting property name: line 1 column 1 (char 1)

Converting this local facts with following task / command - automatically allow read this file by python and ansible.

- name: Fix wrong json format in local facts  
shell: sed -i “s/'/"/g” \*.fact chdir=/etc/ansible/facts.d/

Could you advice whether I should raise this on Github bug list or this is working exectly as should?

Best regards,  
Marcin Praczko

---

<div class="post-metadata">

**Author:** ![jack1](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@jack1](https://forum.ansible.com/u/jack1)\
**Post date:** [April 8, 2015, 2:31pm UTC](https://forum.ansible.com/t/json-and-local-facts/14918/2 "2015-04-08T14:31:07Z")

</div>

Anyone has clue for this question, we have same issue, we are doing something like:

At one ansible playbook, to collect some local facts, and save those facts into a file (using copy content), the saved file looks like:

{u’status’: u’Installed’, u’version’: u’1.0.4’, u’name’: u’apache’}

Later on, there is other ansible playbook will load those facts back in (using local\_vars), even we replace the single quote, the facts file still cannot be loaded back, because there is leading “u” at each element,

Thanks  
Jack

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [April 8, 2015, 2:37pm UTC](https://forum.ansible.com/t/json-and-local-facts/14918/3 "2015-04-08T14:37:19Z")

</div>

You should be using the “to\_json” filter to convert the python data structure to JSON. Or potentially even the “to\_nice\_json” filter.

Such as:

- name: Write variable to json file  
copy: content=‘{{ module|to\_json }}’ dest=/etc/ansible/facts.d/{{ [module.name](http://module.name/) }}.fact  
owner=root group=root mode=0644

---

<div class="post-metadata">

**Author:** ![jack1](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@jack1](https://forum.ansible.com/u/jack1)\
**Post date:** [April 8, 2015, 2:57pm UTC](https://forum.ansible.com/t/json-and-local-facts/14918/4 "2015-04-08T14:57:02Z")

</div>

That works, thank you so much.
