Skip to content

Commit

Permalink
Add a as_string argument
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterlexis committed Feb 6, 2017
1 parent 46b3646 commit 5b2e5e0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ Set `value` to `None` to accomplish this:
value: None
```

Sometimes, a number must be saved as a string in the resulting JSON file.
Setting the `as_string` argument to 'yes' will ensure this:

```yaml
- hosts: all
tasks:
- name: Set 'foo' to "25"
json_file:
dest: /etc/file.conf
key: 'foo'
value: 25
as_string: yes
```

This module supports all arguments supported by the [Ansible file](http://docs.ansible.com/ansible/file_module.html) module (like 'owner', 'group', 'mode' etcetera).

Acknowledgements
Expand Down
14 changes: 11 additions & 3 deletions library/json_file
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ options:
- the value to be associated with the I(key). May be omitted when removing a I(key).
required: false
default: null
as_string:
description:
- should the I(value) be added as a string?
default: false
backup:
description:
- Create a backup file including the timestamp information so you can get
Expand Down Expand Up @@ -103,7 +107,7 @@ def _json_dumps(s):
""" Returns the JSON-serialized version of `s` as a str"""
return json.dumps(s, indent=2, separators=(',', ': '), sort_keys=True)

def do_json(module, filename, key, value=None, state='present', create=False, backup=False):
def do_json(module, filename, key, value=None, as_string=False, state='present', create=False, backup=False):
diff = {'before': '',
'after': ''}

Expand Down Expand Up @@ -151,7 +155,9 @@ def do_json(module, filename, key, value=None, state='present', create=False, ba
# it was already gone
pass
elif state == 'present':
val = _to_type(value)
val = value
if not as_string:
val = _to_type(value)
if parts[-1] not in ref:
ref[parts[-1]] = val
changed = True
Expand Down Expand Up @@ -185,6 +191,7 @@ def main():
dest=dict(required=True),
key=dict(required=True),
value=dict(required=False),
as_string=dict(default='no', type='bool'),
backup=dict(default='no', type='bool'),
state=dict(default='present', choices=['present', 'absent']),
create=dict(default=True, type='bool')
Expand All @@ -199,8 +206,9 @@ def main():
state = module.params['state']
backup = module.params['backup']
create = module.params['create']
as_string = module.params['as_string']

(changed, backup_file, diff, msg) = do_json(module, dest, key, value, state, create, backup)
(changed, backup_file, diff, msg) = do_json(module, dest, key, value, as_string, state, create, backup)

if not module.check_mode and os.path.exists(dest):
file_args = module.load_file_common_arguments(module.params)
Expand Down
12 changes: 12 additions & 0 deletions tests/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,15 @@
with_items:
- { 'key': 'a\.b', 'value': 'bar' }
- { 'key': 'd.a\.b', 'value': 'buzz' }

- name: Add different types, as string
json_file:
dest: '/tmp/10.json'
key: '{{ item.key }}'
value: '{{ item.value }}'
as_string: yes
with_items:
- { 'key': 'integer', 'value': 25 }
- { 'key': 'bool1', 'value': true }
- { 'key': 'bool2', 'value': false }
- { 'key': 'should_be_null', 'value': None }
13 changes: 13 additions & 0 deletions tests/test_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,16 @@ def test_dotted(Command):
'a.b': 'buzz'
}
}

def test_types_as_string(Command):
stdout = Command("cat /tmp/10.json").stdout
data = json.loads(stdout)

assert data == {
'a': 'b',
'c': {'d': 'e'},
'integer': '25',
'bool1': 'True',
'bool2': 'False',
'should_be_null': 'None'
}

0 comments on commit 5b2e5e0

Please sign in to comment.