Skip to content

Instantly share code, notes, and snippets.

@juliedavila
Created November 12, 2015 17:24
Show Gist options
  • Save juliedavila/bc4cb3f538105582b5e0 to your computer and use it in GitHub Desktop.
Save juliedavila/bc4cb3f538105582b5e0 to your computer and use it in GitHub Desktop.
import os
def main():
module = AnsibleModule(
argument_spec = dict(
state = dict(required=True, choices=['present', 'absent']),
name = dict(required=True),
content = dict(required=True)
),
supports_check_mode = False,
)
state = module.params['state']
name = module.params['name']
content = module.params['content']
path = '/tmp/%s' % name
if state == 'present':
if os.path.exists(path):
f = open(path, 'r')
current_contents = f.read()
if current_contents == content:
module.exit_json(changed=False, path=path, content=content)
else:
f.close()
f.open(path, 'w')
f.write(content)
module.exit_json(changed=True, path=path, content=content)
else:
f = open(path, 'w')
f.write(content)
module.exit_json(changed=True, path=path, content=content)
else:
if os.path.exists(path):
os.remove(path)
module.exit_json(changed=True, path=path, msg="Deleted")
else:
module.exit_json(changed=False, path=path, msg="Deleted")
# import module snippets (must stay here after your code)
from ansible.module_utils.basic import *
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment