Ansible基础
Ansible基础
一、Ansible简介
批量管理服务器的工具
无需部署agent,通过ssh进行管理
流行的自动化运维工具:https://github.com/ansible/ansible
二、jenkins简介
可视化运维(主要用在可视化部署)
持续构建,可以和git,svn结合
可结合ssh实现可视化运维
可结合ansible实现可视化运维
三、环境说明
Centos7.3(yum -y install net-tools vim)
关闭防火墙(systemctl stop firewalld,systemctl disable firewalld)
关闭selinux
四、Python3与Ansible的安装
使用源码安装Python3.5
安装支持包
yum -y install lrzsz vim net-tools gcc gcc-c++ ncurses ncurses-devel unzip zlib-devel zlib openssl-devel openssl libffi-devel epel-release libselinux-python
源码编译Python3.5
tar xf Python-3.5.2.tgz -C /usr/src/
cd /usr/src/Python-3.5.2/
./configure --prefix=/usr/local/python/
make && make install
ln -s /usr/local/python/bin/python3 /usr/bin/python3
which python3
python3 -V
使用pip3安装Ansible
安装Ansible最新版本
/usr/local/python/bin/pip3 install ansible
安装完毕后
ln -s /usr/local/python/bin/ansible /usr/local/bin/
which ansible
ansible --version
Ansible查看帮助
/usr/local/python/bin/ansible-doc -l 查看总帮助
/usr/local/python/bin/ansible-doc -s shell 查看shell模块的帮助
/usr/local/python/bin/ansible-doc -s raw
五、使用公私钥实现ssh无密码登录
ansible是无agent的,无agent是怎么批量管理服务器的?主要是借用ssh来批量管理服务器。
ssh默认登陆是需要密码的,所以管理起来比较麻烦,这节课主要是介绍ssh的无密码登陆。
ssh无密码登陆实现以后,使用ansible批量管理服务器就变得简单了。
生成秘钥对
ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ""
分发秘钥
sshpass -p "123123" ssh-copy-id -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 192.168.200.55
进行免密码登录测试==>ssh 192.168.200.55
使用hostname -I查看当前IP地址看是否登录成功
六、Ansible的简单配置和ping模块
1、ansible的配置文件
通过pip安装的ansible是没有配置文件的。我们需要创建一个
- [root@ansible python]# mkdir -p /etc/ansible
- [root@ansible python]# vim /etc/ansible/hosts
- [root@ansible python]# cat /etc/ansible/hosts #ansible主机管理配置文件
- [nginx] #被管理的主机组名称
- webA ansible_ssh_host=192.168.200.132 ansible_ssh_port=22 ansible_ssh_user=root #第一台主机
- webB ansible_ssh_host=192.168.200.138 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=666666 #第二台主机
- 特别提示:
- WebA ===> 主机名
- ansible_ssh_host ===>主机IP
- ansible_ssh_port ===>ssh的默认端口
- ansible_ssh_user ===>ssh的用户名
- ansible_ssh_pass ===>ssh的用户的连接密码
如果我们已经设置了ssh免密钥了。那么就不需要写密码了。例如:webA
我们要是没有设置免密钥,那么就需要安装sshpass工具,并在/etc/ansible/hosts文件里写上主机的连接密码。例如webB
下载epel源安装sshpass
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum -y install sshpass
which sshpass
2、进行Ansible远程执行命令测试
语法:
ansible all -m command -a 'uptime'
ansible 主机组 -m ansible内置功能模块名 -a 命令
进行命令测试:
- #进行ping模块的连接测试
- [root@ansible python]# ansible nginx -m ping
- webB | FAILED! => { #我们发现webB还是没链接成功,这是因为本机的known_hosts文件还没有记录对方主机的信息。
- "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host."
- }
- webA | SUCCESS => { #webA成功
- "changed": false,
- "ping": "pong"
- }
想要解决known_hosts的问题,只需要修改ssh的配置文件/etc/ssh/ssh_config即可
- #修改ssh配置文件
- [root@ansible .ssh]# sed -n '35p' /etc/ssh/ssh_config
- # StrictHostKeyChecking ask
- [root@ansible .ssh]# vim /etc/ssh/ssh_config
- [root@ansible .ssh]# sed -n '35p' /etc/ssh/ssh_config
- StrictHostKeyChecking no #去掉注释,修改成这样
- #重启ssh服务
- [root@ansible .ssh]# systemctl reload sshd.service
- #再次进行ansible连接测试
- [root@ansible .ssh]# ansible nginx -m ping
- webA | SUCCESS => {
- "changed": false,
- "ping": "pong"
- }
- webB | SUCCESS => {
- "changed": false,
- "ping": "pong"
- }
3、Ansible的简单使用方法
ansible -i /etc/ansible/hosts 主机或主机组 -m 指定模块 -a 命令
不用-i指定配置文件默认为/etc/ansible/hosts
4、使用ping模块用来查看服务器是否连接正常,ping模块不需要-a指定参数
ansible all -m ping
- #操作测试
- [root@ansible .ssh]# ansible webA -m ping
- webA | SUCCESS => {
- "changed": false,
- "ping": "pong"
- }
- [root@ansible .ssh]# ansible all -m ping
- webA | SUCCESS => {
- "changed": false,
- "ping": "pong"
- }
- webB | SUCCESS => {
- "changed": false,
- "ping": "pong"
- }
- [root@ansible .ssh]# ansible webA:webB -m ping
- webA | SUCCESS => {
- "changed": false,
- "ping": "pong"
- }
- webB | SUCCESS => {
- "changed": false,
- "ping": "pong"
- }
七、Ansible的三个命令模块
1、Ansible模块command(不支持管道,不建议使用)
- #command支持直接回显命令的执行结果
- [root@ansible ~]# ansible all -m command -a "pwd"
- webA | SUCCESS | rc=0 >>
- /root
- webB | SUCCESS | rc=0 >>
- /root
- #command模块不支持管道符操作
- [root@ansible ~]# ansible all -m command -a "echo test | grep t"
- webA | SUCCESS | rc=0 >>
- test | grep t
- webB | SUCCESS | rc=0 >>
- test | grep t
- #command模块不支持重定向操作
- [root@ansible ~]# ansible all -m command -a "echo bb >> /tmp/testansible"
- webA | SUCCESS | rc=0 >>
- bb >> /tmp/testansible
- webB | SUCCESS | rc=0 >>
- bb >> /tmp/testansible
2、Ansible模块shell(支持管道,支持重定向)
- #shell模块支持管道符
- [root@ansible ~]# ansible all -m shell -a "echo testansible | grep a"
- webA | SUCCESS | rc=0 >>
- testansible
- webB | SUCCESS | rc=0 >>
- testansible
- #shell支持重定向
- [root@ansible ~]# ansible all -m shell -a "echo bb >> /tmp/testansible"
- webA | SUCCESS | rc=0 >>
- webB | SUCCESS | rc=0 >>
- #如果遇到特殊符号需要加入\转义,这样子ansible才能正常运行
- [root@ansible ~]# ansible all -m shell -a "cat /etc/passwd | awk -F":" '{print \$1}'"
- webB | SUCCESS | rc=0 >>
- root
- bin
- daemon
- adm
- lp
- sync
- shutdown
- halt
- operator
- games
- ftp
- nobody
- systemd-bus-proxy
- systemd-network
- dbus
- polkitd
- tss
- postfix
- sshd
- chrony
- webA | SUCCESS | rc=0 >>
- root
- bin
- daemon
- adm
- lp
- sync
- shutdown
- halt
- operator
- games
- ftp
- nobody
- systemd-bus-proxy
- systemd-network
- dbus
- polkitd
- tss
- postfix
- sshd
- chrony
3、ansible模块raw,最原始的方式运行命令(不依赖python,仅通过ssh实现)
- #清除yum缓存
- [root@ansible ~]# ansible all -m raw -a "yum -y clean all"
- webB | SUCCESS | rc=0 >>
- Loaded plugins: fastestmirror
- Cleaning repos: c7-media
- Cleaning up everything
- Shared connection to 192.168.200.138 closed.
- webA | SUCCESS | rc=0 >>
- Loaded plugins: fastestmirror
- Cleaning repos: c7-media epel
- Cleaning up everything
- Cleaning up list of fastest mirrors
- Shared connection to 192.168.200.132 closed.
- #建立yum缓存
- [root@ansible ~]# ansible all -m raw -a "yum makecache"
- webA | SUCCESS | rc=0 >>
- Loaded plugins: fastestmirror
- c7-media | 3.6 kB 00:00
- Loading mirror speeds from cached hostfile
- * c7-media:
- Metadata Cache Created
- Shared connection to 192.168.200.132 closed.
- webB | SUCCESS | rc=0 >>
- Loaded plugins: fastestmirror
- c7-media | 3.6 kB 00:00
- Loading mirror speeds from cached hostfile
- * c7-media:
- Metadata Cache Created
- Shared connection to 192.168.200.138 closed.
- #yum装nmap包
- ansible all -m raw -a "yum -y install nmap"
八、Ansible的copy模块批量下发文件或文件夹
1、copy模块概述
- copy模块的参数,ansible 主机组 -m 模块 -a 命令
- src:指定源文件或目录
- dest:指定目标服务器的文件或目录
- backup:是否要备份
- owner:拷贝到目标服务器后,文件或目录的所属用户
- group:拷贝到目标服务器后,文件或目录的所属群组
- mode:文件或目录的权限
- 准备工作
- [root@ansible ~]# mkdir -p /service/scripts
- [root@ansible ~]# echo "aaa" > /service/scripts/test.txt
- [root@ansible ~]# echo "bbb" > /service/scripts/test2.txt
- 所有被管理端节点必须安装libselinux-python包
- yum -y install libselinux-python
2、copy模块拷贝文件
特别提示:如果目标路径不存在会自动创建
src===>源文件路径 dest=目标路径位置
- [root@ansible ~]# ansible all -m copy -a "src=/service/scripts/test.txt dest=/service/scripts/"
- webB | FAILED! => { #节点未安装libselinux-python
- "changed": false,
- "checksum": "972a1a11f19934401291cc99117ec614933374ce",
- "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
- }
- webA | SUCCESS => {
- "changed": true,
- "checksum": "972a1a11f19934401291cc99117ec614933374ce",
- "dest": "/service/scripts/test.txt",
- "gid": 0,
- "group": "root",
- "md5sum": "5c9597f3c8245907ea71a89d9d39d08e",
- "mode": "0644",
- "owner": "root",
- "secontext": "system_u:object_r:svc_svc_t:s0",
- "size": 4,
- "src": "/root/.ansible/tmp/ansible-tmp-1529035954.8010113-22928023490467/source",
- "state": "file",
- "uid": 0
- }
- #节点安装libselinux-python后在进行发送测试
- [root@ansible ~]# ansible webB -m copy -a "src=/service/scripts/test.txt dest=/service/scripts/"
- webB | SUCCESS => { #发送成功
- "changed": true,
- "checksum": "972a1a11f19934401291cc99117ec614933374ce",
- "dest": "/service/scripts/test.txt",
- "gid": 0,
- "group": "root",
- "md5sum": "5c9597f3c8245907ea71a89d9d39d08e",
- "mode": "0644",
- "owner": "root",
- "secontext": "system_u:object_r:svc_svc_t:s0",
- "size": 4,
- "src": "/root/.ansible/tmp/ansible-tmp-1529036146.1609693-94270890826089/source",
- "state": "file",
- "uid": 0
- }
3、copy模块拷贝文件夹
特别提示:
如果目标路径里有与我拷贝的文件同名文件的话,会直接覆盖目标路径下的文件
- #拷贝/service/scripts/ 目录下所有内容到dest的路径下(注意两条命令的对比)
- [root@ansible ~]# ansible webA -m copy -a "src=/service/scripts/ dest=/service/scripts/"
- webA | SUCCESS => {
- "changed": true,
- "dest": "/service/scripts/",
- "src": "/service/scripts/"
- }
- #拷贝/service/scripts目录本身及其内部的所有内容到dest的路径下(注意两条命令的对比)
- [root@ansible ~]# ansible webA -m copy -a "src=/service/scripts dest=/service/scripts/"
- webA | SUCCESS => {
- "changed": true,
- "dest": "/service/scripts/",
- "src": "/service/scripts"
- }
4、copy模块自动备份
特别提示:
参数:backup=yes ===>意思是,如果目标路径下,有与我同名但不同内容的文件时,在覆盖前,对目标文件先进行备份。
- [root@ansible ~]# ansible webB -m copy -a "src=/service/scripts/ dest=/service/scripts/ backup=yes"
- webB | SUCCESS => {
- "changed": true,
- "dest": "/service/scripts/",
- "src": "/service/scripts/"
- }
5、copy模块指定用户和属主
- [root@ansible ~]# ansible webA -m copy -a "src=/service/scripts/ dest=/service/scripts/ owner=nobody group=nobody mode=0600"
- webA | SUCCESS => {
- "changed": true,
- "dest": "/service/scripts/",
- "src": "/service/scripts/"
- }
九、Ansible的script模块批量运行脚本
ansible的script模块能够实现远程服务器批量运行本地的shell脚本。
- #操作示例-->远程批量分发并自动部署nginx
- #所有被管理端需要挂载光盘,并创建本地yum配置文件
- [root@ansible scripts]# pwd
- /service/scripts
- [root@ansible scripts]# ls | xargs -n1
- auto_nginx.sh #自动安装nginx脚本
- fenfa.sh #批量分发脚本
- nginx-1.10.2.tar.gz #nginx源码包
- [root@ansible scripts]# cat auto_nginx.sh #nginx安装脚本
- #!/bin/sh
- #nginx install shell scripts
- test -d /media/cdrom || mkdir -p /media/cdrom
- mount /dev/sr0 /media/cdrom &>/dev/null
- yum -y install gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel &>/dev/null
- test -d /service/scripts || exit 3
- cd /service/scripts/
- tar xf nginx-1.10.2.tar.gz -C /usr/src/
- cd /usr/src/nginx-1.10.2/
- ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module &>/dev/null
- make &>/dev/null
- make install &>/dev/null
- exit 0
- [root@ansible scripts]# cat fenfa.sh #源码包和安装脚本的批量分发脚本
- #!/bin/sh
- #批量分发脚本
- Group=$1
- ansible $Group -m copy -a "src=/service/scripts/ dest=/service/scripts/"
- ansible $Group -m script -a "/service/scripts/auto_nginx.sh"
- [root@ansible scripts]# sh fenfa.sh all #激活脚本
此脚本只是个演示示例,工作中需要写的尽量严谨一些。