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是基于模块工作的,自身是没有批量部署的能力.真正具有批量 ...
随机推荐
- 关于java泛型的使用方式。。。。
转自:http://onewebsql.com/blog/generics-extends-super 以下基本够用了 Today we continue our mini-series on Jav ...
- 【textarea】在JSP上添加textarea-文本域 调试使用
<body> <form name="dataEventDisplay"> <table border="2" bordercol ...
- 术语-服务:BaaS
ylbtech-术语-服务:BaaS BaaS(后端即服务:Backend as a Service)公司为移动应用开发者提供整合云后端的边界服务. 1.返回顶部 1. 中文名:后端即服务 外文名:B ...
- 9-16Jenkins-2定时任务
定时任务 选择定时任务,点击 "?" 会弹出使用教程,但讨厌英语的人,自然选择现成的中文. 猪脚踏浪https://www.cnblogs.com/zsg88/p/9178625. ...
- [UE4]计算机中的数据表示
一.位:数据的最小单位,一个位仅有两种状态 一个电路,通或断:磁盘上的小磁铁,南极或北极: 使用Bit表示位,百兆带宽,一秒钟可以传输一百兆个bit 二.字节:8个位组成一个字节,一个字节有256种状 ...
- Spring mvc的web.xml配置详解
1.spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name) 2.在web.xml配置监听器ContextLoaderListener(l ...
- freePBX汉化方法记录——备忘
FreePBX汉化[root@bgcc69:/var/www/html/admin/i18n/zh_CN/LC_MESSAGES]$pwd/var/www/html/admin/i18n/zh_CN/ ...
- mysql 字符集排查
mysql 字符集排查 库级别 SELECT * FROM information_schema.schemata WHERE schema_name NOT IN ( 'information_sc ...
- spring boot 静态变量注入配置文件
spring 静态变量注入 spring 中不支持直接进行静态变量值的注入,我们看一下代码: @Component(value = "KafkaConfig") @Configur ...
- DOS 批处理命令For循环命令详解
for命令是一种对一系列对象依次循环执行同一个或多个命令的在命令行或批处理中运行的命令,结合一些Windows管理中的程序后,其处理功能强大.应用灵活方便程度令人刮目相看 for命令是一种对一系列 ...