我尝试用Shell脚本自动化Ansible的初始设置

做过的事情 (zuò guò de shì

我想使用Ansible!但是在执行之前需要在服务器端设置密钥,或者在hosts文件中记录IP地址等等一系列繁琐的配置…
因此,我将Ansible的初始配置转化为Shell脚本进行自动化!
如果使用以下的Shell脚本,您将在短短几秒钟内轻松使用Ansible,请务必查看。

Shell脚本的内容/解释

我用Shell脚本创建了以下的内容。

amazon-linux-extras install -y ansible2
ssh-keygen -N ""
chmod 777 /root/.ssh/id_rsa.pub
ssh -i <秘密鍵> ユーザー名@$1 'sudo chmod 777 /root ; sudo chmod 777 /root/.ssh'
scp -i <秘密鍵> /root/.ssh/id_rsa.pub ユーザー@${1}:/root/.ssh/
ssh -i <秘密鍵> ユーザー名@$1 'sudo chmod 777 /root/.ssh/authorized_keys'
ssh -i <秘密鍵> ユーザー名@$1 ' cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys '
ssh -i <秘密鍵> ユーザー名@$1 'sudo chmod 550 /root ; sudo chmod 550 /root/.ssh ; sudo chmod 550 /root/.ssh/authorized_keys '
if [ `cat /etc/ansible/hosts|grep $1` = "$1" ]
then
echo OK
else
echo $1 >> /etc/ansible/hosts
fi
echo interpreter_python=/usr/bin/python >> /etc/ansible/ansible.cfg

作为流程的话,可以如下所述。

安装Ansible

打开权限,将id_rsa.pub文件中的内容复制到被执行服务器的authorized_keys文件中

关闭权限

在if语句中将被执行服务器的地址写入到/etc/ansible/hosts文件中
※如果已经写入了,则显示“OK”而不执行

在ansible.cfg文件中加入”interpreter_python=/usr/bin/python”

最后

只需使用上述脚本,您就可以轻松进行初始设置。
执行时,只需使用下面的命令即可。

sh (ファイル名).sh IPアドレス

通过这个,你可以轻松地进行初始设置!

广告
将在 10 秒后关闭
bannerAds