因为服务器比较多,一个个添加比较麻烦,故写个shell脚本批量添加
测试代码
#!/bin/bash
set -x
wan=(wan1 wan2 ...)
lan=(lan1 lan2 ...)
domains=yx
n=0
for i in ${wan[@]}
do
# 跳过已添加的
isEnvSet=$(ansible $i -m shell -a 'cat ~/.bashrc | grep domain')
if [[ $isEnvSet ]]
then
continue
fi
ansible $i -m shell -a 'echo \# sid >> ~/.bashrc'
ansible $i -m shell -a 'echo "export domain='${domains[n]}'" >> ~/.bashrc'
ansible $i -m shell -a 'echo "export rsspWan='${wan[n]}'" >> ~/.bashrc'
ansible $i -m shell -a 'echo "export rsspLan='${lan[n]}'" >> ~/.bashrc'
if [[ $n -ge 15 ]]
then
ansible $i -m shell -a 'echo export isMaster=yes >> ~/.bashrc'
fi
ansible $i -m shell -a 'source ~/.bashrc'
# 输出
ansible $i -m shell -a 'tail -4 ~/.bashrc'
# 测试
ansible $i -m shell -a 'echo $domain'
ansible $i -m shell -a 'echo $rsspWan'
ansible $i -m shell -a 'echo $rsspLan'
ansible $i -m shell -a 'echo $isMaster'
let n++
done
这里需要注意的是使用ansible命令时,后面接的单引号和双引号取值的问题
查看输出信息
[root@iZ8vbhc2txn6mmfy2rl64iZ collect_load]# ./env_set.sh
+ wan=(wan)
+ lan=(lan)
+ domains=yx
+ for i in '${wan[@]}'
+ n=0
+ ansible wan -m shell -a 'echo \# sid >> ~/.bashrc'
wan | CHANGED | rc=0 >>
+ ansible wan -m shell -a 'echo "export domain=yx" >> ~/.bashrc'
wan | CHANGED | rc=0 >>
+ ansible wan -m shell -a 'echo "export rsspWan=wan" >> ~/.bashrc'
wan | CHANGED | rc=0 >>
+ ansible wan -m shell -a 'echo "export rsspLan=lan" >> ~/.bashrc'
wan | CHANGED | rc=0 >>
+ [[ n -ge 15 ]]
+ ansible wan -m shell -a 'source ~/.bashrc'
wan | CHANGED | rc=0 >>
+ ansible wan -m shell -a 'tail -4 ~/.bashrc'
wan | CHANGED | rc=0 >>
# sid
export domain=yx
export rsspWan=wan
export rsspLan=lan
+ ansible wan -m shell -a 'echo $domain'
wan | CHANGED | rc=0 >>
yx
+ ansible wan -m shell -a 'echo $rsspWan'
wan | CHANGED | rc=0 >>
wan
+ ansible wan -m shell -a 'echo $rsspLan'
wan | CHANGED | rc=0 >>
lan
+ ansible wan -m shell -a 'echo $isMaster'
wan | CHANGED | rc=0 >>
+ let n++