ansible-ghetto-json icon indicating copy to clipboard operation
ansible-ghetto-json copied to clipboard

Ansible list workaround support

Open hanynowsky opened this issue 7 years ago • 5 comments

Ansible list workaround support

hanynowsky avatar Oct 10 '17 08:10 hanynowsky

I get some sort of strange unicode issues with this.

Using

- ghetto_json:
    path: /tmp/foo.json
    bar:
      - 'bat'

I get result

  "bar": [
    "u'bat'"
  ],

Python 2.7.12, ansible 2.4.1.0

To make things work, I replaced the meat of your if clause with target=eval(target). That'll be much more robust in the face of values containing [, ], or ", but it's eval. :)

bryanlarsen avatar Nov 01 '17 15:11 bryanlarsen

It s working fine for me. did you notice the (jinja filter : to_json):

ghetto_json: path=/etc/sensu/config.json client.keepalive.handlers='{{managed_keepalive_handlers|to_json}}'

  • python 2.7, Ansible 2.3
---
managed_keepalive_handlers:
  - halive
  - pagerduty

hanynowsky avatar Nov 01 '17 17:11 hanynowsky

No, I missed that. to_json isn't as easy to use inline as it is with variables. But given that you're requiring json, how about using json.loads instead of the string.replace hacks? Similar idea to my eval, just not dangerous. :)

bryanlarsen avatar Nov 01 '17 17:11 bryanlarsen

Yeah. Pretty much simpler and efficient. Please go ahead and commit the changes.

hanynowsky avatar Nov 01 '17 17:11 hanynowsky

Please see https://github.com/FauxFaux/ansible-ghetto-json/issues/6#issuecomment-594509703 for a solution to support multiple use-cases in a single call.

dagwieers avatar Mar 04 '20 13:03 dagwieers