Ansible 笔记,ansible笔记


在AWS上做自动部署,项目组选择了ansible,自动化部署工具,具体细节:

What is Ansible:

http://docs.ansible.com/ansible/

形容下ansible怎么工作的,

首先你要有目标机器列表(host)然后又执行脚本的指令顺序集(playbook)

想深入的同学请看:http://sofar.blog.51cto.com/353572/1579894


总之就是I have a playbook,I have a host, Ugh! playbook running on host!

ansible天生支持python,json,ymal,但是有些aws的接口并没有很好的支撑

没办法,自己动手丰衣足食

这里就要去写custom module和custom fliter了,这里从项目的角度分为4个步骤

红色的AWS API学习,绿色的ansible模块开发,黄色的fliter开发,蓝色的playbook测试

层次一下就看出来了,AWS API和我们代码天然隔离的。

而我们要做的就是实现module和filter解耦,filter和playbook解耦

举个例子,我需要一个socket的ip和port,然后写在一个名为{address:,port:}的对象中。

AWS API 可能是{AWS Address:xxx,xxx,xxx,xxx, port:yy}

playbook如果写AWS Address:xxx,xxx,xxx,xxx, port:yy,aws接口升级了,你就得改

你也可以都写到module里边或者filter里边,但是这样就出来一个上帝类 解耦的好处什么的不赘述了

掐头去尾讲中间:

打测试桩,定义好module和filter怎么衔接,以及filter和playbook怎么衔接

模块开发:

例子:

http://blog.toast38coza.me/custom-ansible-module-hello-world/

基本上就是根据需求写python,注意module name和返回值

  • Return values must be able to be serialized as json via the python stdlib json library. basic python types (strings, int, dicts, lists, etc) are serializable. A common pitfall is to try returning an object via exit_json(). Instead, convert the fields you need from the object into the fields of a dictionary and return the dictionary.

Filter开发:

目前还没找到让它自己退出的方法,感觉就是定义函数用的。如果你不想再yaml里边写晦涩的jinja2,选择filter吧

例子:

http://www.dasblinkenlichten.com/creating-ansible-filter-plugins/


Ref:

Customer module:

http://docs.ansible.com/ansible/dev_guide/developing_modules.html

http://docs.ansible.com/ansible/playbooks_best_practices.html

https://docs.ansible.com/ansible/dev_guide/developing_plugins.html


相关内容

    暂无相关文章