第 1 章 Ansible 基本概述
1.1 ansible 是一个配置管理系统 configuration management system,
你只需要可以使用 ssh 访问你的服务器或设备就行.
1. 安装软件
2. 配置服务
1.2 ansible 能做什么
ansible 可以帮助我们完成一些批量任务, 或者完成一些需要经常重复的工作.
比如: 同时在 100 台服务器上安装 nfs 服务, 并在安装后启动服务.
比如: 将某个文件一次性拷贝到 100 台服务器上.
比如: 每当有新服务器加入工作环境时, 你都要为新服务器部署某个服务, 也就是说你需要经常重复的完成相同的工作.
这些场景中我们都可以使用到 ansible.
1.3 ansible 软件特点
1.ansible 不需要单独安装客户端, SSH 相当于 ansible 客户端.
2.ansible 不需要启动任何服务, 仅需安装对应工具即可.
3.ansible 依赖大量的 python 模块来实现批量管理.
4.ansible 配置文件 / etc/ansible/ansible.cfg
实现从管理机 m01 到其他机器的密钥认证
第 2 章 Ansible 安装配置
2.1 ansible 借助公钥批量管理
创建及利用非交换式工具实现批量分发公钥与批量管理服务器
- [root@m01 ~]# ssh-keygen -t rsa
- [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.31
- [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.41
- [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.7
2.2 安装 ansible
[root@m01 ~]# yum install ansible -y
2.3 配置 ansible
- [root@m01 ~]# vim /etc/ansible/hosts
- [zeq]
- 172.16.1.31
- 172.16.1.41
2.4 验证 ansible
2.4.1 ansible 是通过 ssh 端口探测通信
- [root@m01 ~]# ansible zeq -m ping
- 172.16.1.7 | SUCCESS => {
- "changed": false,
- "ping": "pong"
- }
- 172.16.1.31 | SUCCESS => {
- "changed": false,
- "ping": "pong"
- }
- 172.16.1.41 | SUCCESS => {
- "changed": false,
- "ping": "pong"
- }
2.4.2 批量执行命令
[root@m01 ~]# ansible zeq -m command -a "df -h"
2.4.3 如果没有给对应的主机下发公钥, 可以使用密码的方式进行添加
172.16.1.41 ansible_ssh_user='root' ansible_ssh_pass='1' ansible_ssh_port='22'
2.5 定义主机清单
- [root@m01 ~]# vim /etc/ansible/hosts
- [web]
- 172.16.1.7
- [nfs]
- 172.16.1.31
- [backup]
- 172.16.1.41
- [zeq:children]
- web
- nfs
- backup
2.5.1 测试
- [root@m01 ~]# ansible web --list-hosts #web
- hosts (1):
- 172.16.1.7
- [root@m01 ~]# ansible nfs --list-hosts #nfs
- hosts (1):
- 172.16.1.31
- [root@m01 ~]# ansible backup --list-hosts #rsync
- hosts (1):
- 172.16.1.41
- [root@m01 ~]# ansible zeq --list-hosts #集中所有的小组, 用于执行一些基础的配置
- hosts (3):
- 172.16.1.31
- 172.16.1.41
- 172.16.1.7
第 3 章 ansible 命令语法格式及模块
1.command 执行命令
2.shell 执行命令
3.yum 安装软件模块
4.copy 配置模块
5.service 启动服务模块
6.user 用户管理
7.file 创建目录, 创建文件, 往文件写内容
8.cron 定时任务
9.mount 挂载
3.1 command 命令模块
默认模块, 执行命令
[root@m01 ~]# ansible zeq -a "hostname"
3.2 如果需要一些管道操作, 则使用 shell
- [root@m01 ~]# ansible zeq -m shell -a "ifconfig|grep eth0" -f 50
- -f =forks /etc/ansible/ansible.cfg #结果返回的数量
3.3 yum 安装模块
参数 | 说明 |
name | 指定要安装的软件包名称 |
state | 指定使用 yum 的方法 |
installed,present | 安装软件包 |
removed,absent | 移除软件包 |
latest | 安装最新软件包 |
3.3.1 推送脚本文件至远程, 远程执行脚本文件
[root@m01 ~]# ansible zeq -m yum -a "name=httpd state=installed"
3.4 copy 模块
参数 | 说明 |
src | 推送数据的源文件信息 |
dest | 推送数据的目标路径 |
backup | 对推送传输过去的文件, 进行备份 |
content | 直接批量在被管理端文件中添加内容 |
group | 将本地文件推送到远端, 指定文件属组信息 |
owner | 将本地文件推送到远端, 指定文件属主信息 |
mode | 将本地文件推送到远端, 指定文件权限信息 |
3.4.1 推送文件模块
[root@m01 ~]# ansible zeq -m copy -a "src=/etc/hosts dest=/tmp/test.txt owner=www group=www mode=0600"
3.4.2 在推送覆盖远程端文件前, 对远端已有文件进行备份, 按照时间信息备份
[root@m01 ~]# ansible zeq -m copy -a "src=/etc/hosts dest=/tmp/test.txt backup=yes"
3.4.3 直接向远端文件内写入数据信息, 并且会覆盖远端文件内原有数据信息
[root@m01 ~]# ansible zeq -m copy -a "content='bgx'dest=/tmp/zeq"
3.5 service 服务模块
参数 | 说明 |
name | 定义要启动服务的名称 |
state | 指定服务状态是停止或是运行 |
started | 启动 |
stopped | 停止 |
restarted | 重启 |
reloaded | 重载 |
enabled | 是否让服务开启自启动 |
[root@m01 ~]# ansible zeq -m service -a "name=crond state=stopped enabled=yes"
3.6 user 模块
- [root@m01 ~]# echo "bgx"| openssl passwd -1 -stdin
- $1$1KmeCnsK$HGnBE86F/XkXufL.n6sEb.
- [root@m01 ~]# ansible zeq -m user -a 'name=xlw password="$1$1KmeCnsK$HGnBE86F/XkXufL.n6sEb."'
3.7 file 配置模块
参数 | 说明 |
path | 指定远程主机目录或文件信息 |
recurse | 递归授权 |
directory | 在远端创建目录 |
touch | 在远端创建文件 |
link | link 或 hard 表示创建链接文件 |
absent | 表示删除文件或目录 |
mode | 设置文件或目录权限 |
owner | 设置文件或目录属主信息 |
group | 设置文件或目录属组信息 |
- [root@m01 ~]# ansible zeq -m file -a "path=/tmp/zeq state=diretory"
- [root@m01 ~]# ansible zeq -m file -a "path=/tmp/tt state=touch mode=555 owner=root group=root"
- [root@m01 ~]# ansible zeq -m file -a "src=/tmp/tt path=/tmp/tt_link state=link"
3.8 crond 模块
正常使用 crond 服务
- [root@m01 ~]# crontab -l
- * * * * */bin/sh /server/scripts/yum.sh
使用 ansible 添加一条定时任务
[root@m01 ~]# ansible zeq -m cron -a "minute=* hour=* day=* month=* weekday=* job='/bin/sh /server/scripts/test.sh'"[root@m01 ~]# ansible zeq -m cron -a"job='/bin/sh /server/scripts/test.sh'"
设置定时任务注释信息, 防止重复, name 设定
[root@m01 ~]# ansible zeq -m cron -a "name='cron01'job='/bin/sh /server/scripts/test.sh'"
删除相应定时任务
[root@m01 ~]# ansible zeq -m cron -a "name='ansible cron02'minute=0 hour=0 job='/bin/sh /server/scripts/test.sh'state=absent"
注释相应定时任务, 使定时任务失效
[root@m01 scripts]# ansible zeq -m cron -a "name='ansible cron01'minute=0 hour=0 job='/bin/sh /server/scripts/test.sh'disabled=no"
3.9 mount 模块
参数 | 说明 |
mounted | 挂载设备, 并将配置写入 / etc/fstab |
unmounted | 卸载设备, 不会清除 / etc/fstab 写入的配置 |
absent | 卸载设备, 会清理 / etc/fstab 写入的配置 |
fstype | 指定挂载文件类型 |
opts | 设定挂载的参数选项信息 |
path | 指定挂载点 |
[root@m01 ~]# ansible zeq -m mount -a "path=/backup src=10.0.0.31:/data fstype=nfs opts=defautls,noatime state=mounted"
3.10 ansible 查看帮助方法
ansible-doc -l --- 查看所有模块说明信息
ansible-doc copy --- 表示指定查看某个模块参数用法信息
第 4 章 ansible 实战应用
4.1 推送你的公钥 (免密登录)
[root@m01 ~]# sshpass -p1 ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.41
4.2 配置 Ansible 的主机清单
- [root@m01 ~]# cat /etc/ansible/hosts
- [web]
- 172.16.1.7
- 172.16.1.8
- [nfs]
- 172.16.1.31
- [backup]
- 172.16.1.41
4.3 检查主机是否都 ok
- [root@m01 ~]# ansible all -m ping
- epel,firewalld,selinux,ww
4.4 基础环境:
1. 所有的主机都需要安装 rsync 和 nfs-utils
2. 所有的主机都需要准备对应的 rsync 客户端的密码文件 / etc/rsync.pass
3. 所有的主机都需要创建一个 uid 和 gid 为 666 的 www 用户
4. 所有的主机都需要全网备份的脚本, 并配置好定时任务
4.4.1 安装 rsync 和 nfs-utils
[root@m01 ~]# ansible all -m yum -a "name=rsync,nfs-utils state=installed"
4.4.2 准备 rsync 的客户端密码文件
[root@m01 ~]# ansible all -m copy -a "content='1'dest=/etc/rsync.pass owner=root group=root mode=600"
4.4.3 准备对应的 www 用户, uid 和 gid 都为 666
- [root@m01 ~]# ansible all -m group -a "name=www gid=666"
- [root@m01 ~]# ansible all -m user -a "name=www uid=666 group=666 create_home=no shell=/sbin/nologin"
4.4.4 从管理上拷贝对应的脚本文件, 然后将其加入定时任务
- [root@m01 ~]# ansible all -m copy -a "src=./scripts/rsync_backup_md5.sh dest=/server/scripts/ mode=755"
- [root@m01 ~]# ansible all -m cron -a "name='Rsync Bakcup Scripts'hour=01 minute=00 job='/bin/bash /server/scripts/rsync_backup_md5.sh &>/dev/null'"
- [root@m01 ~]# pwd
- /root
- [root@m01 ~]# mkdir scripts
- [root@m01 ~]# cat scripts/rsync_backup_md5.sh
- #!/usr/bin/bash
- export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
4.4.5 脚本编写
1. 定义变量
- Host=$(hostname)
- Addr=$(ifconfig eth1|awk 'NR==2{print $2}')
- Date=$(date +%F)
- Dest=${Host}_${Addr}_${Date}
- Path=/backup
2. 创建备份目录
[ -d $Path/$Dest ] || mkdir -p $Path/$Dest
3. 备份对应的文件
- cd / && \
- [ -f $Path/$Dest/system.tar.gz ] || tar czf $Path/$Dest/system.tar.gz etc/fstab etc/rsyncd.conf && \
- [ -f $Path/$Dest/log.tar.gz ] || tar czf $Path/$Dest/log.tar.gz var/log/messages var/log/secure && \
4. 携带 md5 验证信息
[ -f $Path/$Dest/${Date}.flag ] || md5sum $Path/$Dest/*.tar.gz>$Path/$Dest/${Date}.flag
5. 推送本地数据至备份服务器
- export RSYNC_PASSWORD=1
- rsync -avz $Path/ rsync_backup@172.16.1.41::backup
6. 本地保留最近 7 天的数据
find $Path/ -type d -mtime +7|xargs rm -rf
4.5 应用环境:(配置 rsync 服务 ->Backup 服务器)
1. 安装 rsync
2. 配置 rsync,/etc/rsyncd.conf
3. 创建目录, 创建虚拟用户文件, 变更权限
4. 启动服务, 加入开机自启动
5. 配置邮箱, 准备对应的脚本
4.5.1 安装 rsync
[root@m01 ~]# ansible backup -m yum -a "name=rsync state=installed"
4.5.2 配置 rsync,/etc/rsyncd.conf
- [root@m01 ~]# ansible backup -m copy -a "src=./conf/rsyncd.conf dest=/etc/rsyncd.conf"
- [root@m01 ~]# pwd
- /root
- [root@m01 ~]# mkdir conf
- [root@m01 ~]# cat conf/rsyncd.conf
- uid = www
- gid = www
- port = 873
- fake super = yes
- use chroot = no
- max connections = 200
- timeout = 600
- ignore errors
- read only = false
- list = false
- auth users = rsync_backup
- secrets file = /etc/rsync.password
- log file = /var/log/rsyncd.log
- #####################################
- [backup]
- path = /backup
- [data]
- path = /data
4.5.3 创建目录, 变更权限, 创建虚拟用户文件
- [root@m01 ~]# ansible backup -m file -a "path=/backup state=directory mode=755 owner=www group=www"
- [root@m01 ~]# ansible backup -m file -a "path=/data state=directory mode=755 owner=www group=www"
- [root@m01 ~]# ansible backup -m copy -a "content='rsync_backup:1'dest=/etc/rsync.password mode=600 owner=root group=root"
4.5.4 启动服务, 加入开机自启动
[root@m01 ~]# ansible backup -m service -a "name=rsyncd state=started enabled=yes"
4.5.5 配置邮箱
配置邮箱 (配发件服务器)
yum install mailx -y
set from=xxxxxxx@qq.com 填写自己邮箱
set smtp=smtps://smtp.qq.com:465
set smtp-auth-user=xxxxxxxx@qq.com 填写自己邮箱
- set smtp-auth-password=# 填写客户端授权码
- set smtp-auth=login
- set ssl-verify=ignore
- set nss-config-dir=/etc/pki/nssdb/
4.5.6 准备对应的脚本
1. 定义全局的变量
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
2. 定义局部变量
- Path=/backup
- Date=$(date +%F)
3. 查看 flag 文件, 并对该文件进行校验, 然后将校验的结果保存至 result_时间
find $Path/*_${Date} -type f -name "flag"|xargs md5sum -c>$Path/result_${Date}
4. 将校验的结果发送邮件给管理员
mail -s "Rsync Backup $Date" 123@qq.com <$Path/result_${Date}
5. 删除超过 7 天的校验结果文件, 删除超过 180 天的备份数据文件
- find $Path/ -type f -name "result*" -mtime +7|xargs rm -f
- find $Path/ -type d -mtime +180|xargs rm -rf
4.6 应用环境:(配置 nfs 服务)
1. 安装 nfs-utils
2. 配置 nfs-utils
3. 创建对应的共享目录, 并修改权限
4. 启动 nfs
4.6.1 安装 nfs-utils
[root@m01 ~]# ansible nfs -m yum -a "name=nfs-utils state=installed"
4.6.2 配置 nfs-utils
[root@m01 ~]# ansible nfs -m copy -a "content='/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)'dest=/etc/exports"
4.6.3 创建对应的共享目录, 并递归修改权限
[root@m01 ~]# ansible nfs -m file -a "path=/data state=directory recurse=yes owner=www group=www mode=755"
4.6.4 启动 nfs
[root@m01 ~]# ansible nfs -m service -a "name=nfs-server state=started enabled=yes"
4.7 应用环境:(配置 web 服务, 挂载操作)
[root@m01 ~]# ansible web -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=mounted"
4.8 验证:
1. 验证 nfs 存储是否可以用
2. 验证 rsync 是否能完成推送
3.backup 服务器进行校验
来源: https://www.cnblogs.com/zeq912/p/9636904.html