第1章 Ansible基本概述
 
1.1 ansible是一个配置管理系统configuration management system,
 
ASP站长网你只需要可以使用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

dawei

【声明】:九江站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。