1.所有服务器全部采用静态ip
2.主机名及主机名互相绑定
[root@manage01 ~]# cat /etc/hosts
127.0.0.1 localhost
192.168.98.203 node3
3.关闭防火墙, selinux
# systemctl disable firewalld
# sed -i -r '/SELINUX=/c\SELINUX=disabled' /etc/selinux/config
4.采用时间服务器,时间同步
(1)修改配置文件,配置时间服务器为阿里云的时间服务器
[root@manage01 ~]# egrep "^server" /etc/chrony.conf
server ntp1.aliyun.com
server ntp2.aliyun.com
server ntp3.aliyun.com
server ntp4.aliyun.com
(2)重启服务chronyd
[root@manage01 ~]# systemctl restart chronyd.service
(3)查看源信息 #chronyc
chrony的命令行客户端 [root@manage01 ~]# chronyc sources -v
5.确认和配置yum源(需要epel源)
[root@manage01 ~]# yum -y install epel-*6.ssh远程连接
6.#管理端manage01生成ssh公私钥
[root@manage01 ~]# ssh-keygen
#将公钥传给node1
[root@manage01 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.98.201
#将公钥传给node2 [root@manage01 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.98.202
小窍门 免交互创建公私钥 [root@manage01 ansible]# ssh-keygen -f /root/.ssh/id_rsa -N ""
-f 指定密钥存放路径
-N "" 新密码设置问空
-P "" 老密码是什么
如何可以非交互式传公钥呢
[root@manage01 ansible]# yum -y install sshpass
[root@manage01 ansible]# sshpass -p111111 ssh-copy-id -o StrictHostKeyChecking=no -i /root/.ssh/id_rsa.pub root@192.168.98.202
StrictHostKeyChecking 严厉的主机监测=no 就不会问你yes|no了
sshpass 非交互式传密码
测试证书是否生效 [root@manage01 ~]# for i in `seq 201 203`;do > ssh root@192.168.98.$i "hostname" > done node1 node2 看到返回客户端的计算机名称
安装方式:
1)yum
[root@manage01 ~]# yum -y install ansible
[root@manage01 ~]# ansible --version
2) 源码
1、官网下载地址:https://releases.ansible.com/ansible[root@manage01 ~]# wget https://releases.ansible.com/ansible/ansible-2.9.3.tar.gz2、安装ansible[root@manage01 ~]# tar xf ansible-2.9.3.tar.gz [root@manage01 ~]# mv ansible-2.9.3 /opt/ansible[root@manage01 ~]# cd /opt/ansible-2.9.3#python软件包安装--1、安装依赖[root@manage01 ansible-2.9.0rc3]# pip3 install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/[root@manage01 ansible]# ln -s /usr/bin/pip3 /usr/bin/pip#python软件包安装--2、安装软件[root@manage01 ansible-2.9.0rc3]# pip install --user ansible -i https://pypi.tuna.tsinghua.edu.cn/simple/#安装后设置[root@manage01 ~]# ln -s /usr/bin/python3 /usr/bin/python[root@manage01 ~]# ln -s /opt/ansible/bin/* /usr/bin/[root@manage01 ~]# cp /opt/ansible/examples/ansible.cfg /etc/ansible/[root@manage01 ~]# cp /opt/ansible/examples/hosts /etc/ansible/FAQ1
[root@manage01 ansible-2.9.0rc3]# ./bin/ansible -m ping 192.168.98.201 [WARNING]: No inventory was parsed, only implicit localhost is available [WARNING]: provided hosts list is empty, only localhost is available. Note thatthe implicit localhost does not match 'all' [WARNING]: Could not match supplied host pattern, ignoring: 192.168.98.201 触发原因 执行ansible的时候回去读取客户端文件hosts,如果没有把客户端加入到hosts文件,就说明无法ansible无法管理。 解决方案 [root@manage01 ansible]# mkdir /etc/ansible [root@manage01 ansible]# cp examples/hosts /etc/ansible/ #将需要管理的客户端IP地址写入hosts文件,可以分组或者直接写 [root@manage01 ~]# cat /etc/ansible/hosts 192.168.98.[201:203] 分组 [group1]192.168.98.[201:203]管理端ansible目录:/etc/ansible
[root@manage01 ansible]# tree .
├── ansible.cfg #ansible配置文件,不需要配置
├── hosts #主机列表
└── roles #角色列表
[root@manage01 ansible]# egrep -v "(^#|^$)" /etc/ansible/hosts
[group1] #名字可以随便起 后面跟上业务机器的IP地址或者域名
192.168.98.201
192.168.98.202
192.168.98.203
关于业务机器分组
分组中使用范围
[nginx] 组名
apache[1:10].aaa.com 表示apache1.aaa.com到apache10.aaa.com这10台机器 nginx[a:z].aaa.com 表示nginxa.aaa.com到nginxz.aaa.com共26台机器
10.1.1.[11:15] 表示10.1.1.11到10.1.1.15这5台机器
如果业务机器的SSH端口不是22
10.1.1.13:2222 表示10.1.1.13这台,但ssh端口为2222
指定业务机器别名,未做免密登陆的机器可以通过下面的机器设置账号密码
nginx1 ansible_ssh_host=10.1.1.13 ansible_ssh_port=2222 ansible_ssh_user=root ansible_ssh_pass="123456"
ansible_ssh_host 指定业务机器的IP或域名
ansible_ssh_port 指定业务机器的ssh端口
ansible_ssh_user 指定业务机器的ssh用户名
ansible_ssh_pass 指定业务机器的ssh用户名密码
利用机器别名分组
nginx1 ansible_ssh_host=10.1.1.13 ansible_ssh_port=2222 ansible_ssh_user=root ansible_ssh_pass="123456"
nginx2 ansible_ssh_host=10.1.1.12
[nginx]
nginx1 #写服务器别名
nginx2
[root@manage01 ansible]# egrep -v "(^#|^$)" /etc/ansible/hosts
#别名定义
web1 ansible_ssh_host=192.168.98.203 ansible_ssh_port=12121
#分组
[group1]
192.168.98.201 #未做免密登陆机器
192.168.98.202:12121 ansible_ssh_user=sko ansible_ssh_pass='123'
#别名机器
web1 ansible_ssh_user=sko ansible_ssh_pass='123'
我们可以使用ansible通过调用ping模块来测试分组机器或某个机器
-m 指定使用的模块 group1 业务机器分组
#测试单个机器
#测试主机列表中的机器
#测试单个机器方法
[root@manage01 ~]# ansible -m ping 192.168.98.201
192.168.98.201 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
#测试主机列表中的机器方法
[root@manage01 ~]# ansible -m ping group1
192.168.98.201 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
}, "changed": false,
"ping": "pong"
}
| 留言与评论(共有 0 条评论) “” |