Created
October 15, 2015 17:41
-
-
Save juliedavila/debdd52e0f48e67069ca to your computer and use it in GitHub Desktop.
Training Solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import urllib | |
import os | |
GH_URL = 'https://raw.githubusercontent.com/jsmartin/lightbulb/master/lessons/common_role/roles/apache/templates/index.html.j2' | |
def download(module, url, destination): | |
opener = urllib.URLopener() | |
opener.retrieve(url, destination) | |
return True | |
def already_there(module, path): | |
if os.path.exists(path): | |
return True | |
else: | |
return False | |
def main(): | |
module = AnsibleModule( | |
argument_spec = dict( | |
path=dict(required=True), | |
state=dict(required=True, choices=['present', 'absent']) | |
) | |
) | |
path = module.params['path'] | |
state = module.params['state'] | |
full_path = os.path.join(path, 'index.html.j2') | |
exists = already_there(module, full_path) | |
if exists: | |
if state == 'absent': | |
os.remove(full_path) | |
changed = True | |
else: | |
changed = False | |
else: | |
if state == 'present': | |
download(module, GH_URL, full_path) | |
changed = True | |
else: | |
changed = False | |
return module.exit_json(changed=changed, state=state, path=full_path) | |
# 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