前言
学习ansible各种模块、语法、参数
安装前步骤、配置服务器认证和服务器变量
用ansible模块替代Shell脚本
一、ansible的介绍
ansible是一个同时管理多个远程主机的软件(任何通过SSH协议登录的主机)ansible可以管理远程虚拟机、物理机、也可以是本地主机(Linux、Windows)
ansible的认证方式有哪些?1.SSH提供密码认证 2.公私钥认证
二、ansible安装配置
1.配置SSH互信
ansible 管理机器主机IP:192.168.10.11
配置ssh互信,设置免密登录
[root@ansible ~]# ssh-keygen
[root@ansible ~]# ssh-copy-id -i 192.168.10.11
[root@ansible ~]# scp -r /root/.ssh 192.168.10.22:/root/.ssh/
[root@ansible ~]# scp -r /root/.ssh 192.168.10.24:/root/.ssh/
[root@ansible ~]# scp -r /root/.ssh 192.168.10.25:/root/.ssh/
[root@ansible ~]# scp -r /root/.ssh 192.168.10.7:/root/.ssh/
2.ansible的安装及配置
2.1 安装ansible
[root@ansible ~]# yum install epel-* #配置好依赖源
[root@ansible ~]# yum -y install ansible
2.2 配置aansible主机清单文件
(1)[root@ansible ~]# vim /etc/ansible/hosts
[webserver] #这是一个组,名字随意,操作webserver组等于操作这里面包含的所有机器
web1.com #可以写主机名也可以写ip--这里的web1.com对应的IP是192.168.10.22
web2.com #这里的web2.com对应的IP是192.168.10.23
192.168.10.24
[nfs]
192.168.10.25
[backup]
192.168.10.7
[root@ansible ~]# ansible all -m shell -a “hostname” #让所有的主机返回主机名
2.3 给backup组添加密码认证信息
[root@ansible ~]# vim /etc/ansible/hosts
#设置backup的端口和密码
[backup]
192.168.10.7 ansible_port=22 ansible_password='123456'
测试
[root@ansible ~]# ansible 192.168.10.7 -a "hostname"
192.168.10.7 | CHANGED | rc=0 >>
backup.com
查看backup组的内存信息
[root@ansible ~]# ansible 192.168.10.7 -a “free -h”
[root@ansible ~]# ansible 192.168.10.7 -a "free -h"
192.168.10.7 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 3.7G 316M 3.2G 12M 217M 3.2G
Swap: 2.0G 0B 2.0G
[root@ansible ~]# vim /etc/ansible/hosts
[backup]
192.168.10.7 ansible_port=22990 ansible_password='123456'
[root@backup ~]# passwd #123456与上述配置文件相同
更改用户 root 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@backup ~]# vim /etc/ssh/sshd_config
Port 22990
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
重启服务、查看端口是否为已经修改的22990
[root@backup ~]# systemctl restart sshd
[root@backup ~]# ss -anplt | grep sshd
LISTEN 0 128 :22990 : users:((“sshd”,pid=2794,fd=3))
LISTEN 0 128 [::]:22990 [::]: users:((“sshd”,pid=2794,fd=4))
验证
[root@ansible ~]# ansible 192.168.10.7 -a "free -h"
192.168.10.7 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 3.7G 321M 3.2G 12M 219M 3.1G
Swap: 2.0G 0B 2.0G
[root@ansible ~]# ansible 192.168.10.7 -a "hostname"
192.168.10.7 | CHANGED | rc=0 >>
backup.com
2.4 对一组机器进行添加信息
在ansible管理机器上执行上述操作
[webserver:vars] #添加[webserver]组的变量
ansible_port=22 #公共变量分行写
ansible_password='123'
[webserver]
web1.com #------>192.168.10.22
web2.com #------>192.168.10.23
192.168.10.24
验证测试
[root@ansible ~]# ansible webserver -m ping
192.168.10.24 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
web2.com | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
web1.com | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
ansible管理其他机器需要确定其目标机器的指纹
指纹保存在[root@ansible ~]# ls ~/.ssh/known_hosts当中
3.ansible模块–ping模块–command模块
3.1 用ping模块测试连通性
[root@ansible ~]# ansible backup -m ping
192.168.10.7 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
3.2 command简单命令模块
command模块是ansible的默认模块 ,指定 -m command 只支持简单命令执行(不支持管道符、重定向、变量等)
(1)远程查看主机名:ansible webserver -m command -a “hostname” 与 ansible webserver -a "hostname"用法相同
[root@ansible ~]# ansible webserver -m command -a "hostname"
192.168.10.24 | CHANGED | rc=0 >>
mem1.com
web2.com | CHANGED | rc=0 >>
web2.com
web1.com | CHANGED | rc=0 >>
web1.com
[root@ansible ~]# ansible webserver -a "hostname"
web2.com | CHANGED | rc=0 >>
web2.com
web1.com | CHANGED | rc=0 >>
web1.com
192.168.10.24 | CHANGED | rc=0 >>
mem1.com
(2)远程查看主机内存
[root@ansible ~]# ansible backup -a “free -m”
[root@ansible ~]# ansible backup -a "free -m" #command省略
192.168.10.7 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 3770 340 3176 11 253 3190
Swap: 2047 0 2047
(3)远程创建文件查看文件 #会出现warnning告警信息参考(5)关闭告警信息
[root@ansible ~]# ansible backup -m command -a "touch /opt/a1.txt"
192.168.10.7 | CHANGED | rc=0 >> #创建文件
[root@ansible ~]# ansible backup -m command -a "cat /opt/a1.txt"
192.168.10.7 | CHANGED | rc=0 >> #查看文件
(4)查看CPU负载
[root@ansible ~]# ansible webserver -a "uptime"
web2.com | CHANGED | rc=0 >>
12:45:16 up 1:53, 2 users, load average: 0.00, 0.02, 0.05
192.168.10.24 | CHANGED | rc=0 >>
12:45:16 up 1:53, 2 users, load average: 0.00, 0.01, 0.05
web1.com | CHANGED | rc=0 >>
12:45:16 up 1:53, 2 users, load average: 0.00, 0.01, 0.05
(5)关闭告警信息
[root@ansible ~]# ansible backup -m command -a "touch /opt/a1.txt warn=false"
192.168.10.7 | CHANGED | rc=0 >>
(6)创建用户
[root@ansible ~]# ansible webserver -m command -a "useradd nihaoa"
web1.com | CHANGED | rc=0 >>
web2.com | CHANGED | rc=0 >>
192.168.10.24 | CHANGED | rc=0 >>
4command模块练习
1.备份/var/log日志目录,需要先进入根目录
[root@ansible ~]# ansible backup -m command -a "tar -zcvf /opt/log.tgz /var/log chdir=/ warn=false"
[root@ansible ~]# ansible backup -a "ls /opt/" #测试查看log.tgz
192.168.10.7 | CHANGED | rc=0 >>
a1.txt
log.tgz
rh
ansible远程操作backup组利用command模块备份/var/log日志,需要先切换到根目录下。
2.在opt目录下创建a2.txt文件
[root@ansible ~]# ansible backup -a "touch a2.txt chdir=/opt warn=false"
192.168.10.7 | CHANGED | rc=0 >>
[root@ansible ~]# ansible backup -a "ls /opt/"
192.168.10.7 | CHANGED | rc=0 >>
a1.txt
a2.txt
log.tgz
rh
3.目标目录不存在用removes
备份etc到/backup_config中
[root@ansible ~]# ansible backup -a "tar -zcvf /backup_config/etc.tgz etc chdir=/ removes=/backup_config warn=false"
[root@ansible ~]# ansible backup -a "ls /backup_config warn=false"
192.168.10.7 | CHANGED | rc=0 >>
etc.tgz
没有创建目录会出现以下情况:
[root@ansible ~]# ansible backup -a "tar -zcvf /backup_config/etc.tgz etc chdir=/ removes=/backup_config"
192.168.10.7 | SUCCESS | rc=0 >>
skipped, since /backup_config does not exist #此命令不会对目标机器进行任何修改,需要创建目录
虽然ansible提供大量的模块,但是shell模块比command模块更加实用