ansible之service和server模块

service模块介绍
例:启动指定节点上的httpd 服务,并让其开机自启动

[root@master tmp]# ansible client02 -m service -a 'name=httpd state=restarted enabled=yes'
client02 | SUCCESS => {
    "changed": true, 
    "enabled": true, 
    "name": "httpd", 
    "state": "started"
}
[root@client01 tmp]# ps -ef|grep httpd|wc -l                                
1

[root@client02 tmp]# ps -ef|grep httpd|wc -l
9

server模块介绍

可以提供的status:running,started,stopped,restarted,reloaded

[root@master tmp]# cat /etc/ansible/hosts
[localhost]
master.test.com
[slave]
client02
client01

此处参数  -s 意思是
run operations with sudo (nopasswd) (deprecated, use
                        become)
结尾参数-k 意思是要让输入ssh 密码

例:检查某节点的httpd 服务
[root@master ~]# ansible slave -m service -a "name=httpd state=running" –s

[root@master tmp]# ansible client02 -m service -a "name=httpd state=running" -s
client02 | SUCCESS => {
    "changed": false, 
    "name": "httpd", 
    "state": "started"
}

[root@master tmp]# ansible slave -m service -a "name=httpd state=stopped" -k
SSH password: 111111
client01 | SUCCESS => {
    "changed": false, 
    "name": "httpd", 
    "state": "stopped"
}
client02 | SUCCESS => {
    "changed": false, 
    "name": "httpd", 
    "state": "stopped"
}

[root@master tmp]# ansible client02 -m service -a "name=httpd state=status" -s
client02 | FAILED! => {
    "changed": false, 
    "failed": true, 
    "msg": "value of state must be one of: running,started,stopped,restarted,reloaded, got: status"
}

[root@master tmp]# ansible client02 -m service -a "name=httpd state=stopped" -s
client02 | SUCCESS => {
    "changed": true, 
    "name": "httpd", 
    "state": "stopped"
}

[root@master tmp]# ansible client01 -m service -a "name=httpd state=running" -s

paramiko: The authenticity of host 'client01' can't be established.
The ssh-rsa key fingerprint is 3d906ef1d450e4cc7031aef5e8c296f6.
Are you sure you want to continue connecting (yes/no)?
yes
client01 | SUCCESS => {
    "changed": true, 
    "name": "httpd", 
    "state": "started"
}

[root@master tmp]# ansible slave -m service -a "name=httpd state=started" -s

client02 | SUCCESS => {
    "changed": true, 
    "name": "httpd", 
    "state": "started"
}
client01 | SUCCESS => {
    "changed": true, 
    "name": "httpd", 
    "state": "started"
}
[root@client01 tmp]# ps -ef|grep httpd|wc -l
10
[root@client02 tmp]# ps -ef|grep httpd|wc -l
10

[root@master tmp]# ansible slave -m service -a "name=httpd state=stopped" -s
client02 | SUCCESS => {
    "changed": true, 
    "name": "httpd", 
    "state": "stopped"
}
client01 | SUCCESS => {
    "changed": true, 
    "name": "httpd", 
    "state": "stopped"
}

[root@client01 tmp]# ps -ef|grep httpd|wc -l
1
[root@client02 tmp]# ps -ef|grep httpd|wc -l
1

转载于:https://blog.51cto.com/wujianwei/2082900

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值