Created
August 6, 2012 15:32
-
-
Save dax/3275616 to your computer and use it in GitHub Desktop.
Ansible playbook
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 subprocess | |
import re | |
import traceback | |
import tempfile | |
def execute(args, module, shell=None): | |
try: | |
cmd = subprocess.Popen(args, shell=shell, | |
stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
out, err = cmd.communicate() | |
return out, err, cmd.returncode | |
except (OSError, IOError), e: | |
module.fail_json(cmd=args, msg=str(e)) | |
except: | |
module.fail_json(msg=traceback.format_exc()) | |
def main(): | |
module = AnsibleModule( | |
argument_spec = dict( | |
key = dict(required=True), | |
uid = dict(required=True), | |
) | |
) | |
key = module.params['key'] | |
uid = module.params['uid'] | |
out, err, returncode = execute(["apt-key list | grep '" + uid + "'"], | |
module, shell="/bin/sh") | |
if returncode == 0: | |
module.exit_json(changed=False, key=key, uid=uid) | |
else: | |
tmp_file = tempfile.NamedTemporaryFile() | |
tmp_file.write(key) | |
tmp_file.flush() | |
out, err, returncode = execute(["apt-key", "add", tmp_file.name], module) | |
tmp_file.close() | |
if returncode == 0: | |
module.exit_json(changed=True, key=key, uid=uid) | |
else: | |
module.fail_json(msg=str(err), key=key, uid=uid) | |
# include magic from lib/ansible/module_common.py | |
#<<INCLUDE_ANSIBLE_MODULE_COMMON>> | |
main() |
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
$ ~/ansible/hacking/test-module -m ~/ansible/library/apt_key -a "key='{% import files/cloudera.key %}' uid='Cloudera Apt Repository'" | |
* including generated source, if any, saving to: /root/.ansible_module_generated | |
* this will offset any line numbers in tracebacks/debuggers! | |
*********************************** | |
RAW OUTPUT | |
{"msg": "gpg: no valid OpenPGP data found.\n", "failed": true, "uid": "Cloudera Apt Repository", "key": "{% import files/cloudera.key %}"} | |
*********************************** | |
PARSED OUTPUT | |
{ | |
"failed": true, | |
"key": "{% import files/cloudera.key %}", | |
"msg": "gpg: no valid OpenPGP data found.\n", | |
"uid": "Cloudera Apt Repository" | |
} |
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
--- | |
- name: Add Cloudera APT repository | |
action: copy src=files/cloudera.list dest=/etc/apt/sources.list.d/cloudera.list | |
notify: | |
- update APT cache | |
- name: Add Cloudera key | |
action: apt_key key="{% import files/cloudera.key %}" uid="Cloudera Apt Repository" | |
notify: | |
- update APT cache |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment