Ansible develop module
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的更多相关文章
- Ansible Notes: module: get_facts
功能:用来获取remote host的facts 它是一个非常基础的模块[1],playbook里面可以直接当关键字用gather_facts: False 执行set_up模块时自动调用get_fa ...
- ansible服务模块和组模块使用
本篇文章主要是介绍ansible服务模块和组模块的使用. 主要模块为ansible service module和ansible group moudle,下面的内容均是通过实践得到,可以直接运行相关 ...
- ansible基础-安装与配置
一 安装 1.1 ansible架构 ansible是一个非常简单的自动化部署项目,由python编写并且开源.用于提供自动化云配置.配置文件管理.应用部署.服务编排和很多其他的IT自动化需求. an ...
- Gitlab_ansible_jenkins三剑客③Ansible的安装及使用
一台服务器可能会安装不同的python应用,不同的应用可能使用的模块版本不同,如果都安装在同样的环境下容易冲突,为了避免冲突,引入virtualenv 这个包管理工具进行环境的隔离 使用pip安装之前 ...
- Ansible安装部署以及常用模块详解
一. Ansible 介绍Ansible是一个配置管理系统configuration management system, python 语言是运维人员必须会的语言, ansible 是一个基于py ...
- 【原创】运维基础之Ansible(1)简介、安装和使用
官方:https://www.ansible.com/ 一 简介 Ansible is a radically simple IT automation engine that automates c ...
- 轻量级集群管理软件-Ansible
ansible概述和运行机制 ansible概述 Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具, 它用Python写成,类似于saltstack和Puppet,但是有一个不同 ...
- Ansible 安装与配置(一)
公司大概有200多云主机需要进行管理,但是如果通过手工管理费时还累最终结果也容易出错,所以考虑通过自动化的方式来管理云主机,目前开源的自动化工具,大家用的比较多的有Ansible和Saltstack这 ...
- ansible简介,简单实用
Ansible ansilbe是实现自动化运维的工具,基于python开发,实现批量系统配置,批量程序部署,批量运行命令等功能. ansible是基于模块工作的,自身是没有批量部署的能力.真正具有批量 ...
随机推荐
- 【python】重定向输出
重定向的理解:就是把所要输出的结果输出到其他的地方.常用方法:"print >>",(若有其他方法后续补充) 举个例子: __author__ = 'paulwinfl ...
- 【python】专用下划线标识符说明
__xxx__:系统定义名字 __xxx:类中私有变量名 说明:__xxx看做“私有的”,在模块或者类外是不可以使用.
- unbuntu 安装及服务器配置
关于分区: 挂载点 装置 说明 / /dev/hda1 15G /home /dev/hda2 最大的剩余空间 /boot /dev/hda3 200MB左右 swap /dev/hda5 大约内存大 ...
- AWS ECU SSH无法连接问题处理
AWS ECU SSH无法连接问题处理,因同事误操作导致/var/empty/sshd目录权限为771,需要修改为711,因AWS只有一台实例,所以需要通过建立临时实例来挂载“卷”来修改/var/ ...
- bzoj2026: [SHOI2009]Coin
Description Constantine刚结束在MySky Island的度假,正准备离开的时候,他想送给她的好朋友YY一份特别的礼物——MySky Island上特别的手工艺品宝石纪念币.宝石 ...
- spring boot学习(6) SpringBoot 之事务管理
两个操作要么同时成功,要么同时失败: 事务的一致性: 以前学ssh ssm都有事务管理service层通过applicationContext.xml配置,所有service方法都加上事务操作: 用来 ...
- Mybatis学习(3)关于mybatis全局配置文件SqlMapConfig.xml
比如针对我这个项目的mybatis全局配置文件SqlMapConfig.xml做一些说明: <?xml version="1.0" encoding="UTF-8& ...
- excel 怎么添加超链接
1.只能对单元格添加超链接 2.如果要对单元格里面个别字做成超链接,可以使用图形工具,设置一个图形在里面,对这个图形做超链接 参考:https://jingyan.baidu.com/article/ ...
- 为什么 JVM 不用 JIT 全程编译
从知乎扣出来的内容 https://www.zhihu.com/question/37389356 作者:RednaxelaFX链接:https://www.zhihu.com/question/37 ...
- OSPF里几个特殊区域(stub、Totally stubby、NSSA、Totally NSSA)总结
网友总结: 简单的说,就是 stub过滤4,5类lsa,ABR会产生缺省的3类lsa,区域内不能引入外部路由 total stub过滤3,4,5类lsa,ABR会产生缺省的3类lsa,区域内不能引入外 ...