背景說明
很多時(shí)候我們需要批量對主機(jī)執(zhí)行shell
畅铭,比如說設(shè)置內(nèi)核參數(shù)。一臺一臺登錄執(zhí)行配置顯然不行榄笙。
當(dāng)然ansible-playbook
是個不錯的選擇搭盾。
但ansible-playbook
并不太適用于離線環(huán)境(主要yum
不可用場景),且對新手不太友好,需要一定的學(xué)習(xí)成本秉颗。
這個時(shí)候我們就可以使用easyctl 來實(shí)現(xiàn)
安裝easyctl
選擇任意方式安裝
- 編譯安裝最新版
git clone https://github.com/weiliang-ms/easyctl.git
cd easyctl
go build -ldflags "-w -s" -o /usr/local/bin/easyctl
- 下載編譯好的文件
chmod +x easyctl
mv easyctl /usr/local/bin
兼容性
CentOS6
CentOS7
使用方式
- 生成默認(rèn)配置文件
$ easyctl exec shell
INFO[0000] 生成配置文件樣例, 請攜帶 -c 參數(shù)重新執(zhí)行 -> config.yaml
- 修改配置文件
config.yaml
中主機(jī)列表配置
easyctl
根據(jù)主機(jī)列表ssh
遠(yuǎn)程至目標(biāo)主機(jī)執(zhí)行shell
server:
# - host: 10.79.165.[11:13]
# username: root
# password: neusoft
# port: 22
# - host: 10.79.164.[61:63]
# username: root
# password: neusoft
# port: 22
- host: 10.10.10.[1:3]
username: root
privateKeyPath: ~/.ssh/id_rsa
password: ""
port: 22
excludes:
- 192.168.235.132
script: "1.sh"
- 執(zhí)行shell
添加--debug
可以輸出詳細(xì)內(nèi)容痢毒。
$ easyctl exec shell -c config.yaml --debug
配置項(xiàng)說明
- 主機(jī)配置段:該段配置遠(yuǎn)程執(zhí)行
shell
的主機(jī)信息,字段說明如下-
host: 10.10.10.[1:3]
主機(jī)地址段蚕甥,適用于ip
連續(xù)場景哪替。分隔符可以為[1:3]
、1-2
菇怀、[1-2]
凭舶、1:2
-
username
: 遠(yuǎn)程主機(jī)ssh
用戶名稱,缺省值為root
-
password
: 對應(yīng)username
的密碼 -
privateKeyPath
:ssh
私鑰路徑 -
port
:ssh
端口爱沟,默認(rèn)22
-
excludes
: 排除host
地址段內(nèi)的ip
地址列表
-
privateKeyPath
優(yōu)先級高于password
:
-
privateKeyPath
為空帅霜,取password
值,ssh
使用密碼登錄方式 -
privateKeyPath
非空呼伸,取privateKeyPath
值身冀,ssh
使用密鑰登錄方式
server:
# - host: 10.79.165.[11:13]
# username: root
# password: neusoft
# port: 22
# - host: 10.79.164.[61:63]
# username: root
# password: neusoft
# port: 22
- host: 10.10.10.[1:3]
username: root
privateKeyPath: ~/.ssh/id_rsa
password: ""
port: 22
excludes:
- 192.168.235.132
- 腳本配置:
-
script: "date"
: 遠(yuǎn)程執(zhí)行的shell
指令,適用于運(yùn)行單個shell
指令場景 -
script: "1.sh"
: 遠(yuǎn)程執(zhí)行的shell
腳本括享,適用于運(yùn)行多個shell
指令場景
-
配置樣例
1.主機(jī):
10.10.10.1-10.10.10.10
執(zhí)行date
指令搂根,使用密鑰登錄方式
server:
- host: 10.10.10.[1:10]
username: root
privateKeyPath: ~/.ssh/id_rsa
password: ""
port: 22
excludes:
- 192.168.235.132
script: "date"
2.主機(jī):
10.10.10.1-10.10.10.10
執(zhí)行date
指令,使用密碼登錄方式
server:
- host: 10.10.10.[1:10]
username: root
# privateKeyPath: ~/.ssh/id_rsa
password: "123456"
port: 22
excludes:
- 192.168.235.132
script: "date"
3.主機(jī):
10.10.10.1
铃辖、10.10.10.3
剩愧、10.10.10.4
執(zhí)行date
指令,使用密碼登錄方式
server:
- host: 10.10.10.[1:4]
username: root
# privateKeyPath: ~/.ssh/id_rsa
password: "123456"
port: 22
excludes:
- 10.10.10.2
script: "date"
4.主機(jī):
10.10.10.1
娇斩、10.10.10.3
仁卷、10.10.10.4
執(zhí)行shell
腳本,使用密碼登錄方式
server:
- host: 10.10.10.[1:4]
username: root
# privateKeyPath: ~/.ssh/id_rsa
password: "123456"
port: 22
excludes:
- 10.10.10.2
script: "/root/modify-sysctl.sh"
/root/modify-sysctl.sh
腳本內(nèi)容如下
#!/bin/bash
sed -i '/vm.dirty_background_ratio/d' /etc/sysctl.conf
sed -i '/vm.dirty_ratio/d' /etc/sysctl.conf
echo "vm.dirty_ratio=10" >> /etc/sysctl.conf
echo "vm.dirty_background_ratio=5" >> /etc/sysctl.conf
sysctl -p
5.主機(jī):
10.10.10.1
成洗、10.10.10.3
五督、10.10.10.4
執(zhí)行shell
腳本藏否,使用密碼登錄方式瓶殃,且ssh
端口及密碼均不一致
server:
- host: 10.10.10.1
username: root
# privateKeyPath: ~/.ssh/id_rsa
password: "123456"
port: 22
- host: 10.10.10.3
username: root
# privateKeyPath: ~/.ssh/id_rsa
password: "123"
port: 22122
- host: 10.10.10.4
username: root
# privateKeyPath: ~/.ssh/id_rsa
password: "456"
port: 22222
excludes:
- 10.10.10.2
script: "/root/modify-sysctl.sh"
/root/modify-sysctl.sh
腳本內(nèi)容如下
#!/bin/bash
sed -i '/vm.dirty_background_ratio/d' /etc/sysctl.conf
sed -i '/vm.dirty_ratio/d' /etc/sysctl.conf
echo "vm.dirty_ratio=10" >> /etc/sysctl.conf
echo "vm.dirty_background_ratio=5" >> /etc/sysctl.conf
sysctl -p