前言
- 服務器運維時,經(jīng)常會依次操作每臺服務器執(zhí)行相同的命令,重復操作顯然效率低下
- 本文推薦使用工具ansible,批量操作服務器
- 我這里先準備3臺服務器
IP地址
192.168.88.5
192.168.88.6
192.168.88.7
安裝及配置
# 安裝 ansible,你想用哪臺服務器批量操作就給哪臺安裝
yum -y install ansible
# 編輯配置文件,配置ip組
vim /etc/ansible/hosts
# 給配置文件添加ip組,如下內(nèi)容,我這里建了4個組
[my]
192.168.88.5
192.168.88.6
192.168.88.7
[a]
192.168.88.5
[b]
192.168.88.6
[c]
192.168.88.7
設置無密訪問
ssh-keygen
ssh-copy-id root@192.168.88.5
ssh-copy-id root@192.168.88.6
ssh-copy-id root@192.168.88.7
測試是否連通
ansible my -m ping
發(fā)送指令到ip組,成功會返回結(jié)果
# my組3臺服務器執(zhí)行date命令
ansible my -m shell -a "date"
# my組3臺服務器執(zhí)行ip addr | grep 88
ansible my -m shell -a "ip addr | grep 88"
# my組臺服務器停止redis服務
ansible my -m shell -a "/sbin/service redis stop"
# 操作b組,執(zhí)行date命令
ansible b -m shell -a "date"
效果演示
[root@localhost redis]# ansible my -m shell -a "date"
192.168.88.5 | CHANGED | rc=0 >>
Wed Mar 3 03:25:43 CST 2021
192.168.88.7 | CHANGED | rc=0 >>
Wed Mar 3 03:25:43 CST 2021
192.168.88.6 | CHANGED | rc=0 >>
Wed Mar 3 03:25:43 CST 2021
[root@localhost redis]# ansible test -m shell -a "ip addr | grep 88"
192.168.88.5 | CHANGED | rc=0 >>
inet 192.168.88.5/24 brd 192.168.88.255 scope global noprefixroute ens32
192.168.88.6 | CHANGED | rc=0 >>
inet 192.168.88.6/24 brd 192.168.88.255 scope global noprefixroute ens32
192.168.88.7 | CHANGED | rc=0 >>
inet 192.168.88.7/24 brd 192.168.88.255 scope global noprefixroute ens32
inet 192.168.88.88/24 scope global secondary ens32
[root@localhost redis]# ansible ^Cm shell -a "/sbin/service redis stop"
[root@localhost redis]# ^C
[root@localhost redis]# ^C
[root@localhost redis]# ansible my -m shell -a "date"
192.168.88.5 | CHANGED | rc=0 >>
Wed Mar 3 03:26:24 CST 2021
192.168.88.7 | CHANGED | rc=0 >>
Wed Mar 3 03:26:25 CST 2021
192.168.88.6 | CHANGED | rc=0 >>
Wed Mar 3 03:26:24 CST 2021
[root@localhost redis]# ansible my -m shell -a "ip addr | grep 88"
192.168.88.6 | CHANGED | rc=0 >>
inet 192.168.88.6/24 brd 192.168.88.255 scope global noprefixroute ens32
192.168.88.5 | CHANGED | rc=0 >>
inet 192.168.88.5/24 brd 192.168.88.255 scope global noprefixroute ens32
192.168.88.7 | CHANGED | rc=0 >>
inet 192.168.88.7/24 brd 192.168.88.255 scope global noprefixroute ens32
inet 192.168.88.88/24 scope global secondary ens32
[root@localhost redis]# ansible my -m shell -a "/sbin/service redis stop"
[WARNING]: Consider using the service module rather than running 'service'. If you need to use command because service is insufficient you can
add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
192.168.88.6 | CHANGED | rc=0 >>
Stopping ...
Redis stoppedWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.88.5 | CHANGED | rc=0 >>
/www/server/redis/src/redis-cli -h 0.0.0.0 -p 6379 -a 123123
Stopping ...
Redis stoppedWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.88.7 | CHANGED | rc=0 >>
Stopping ...
Redis stoppedWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
[root@localhost redis]# ansible b -m shell -a "date"
192.168.88.6 | CHANGED | rc=0 >>
Wed Mar 3 03:27:25 CST 2021
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。