37. 部署ansible¶
1. 环境准备¶
# 管理机器
192.168.178.120 # 部署ansible
# 被管理机器
192.168.178.121
192.168.178.122
# 要求:处于同一网段
管理机器¶
1. 安装ansible¶
#1.安装所需环境
yum install epel-release libselinux-python -y
#2.安装ansible
yum install ansible -y
2. 检查安装情况¶
rpm -ql ansible
#1. 查看配置文件
rpm -ql ansible |grep "^/etc"
#2. 查看命令
rpm -ql ansible |grep "^/usr/bin"
3. 检查ansible版本¶
ansible --version
4. 备份现有的配置文件¶
cp /etc/ansible/hosts /etc/ansible/hosts.bak
5. 修改配置文件¶
vim /etc/ansible/hosts
#添加要管理服务器的IP地址,在文件末尾添加如下几行
[chupenggroup]
192.168.178.121
192.168.178.122
被管理机器¶
1. 安装ansible的依赖环境¶
yum install epel-release libselinux-python -y
测试¶
1. 首先远程登录被管理机器测试¶
# 服务端操作
ssh root@192.168.178.121 # 点击确定,然后输入密码
ssh root@192.168.178.122 # 点击确定,然后输入密码
2. 输入命令测试¶
ansible chupenggroup -m command -a 'hostname' -k -u root
# 输入密码123456,查看返回的数据
# 疑问:如果每个被管理机器密码不一样的话,这里会出现什么情况?需要每个被管理机器都输入一次密码吗?
3. 配置ansible免密登录¶
1. 方法一:修改ansible配置文件¶
# 方法1:修改配置文件
vim /etc/ansible/hosts
[chupenggroup]
192.168.178.121 ansible_user=root ansible_ssh_pass=123456
192.168.178.122 ansible_user=root ansible_ssh_pass=123456
2. 方法二:配置公私钥¶
# 管理机器上
#1. 生成公私钥对
ssh-keygen -f ~/.ssh/id_rsa -P '' >/dev/null 2>&1
#2. 查看
ls ~/.ssh
#3. 编写公钥分发脚本
mkdir /myscripts
vim /myscripts/ssh_key.sh
#!/bin/bash
rm -rf ~/.ssh/id_rsa*
ssh-keygen -f ~/.ssh/id_rsa -P "" >/dev/null 2>&1
SSH_Pass=123456
Key_Path=~/.ssh/id_rsa.pub
for ip in 121 123
do
sshpass -p$SSH_Pass ssh-copy-id id $Key_Path "-o StrictHostKeyChecking=no" 192.168.178.$ip
done
#4. 授予权限
chmod +x /myscripts/ssh_key.sh
#5. 执行脚本
/usr/bin/bash /myscripts/ssh_key.sh
4. 测试免密登录¶
ansible chupenggroup -m command -a "ifconfig ens33"
最后更新:
2022-02-18 11:26:47