DSPy a system for AI to Write Prompts and Do Fine Tuning
Extending ansible
1. Create Your Own Module
Yan Kurniawan, March 2015
EXTENDING ANSIBLE
2. ABOUT ME
• Cloud Engineer at Flux7 (http://flux7.com), a US-based AWS
Consulting Partner
• Writer of “Ansible for AWS” book
http://leanpub.com/ansible-for-aws
3. ANSIBLE MODULES
• "batteries-included" philosophy
Useful out-of-the-box modules
• Over 200 core modules available
http://docs.ansible.com/modules_by_category.html
10. WRITING CUSTOM MODULE
• Any language (Python, Bash, C++, PHP, clojure, Ruby, etc); the only
requirement is being able to read/write files and write to stdout
• Output of the module should be in JSON format
• Recommended: Python
Boilerplate available; reduce the amount of code required
Arguments are handled automatically
Output is automatically converted to JSON
You can learn from Ansible core modules
https://github.com/ansible/ansible-modules-core
Chance to contribute to the Ansible project
12. THE BOILERPLATE
def main():
module = AnsibleModule(
argument_spec = dict(
arg1 = dict(choices=SOME_CHOICES),
arg2 = dict(required=False)
),
)
# Your code here
# import module snippets
from ansible.module_utils.basic import *
main()
13. DEMO
• A simple module to return Amazon EC2 instance id(s)
using tags as input argument.
• Save the module as instance_lookup in
library/ directory
• Use the return value to start/stop/terminate
instance(s) using Ansible ec2 module.
17. SOME CODES
if region:
try:
ec2 = connect_to_region(region)
except boto.exception.NoAuthHandlerFound, e:
module.fail_json(msg=str(e))
else:
module.fail_json(msg="region must be specified")
instance_ids = []
for tag, value in tags.iteritems():
for instance in ec2.get_only_instances
(filters={'tag:' + tag: value}):
instance_ids.append(instance.id)
20. THE PLAYBOOK
- name: get instance id
instance_lookup:
region: "{{ region }}”
tags:
Name: test01
register: instanceid
- name: start instance
ec2:
region: "{{ region }}"
instance_ids: "{{ instanceid.instance_ids }}"
state: running
wait: yes
when: instanceid is defined
21. MORE RESOURCES
• Module development guideline
http://docs.ansible.com/developing_modules.html
• Boto reference
https://boto.readthedocs.org/en/latest/