ansible服务器环境部署
DNS 域名解析
vim /etc/hosts
192.168.220.10 ansible
192.168.220.12 host1
192.168.220.13 host2
192.168.220.14 host3
192.168.220.15 host4
安装ansible服务器
yum install -y epel-release //安装epel扩展源
yum install -y ansible //安装ansible
ansible --version //可以选择查看ansible版本
检测部署是否完成
ansible --version 查看ansible版本
rpm -ql ansible 列出所有文件
rpm -qc ansible 查看配置文件
ansible --help 查看ansible帮助
ansible-doc #帮助命令
ansible-doc -l 看所有模块
ansible-doc 模块名 #查看模块的详细信息
ansible-doc 模块名 -s #查看模块的选项使用说明
例如:ansible-doc -s yum 看yum模块,了解其功能 install ( present’ or installed’, latest’), or remove ( absent’ or `removed’) yum list Package name enablerepo
ansible常用参数
-m //指定使用的模块名称,不指定默认使用command模块
-a //使用的模块参数,模块的具体动作;指定要执行的具体命令
--version //ansible版本信息
-i //主机清单文件路径,默认是在/etc/ansible/hosts
-k //提示输入ssh密码,而不使用基于ssh的密钥认证
-v //显示详细信息
-C //模拟执行测试,但不会真的执行
-T //执行命令的超时
-f //一次返回几个结果
--syntax-check //验证语法
ssh-key(可选)
为了让ansible能够免密控制其它服务器,将生成秘钥并拷贝到其它服务器(要进行控制的服务器上)
ssh-keygen //生成密钥
ssh-copy-id host1 //拷贝秘钥到其它服务器。注意,这里只拷贝到了host1,其它机器没有拷贝
ssh root@host1 //连接成功后 注意退出远程连接
exit
定义主机清单
vim /etc/ansible/hosts //添加以下内容。没有添加在主机清单的服务器,ansible会ping不通
host1
host2
host3
host4
// 在ansible 服务器上
ansible localhost -m ping //测试对本地的连通性
ansible host1 -m ping //测试对host1的连通性
简介输出 -o
ansible host2 -m ping //结果会显示失败,因为host2上没有免密登录秘钥
ansible host2 -m ping -u root -k -o //表明身份后即可成功
vim /etc/ssh/ssh_config
StrictHostKeyChecking no //找到StrictHostKeyChecking此行内容,注释解除,再将ask改为no
systemctl restart sshd //重启生效
请注意ping和ssh
ping
ICMP:网际消息管理协议
关闭host1主机的sshd进程,进行ping连通性测试。
再使用ansible对host1进行联通测试时,却是失败的。
结论ansible的ping,是探测ssh程序是否连接。不是icmp协议
//在host1服务器
systemctl stop sshd
//在ansible服务器
ping host1 //可以ping通host1服务器
ansible host1 -m ping //结果显示不成功
ansible主机清单
Ansible主机清单:主机资产清单,用于定义被管理主机的认证信息, 例如ssh登录用户名、密码以及key相关信息。
添加主机组
vim /etc/ansible/hosts
[web] //添加主机组
host1
host2
host3
host4
ansible web -m ping -o //-o输出以行的形式展示
如果是以下报错,请检查你的主机清单是否有错误
增加用户名和密码
[web]
host1 ansible_ssh_user='root' ansible_ssh_pass='root' //ansible_ssh_user ansible连接的用户名
host2 ansible_ssh_user='root' ansible_ssh_pass='root' //ansible_ssh_pass ansible连接的密码
host3 ansible_ssh_user='root' ansible_ssh_pass='root'
host4 ansible_ssh_user='root' ansible_ssh_pass='root'
ansible web -m ping //免用户名免密码
//另一种写法
[web]
host[1:4] ansible_ssh_user='root' ansible_ssh_pass='root'
修改端口号
例如:
修改host1服务器的端口号
vim /etc/ssh/sshd_config
Port 2222 //内容解除注释,并将22修改为2222 端口号1024~65535之间都可以
systemctl restart sshd
vim /etc/ansible/hosts
[web]
host1 ansible_ssh_user='root' ansible_ssh_pass='root' ansible_ssh_port='2222'
host[2:4] ansible_ssh_user='root' ansible_ssh_pass='root'
ansible web -m ping -o
组:变量
[web] //组名
host[1:4]
[web:vars] //组的变量
ansible_ssh_user='root' ansible_ssh_pass='root'
子分组
主机组:
1.嵌套 [组名:children]
2.对组定义变量 [组名:vars]
vim /etc/ansible/hosts
[apache] //子分组名
host[1:2]
[nginx] //子分组名
host[3:4]
[web:children] //apache、nginx为web的孩子
apache
nginx
[web:vars] //web的变量
ansible_ssh_user='root'
ansible_ssh_pass='root'
自定义主机列表
vim hostlist //新建主机列表清单
[dockers]
host1
host2
[dockers:vars]
ansible_ssh_user='root'
ansible_ssh_pass='root'
ansible -i hostlist dockers -m ping -o // -i链接主机清单,hostlist文件名,dockers组名。