本篇文章主要是介绍ansible服务模块和组模块的使用。

主要模块为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

服务的状态

在查看模块的参数的时候,可以使用命令: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的字样。

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"

}

当服务正在运行的时候,显示为changed为false,state显示为状态,表示为正在运行

[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"

}

当服务停止的时候,显示为changed为true,表示这个时候将服务进行了启动,状态为启动

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"

}

服务为启动状态,从而将服务停止,所以changed为真,最后状态为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"

}

服务不论为啥状态,都会将服务进行重启,然后changed为真,最后状态修改为启动

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"

}

主要使用的选项为enabled,设定为yes表示为开机启动,设定为no表示开机不启动

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"

}

表示仅仅只重启eth0的网络。

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"

}

在使用pattern的时候,表示进程运行的基础是pattern,例如上面的例子中,nagios服务启动是基于/usr/local/nagios//bin/nagios,在查询的时候使用是的ps

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

}

指定组的gid为550,并且创建为系统组,在创建为系统组的时候,表示创建组的GID在指定的范围之内,有的系统表示GID在500以下的为系统组

ansible服务模块和组模块使用的更多相关文章

  1. CentOS7Linux中自动化运维工具Ansible的安装,以及通过模块批量管理多台主机

    使用自动化运维工具Ansible集中化管理服务器 Ansible概述 Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具.它用Python写成,类似于saltstack和Puppet ...

  2. ansible学习系列2-ansible常用模块使用

    1. 查看支持的模块 [root@localhost ~]# ansible-doc -l 这里我们看下ansible的支持的模块个数 [root@localhost ~]# ansible-doc ...

  3. ansible学习基础知识和模块(一)

    基础知识补充: 常用自动化运维工具 Ansible:使用python来开发的,无需设置Agentless(代理),一般管理几百台.与ssh的方式也不一样,ssh是基于c/s模式(客户端+服务器)来使用 ...

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

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

  5. ansible环境部署及常用模块总结 - 运维笔记

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

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

    Ansible命令使用 Ansible语法使用ansible <pattern_goes_here> -m <module_name> -a <arguments> ...

  7. Ansible基础配置与常用模块使用

    环境介绍: Ansible服务端IP:192.168.2.215 Ansible客户端IP:192.168.2.216.192.168.2.218.192.168.2.113   一.创建Ansibl ...

  8. 10.Python之Ansible自动化运维常用模块

    Ansible中文权威文档:http://www.ansible.com.cn/docs/ Ansible从入门到精通:https://www.bilibili.com/video/av3361175 ...

  9. ansible 的安装及常见模块使用

    ansible 基础keys的ssh协议配置的 特性:幂等性:一个任务执行1遍和执行n遍效果一样. ansible是个管理软件不是服务,不需要长期运行  一.通过epel源安装ansible, 1.下 ...

随机推荐

  1. [IT] 关闭笔记本的蜂鸣提示

    很久没有写什么了,今天开过workshop, 稍微放松些, 一时动念上来看看,没想到最近一篇都是2010年的了, 不得不感叹时光流逝之快啊. 那就写点什么吧. 现在每天工作用的DELL笔记本,性能差强 ...

  2. 10.cadence.自定义焊盘的创建[原创]

    一.自定义图形焊盘 1.设置环境(面板大小,格点) --- ------ 圆形 Shape > Circular ---- 两个DRC错误,证明图形重合了, 将图形复合一下: --- 椭圆类焊盘 ...

  3. python3代码

    import urllib.request url="http://mm.taobao.com/json/request_top_list.htm?type=0&page=1&quo ...

  4. [Python]计算豆瓣电影TOP250的平均得分

    用python写的爬虫练习,感觉比golang要好写一点. import re import urllib origin_url = 'https://movie.douban.com/top250? ...

  5. Linq 学习笔记

    简介: LINQ 提供一种统一的方式,让我们能在C#语言中直接查询和操作各种数据.​​ LINQ是用来描述数据访问总体方式的术语.LINQ to Object是针对实现了IEnumerable< ...

  6. cdoj 1324 卿学姐与公主 线段树裸题

    卿学姐与公主 Time Limit: 2000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit St ...

  7. [ionic开源项目教程] - 第3讲 左右滑动菜单的实现(使用Tabs和SlideBox)

    使用Tabs和SlideBox实现左右滑动菜单 1.将tab1.html的代码改为如下: <ion-view view-title="健康"> <ion-cont ...

  8. 今天发现猎豹浏览器的一个大坑 Request.IsAuthenticated 一直为 false;另外附加原因以及临时的解决方法

    今天掉到了一个大坑里面,爬了1个多小时才发现不是代码的问题,居然是浏览器的问题… 下面是问题的发生过程 单点登陆  有2个站点  http://a.abc.com  http://b.abc.com ...

  9. jquery总结(1)

    jquery是一种js对象.里面封装了一些方法,但是jquery对象不能直接使用js方法,js对象不能直接使用jquery方法. jquery对象类似于js对象的集合,就是存在形式是以特殊数组的形式: ...

  10. Mysql自定义函数之------------This function has none of DETERMINISTIC, NO SQL解决办法

    This function has none of DETERMINISTIC, NO SQL解决办法 创建存储过程时 出错信息: ERROR 1418 (HY000): This function ...