目录
第1章 介绍:
第2章 IP列表
第3章 开始配置
3.1 ssh
3.2 开始安装ansible
3.3 利用ansible远程执行各类脚本
python语言是运维人员必会的语言!
ansible是一个基于Python开发的自动化运维工具
ansible的功能实现基于SSH远程连接服务
ansible可以实现批量系统配置,批量软件部署,批量文件拷贝,批量运行命令等功能
机器说明 | ip | 主机名 |
主虚拟机 | 192.168.198.137 | yzh |
从虚拟机 | 192.168.198.128 | yzh1 |
实现从主机yzh到从机的密钥认证关系:
主机与从机之间进行ssh连接
3.2.1 主机yzh安装ansible
需要epel.repo源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum -y install ansible
3.2.2 从机需安装
yum -y install libselinux-python
3.2.3 ansible基础配置
ansible的配置文件:
[root@yzh ~]# tree /etc/ansible/
/etc/ansible/
|-- ansible.cfg
|-- hosts
|-- hosts.bak
`-- roles
1 directory, 3 files
编辑ansible的主机配置文件hosts,添加主机组yzh(修改前可对文件进行备份)
[root@yzh ~]# cp /etc/ansible/hosts{,.bak}
cp:是否覆盖"/etc/ansible/hosts.bak"? n
[root@yzh ~]# tail -4 /etc/ansible/hosts
[yzh]
192.168.198.137 ansible_ssh_user=root ansible_ssh_pass=123456
192.168.198.128 ansible_ssh_user=root ansible_ssh_pass=123456
命令说明:
ansible_ssh_user:ssh连接的用户名
ansible_ssh_pass:ssh连接的密码
3.2.4 利用ansible远程批量执行命令
语法:
ansible chensiqi -m command -a 'uptime'
ansible 主机组 -m ansible内置功能模块名 -a 命令
示例:获取192.168.198.128的主机的w信息(也可把ip换成yzh,可查看整个组的信息)
[root@yzh ~]# ansible 192.168.198.128 -m command -a "w"
192.168.198.128 | SUCCESS | rc=0 >>
10:40:36 up 2:27, 5 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 :0 08:20 2:27m 2.06s 2.06s /usr/bin/Xorg :
root pts/0 :0.0 08:22 2:17m 0.00s 0.00s bash
root pts/1 192.168.198.1 08:23 2:00m 0.03s 0.03s -bash
root pts/2 192.168.198.1 09:24 35:26 0.11s 0.11s -bash
root pts/3 yzh 10:40 1.00s 0.19s 0.00s /bin/sh -c /usr
示例:调用ansible内置的copy模块
[root@yzh ~]# ansible 192.168.198.128 -m copy -a "src=/etc/hosts dest=/tmp"
192.168.198.128 | SUCCESS => {
"changed": true,
"checksum": "691f8b5215f65d3e87664c6b3882b57061a0f99c",
"dest": "/tmp/hosts",
"gid": 0,
"group": "root",
"md5sum": "664a99e015c25cb10144fbb916393125",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 194,
"src": "/root/.ansible/tmp/ansible-tmp-1558081064.43-122053739556327/source",
"state": "file",
"uid": 0
}
[root@yzh ~]# ansible 192.168.198.128 -m command -a "ls /tmp"
192.168.198.128 | SUCCESS | rc=0 >>
ansible_cCYhKy
gconfd-gdm
gconfd-root
hosts #拷贝成功
keyring-Ap9rWy
keyring-bNDu5g
keyring-HpvTEh
keyring-IwWnab
keyring-lReuKL
keyring-M4qxxu
keyring-pBA4vc
keyring-pJ7iua
keyring-rp2TH1
keyring-SH1pPt
keyring-Y7dLDb
orbit-gdm
orbit-root
pulse-CwxpEBAJpPWx
pulse-o8WBp8v9b91p
pulse-oLKO1dO9JElO
[root@yzh ~]#
[root@yzh ~]# ssh [email protected] "ls /tmp"
[email protected]'s password:
gconfd-gdm
gconfd-root
hosts #拷贝成功
keyring-Ap9rWy
keyring-bNDu5g
keyring-HpvTEh
keyring-IwWnab
keyring-lReuKL
keyring-M4qxxu
keyring-pBA4vc
keyring-pJ7iua
keyring-rp2TH1
keyring-SH1pPt
keyring-Y7dLDb
orbit-gdm
orbit-root
pulse-CwxpEBAJpPWx
pulse-o8WBp8v9b91p
pulse-oLKO1dO9JElO
命令说明:
-m:调用ansible内置模块 copy 拷贝模块
-a:接命令。由于调用了copy模块,命令格式发生改变。src=本地文件路径 dest=目的地所在路径
示例:调用copy模块实现保存文件的属性改变
[root@yzh ~]# ansible 192.168.198.128 -m copy -a "src=/etc/hosts dest=/tmp owner=yzh group=yzh mode=600"
192.168.198.128 | SUCCESS => {
"changed": true,
"checksum": "691f8b5215f65d3e87664c6b3882b57061a0f99c",
"dest": "/tmp/hosts",
"gid": 503,
"group": "yzh",
"mode": "0600",
"owner": "yzh",
"path": "/tmp/hosts",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 194,
"state": "file",
"uid": 500
}
[root@yzh ~]# ssh [email protected] "ll /tmp/hosts"
[email protected]'s password:
bash: ll: command not found #并不能使用别名
[root@yzh ~]# ssh [email protected] "ls -l /tmp/hosts"
[email protected]'s password:
-rw-------. 1 yzh yzh 194 5月 17 09:44 /tmp/hosts
3.3.1 先将脚本分发到从机上去
[root@yzh ~]# echo "echo '测试成功!'" >> /root/1/test.sh
[root@yzh ~]# ansible yzh -m copy -a "src=/root/1/test.sh dest=/root/1/ mode=0755 backup=yes"
192.168.198.128 | SUCCESS => {
"changed": true,
"checksum": "05dfb0e37f8f2270c41947f9d44ec9520b45e043",
"dest": "/root/1/test.sh",
"gid": 0,
"group": "root",
"md5sum": "8af588216a93167632151f412f1d4b3d",
"mode": "0755",
"owner": "root",
"secontext": "system_u:object_r:admin_home_t:s0",
"size": 23,
"src": "/root/.ansible/tmp/ansible-tmp-1558082749.28-252420826366723/source",
"state": "file",
"uid": 0
}
192.168.198.137 | SUCCESS => {
"changed": true,
"checksum": "05dfb0e37f8f2270c41947f9d44ec9520b45e043",
"dest": "/root/1/test.sh",
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/root/1/test.sh",
"size": 23,
"state": "file",
"uid": 0
}
注意:dest路径的写法,若是不存在的目录,结尾要加斜线(/root/1/),否则默认不会创建目标目录
3.3.2 远程批量执行脚本
ansible yzh -m shell -a "/root/1/test.sh"
[root@yzh ~]# ansible yzh -m command -a "sh /root/1/test.sh"
192.168.198.128 | SUCCESS | rc=0 >>
测试成功!
192.168.198.137 | SUCCESS | rc=0 >>
测试成功!
以上即为一些基础的ansible的操作。