ansible服务模块和组模块使用
2016-01-15 21:25
381 查看
本篇文章主要是介绍ansible服务模块和组模块的使用。
主要模块为ansible service module和ansible group moudle,下面的内容均是通过实践得到,可以直接运行相关的代码进行测试。
注意参数均为小写。
在查看模块的参数的时候,可以使用命令:ansible-doc -s service(查看服务的帮助手册)
在使用参数的时候,name是必须写上的。
Controls services on remote hosts. Supported initsystems include BSD init, OpenRC, SysV, Solaris SMF, systemd, upstart.
在下面的代码中,ansible的配置文件中设置了询问ssh的密码,也就是每次都会出现ssh password的字样。
当服务正在运行的时候,显示为changed为false,state显示为状态,表示为正在运行
当服务停止的时候,显示为changed为true,表示这个时候将服务进行了启动,状态为启动
服务为启动状态,从而将服务停止,所以changed为真,最后状态为stopped
服务不论为啥状态,都会将服务进行重启,然后changed为真,最后状态修改为启动
将服务进行重新加载,差不多就是重新启动服务。
表示在服务器上不存在此服务,报错中信息表示可能服务的名称写错。
主要使用的选项为enabled,设定为yes表示为开机启动,设定为no表示开机不启动
表示仅仅只重启eth0的网络。
在使用pattern的时候,表示进程运行的基础是pattern,例如上面的例子中,nagios服务启动是基于/usr/local/nagios//bin/nagios,在查询的时候使用是的ps
主要就是用来删除一个组,然后添加一个组
指定组的gid为550,并且创建为系统组,在创建为系统组的时候,表示创建组的GID在指定的范围之内,有的系统表示GID在500以下的为系统组
主要模块为ansible service module和ansible group moudle,下面的内容均是通过实践得到,可以直接运行相关的代码进行测试。
注意参数均为小写。
1、服务模块使用
服务模块的参数列表如下所示:参数 | 必填 | 默认值 | 选项 | 说明 |
Arguments | 否 | - | - | 参数 |
Enabled | 否 | - | Yes/no | 开机自动启动 |
Name= | 否 | - | - | 服务名称 |
Pattern | 否 | - | - | 如果服务没有响应,则ps查看是否具有指定参数的进程,有则认为服务已经启动 |
Runlevel | 否 | Default | 服务启动级别(仅仅在debain系统中) | |
Sleep | 否 | - | - | 如果服务被重新启动,则睡眠多少秒再执行停止和启动命令(restarted状态) |
State | 否 | - | running, started, stopped, restarted, reloaded | 服务的状态 |
在使用参数的时候,name是必须写上的。
Controls services on remote hosts. Supported initsystems include BSD init, OpenRC, SysV, Solaris SMF, systemd, upstart.
在下面的代码中,ansible的配置文件中设置了询问ssh的密码,也就是每次都会出现ssh password的字样。
1.1 检查服务状态为running
无论服务处在什么状态,最后都是将服务状态设置为启动[root@ansibleserver ansible]# ansible nagiosserver -m service -a "name=nagios state=running" SSH password: 192.168.1.20 | success >> { "changed": false, "name": "nagios", "state": "started" } |
[root@ansibleserver ansible]# ansible nagiosserver -m service -a "name=nagios state=running" SSH password: 192.168.1.20 | success >> { "changed": true, "name": "nagios", "state": "started" } |
1.2 将服务停止
[root@ansibleserver ansible]# ansible nagiosserver -m service -a "name=nagios state=stopped" SSH password: 192.168.1.20 | success >> { "changed": true, "name": "nagios", "state": "stopped" } |
1.3 将服务重新启动
[root@ansibleserver ansible]# ansible nagiosserver -m service -a "name=nagios state=restarted" SSH password: 192.168.1.20 | success >> { "changed": true, "name": "nagios", "state": "started" } |
1.4 将服务重新加载
[root@ansibleserver ansible]# ansible nagiosserver -m service -a "name=nagios state=reloaded" SSH password: 192.168.1.20 | success >> { "changed": true, "name": "nagios", "state": "started" } |
1.5 当不存在服务的时候
[root@ansibleserver ~]# ansible -i hosts kel -m service -a "name=nagios state=stopped" SSH password: 192.168.1.60 | FAILED >> { "failed": true, "msg": "cannot find 'service' binary or init script for service, possible typo in service name?, aborting" } |
1.6 将服务设置为开机启动
[root@ansibleserver ansible]# ansible nagiosserver -m service -a "name=nagios enabled=no" SSH password: 192.168.1.20 | success >> { "changed": true, "enabled": false, "name": "nagios" } [root@ansibleserver ansible]# ansible nagiosserver -m service -a "name=nagios enabled=yes" SSH password: 192.168.1.20 | success >> { "changed": true, "enabled": true, "name": "nagios" } |
1.7 添加额外的参数args
[root@ansibleserver ansible]# ansible nagiosserver -m service -a "name=network state=restarted args=eth0" SSH password: 192.168.1.20 | success >> { "changed": true, "name": "network", "state": "started" } |
1.8 pattern的使用
[root@ansibleserver ansible]# ansible nagiosserver -m service -a "name=nagios pattern=/usr/local/nagios//bin/nagios state=restarted" SSH password: 192.168.1.20 | success >> { "changed": true, "name": "nagios", "state": "started" } |
2、组模块的使用
组模块主要对应的命令为groupadd,groupdel,groupmod。参数 | 必填 | 默认 | 选项 | 说明 |
Gid | 非 | 组的gid | ||
Name | 是 | 组名称 | ||
State | 非 | Present | Present/absent | 添加删除组 |
System | 非 | 非 | Yes/no | 是否属于系统组 |
2.1 添加删除组
[root@ansibleserver ansible]# ansible pythonserver -m group -a "name=kellyse state=absent" SSH password: 192.168.1.60 | success >> { "changed": true, "name": "kellyse", "state": "absent" } [root@ansibleserver ansible]# ansible pythonserver -m group -a "name=kelly state=present" SSH password: 192.168.1.60 | success >> { "changed": true, "gid": 501, "name": "kelly", "state": "present", "system": false } |
2.2 指定gid等创建组
[root@ansibleserver ansible]# ansible pythonserver -m group -a "name=kelly gid=550 system=yes state=present" SSH password: 192.168.1.60 | success >> { "changed": true, "gid": 550, "name": "kelly", "state": "present", "system": true } |
相关文章推荐
- Codeforces Round #339 (Div. 2)-D. Skills
- 转义符大全
- jquery.form.js使用
- ansible服务模块和组模块使用
- Java语言概述
- codeforces 614B(div.2) 模拟
- 手把手教你自制一寸两寸照
- Sed
- win7 c盘越用越少的原因—win7 c盘空间不够的解决方法
- python list循环语句的使用方法
- 数据库重点知识总结(二)
- IOS动画中的枚举UIViewAnimationOptions
- HDOJ1713(相遇周期)(有点坑)
- Android界面设计的4种方式之二——在Java代码中控制UI界面
- Android截屏(包括未显示部分)
- 总结下自己的入门学黑之路,希望对各位一些帮助!少走点弯路! --写文时间2011-10-26
- grep的妙用
- Codeforces 615 D Multipliers
- UVA 12563:jin ge jin qu hao
- Redis与Memcached的区别