Json and local facts.

1 363 visningar
Hoppa till det första olästa meddelandet

Marcin Prączko

oläst,
2 apr. 2014 08:12:132014-04-02
till ansible...@googlegroups.com
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

jack

oläst,
8 apr. 2015 10:31:072015-04-08
till ansible...@googlegroups.com
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

Matt Martz

oläst,
8 apr. 2015 10:38:022015-04-08
till ansible...@googlegroups.com
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 }}.fact 
        owner=root group=root mode=0644

Also note that I changed the double quotes to single quotes.  JSON data will use double quotes, and you could end up with the quotes breaking you out of the value for content.


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/48324b91-3438-44c5-98bb-7bc1881ef7c0%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

jack

oläst,
8 apr. 2015 10:57:022015-04-08
till ansible...@googlegroups.com
That works, thank you so much.


Svara alla
Svara författaren
Vidarebefordra
0 nya meddelanden