def cnf(action,configs,path):
message = "Modify %s\n" %path
changed = False
if action == "get":
with open(path,"r") as f:
message = f.read()
return changed,message
if not os.path.exists(path):
message += "%s do not exist, create it\n" %path
cnf = Mycnf(path)
if action == "update":
changed,msg = cnf.set_all(configs)
message += msg
elif action == "delete":
changed, msg = cnf.delete_all(configs)
message += msg
else:
message = "Invalid action"
return changed,message def main():
module = AnsibleModule(
argument_spec=dict(
type=dict(default="cnf"),
action=dict(default="update"),
configs=dict(),
path=dict(default="/etc/my.cnf"),
),
supports_check_mode=True
) type = module.params["type"]
action = module.params["action"]
configs = module.params["configs"]
path = module.params["path"] if type not in TYPE:
module.fail_json(msg="Invalid type %s,must be in %s" %(type,str(TYPE)))
if action not in ACTION:
module.fail_json(msg="Invalid action %s,must be in %s" %(action,str(ACTION)))
try:
configs = json.loads(configs)
except:
module.fail_json(msg="configs must be a json type")
if action == "get" and not os.path.exists(path):
module.fail_json(msg="%s do not exist" %path)
try:
cmd = globals()[type]
changed, msg = cmd(action,configs,path)
module.exit_json(changed=changed, stdout=msg)
except Exception as e:
module.fail_json(msg=e.message) from ansible.module_utils.basic import * if __name__ == '__main__':
main() ======================================================================
- hosts: localhost
  tasks:
 
  - name: test my module
    config_file:
      type: cnf
      action: delete
      configs: '{"mysqld":{"mhc":"2b"}}'
      path: /root/test/ansible_test/mhc_test/my.cnf
    register: mhc
 
  - debug: var=mhc    

Ansible develop module的更多相关文章

  1. Ansible Notes: module: get_facts

    功能:用来获取remote host的facts 它是一个非常基础的模块[1],playbook里面可以直接当关键字用gather_facts: False 执行set_up模块时自动调用get_fa ...

  2. ansible服务模块和组模块使用

    本篇文章主要是介绍ansible服务模块和组模块的使用. 主要模块为ansible service module和ansible group moudle,下面的内容均是通过实践得到,可以直接运行相关 ...

  3. ansible基础-安装与配置

    一 安装 1.1 ansible架构 ansible是一个非常简单的自动化部署项目,由python编写并且开源.用于提供自动化云配置.配置文件管理.应用部署.服务编排和很多其他的IT自动化需求. an ...

  4. Gitlab_ansible_jenkins三剑客③Ansible的安装及使用

    一台服务器可能会安装不同的python应用,不同的应用可能使用的模块版本不同,如果都安装在同样的环境下容易冲突,为了避免冲突,引入virtualenv 这个包管理工具进行环境的隔离 使用pip安装之前 ...

  5. Ansible安装部署以及常用模块详解

    一.  Ansible 介绍Ansible是一个配置管理系统configuration management system, python 语言是运维人员必须会的语言, ansible 是一个基于py ...

  6. 【原创】运维基础之Ansible(1)简介、安装和使用

    官方:https://www.ansible.com/ 一 简介 Ansible is a radically simple IT automation engine that automates c ...

  7. 轻量级集群管理软件-Ansible

    ansible概述和运行机制 ansible概述 Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具,  它用Python写成,类似于saltstack和Puppet,但是有一个不同 ...

  8. Ansible 安装与配置(一)

    公司大概有200多云主机需要进行管理,但是如果通过手工管理费时还累最终结果也容易出错,所以考虑通过自动化的方式来管理云主机,目前开源的自动化工具,大家用的比较多的有Ansible和Saltstack这 ...

  9. ansible简介,简单实用

    Ansible ansilbe是实现自动化运维的工具,基于python开发,实现批量系统配置,批量程序部署,批量运行命令等功能. ansible是基于模块工作的,自身是没有批量部署的能力.真正具有批量 ...

随机推荐

  1. 织梦SQL标签的使用

    (>=DedeCMS 3,DedeCMS 4,DedeCMS 5) 名称:sql 功能:用于获取MySQL数据库内容的标签 语法: 1 2 3 {dede:sql sql='' appname= ...

  2. 一款easyUI写的界面例子,小记

    后台是用strut2的: 前台界面风格easyUI,感觉挺好用的: <!DOCTYPE html> <html> <head> <meta charset=& ...

  3. canvas基础一

    使用HTML5中<canvas>元素可以在页面中设定一个区域,然后通过JavaScript动态地在这个区域中绘制图形,要在这块画布(canvas)上绘图,需要取得绘图上下文,而取得绘图上下 ...

  4. ES之八:elasticsearch2.x下的JAVA API示例

    D:\soft\elasticsearch\elasticsearch-2.1.0\lib package com.dxz.es; import java.net.InetAddress; impor ...

  5. java study文件读写

    文件读写 如果在代码中写入大量的数据,会增加代码的冗余度,通过读取文件的方式,可以精简代码,便于数据的修改和代码的维护 IO流的分类:字节流和字符流 字符流 字符输出流:写文本文件的,抽象基类java ...

  6. 跨域获取json数据

    原文地址:http://my.oschina.net/LinBandit/blog/34570   前阵子做了一个前端动态加载json数据的应用,其中使用xmlhttprequest动态加载js,但是 ...

  7. 学学Gearman

    通常,多语言多系统之间的集成是个大问题,一般来说,人们多半会采用WebService的方式来处理此类集成问题,但不管采用何种风格的WebService,如RPC风格,或者REST风格,其本身都有一定的 ...

  8. linux查看某个目录下有哪些文件的命令

    分别是ll和ls命令 ll /usr/local/lib ls /usr/local/lib

  9. 本地管理表空间和字典管理表空间的特点,ASSM有什么特点

    字典管理表空间(Dictionary-Managed Tablespace简称DMT),8i以前包括以后都还可以使用的一种表空间管理模式,通过数据字典管理表空间的空间使用. Oracle使用两个字典来 ...

  10. 发送短信验证码及调用短信接口与C# 后台 post 发送

    #region 调用短信接口 public ActionResult Mobile(string Tel)//调用接口 { Random rm = new Random(); int i; strin ...