7.6ansible-软件包管理模块
yum_repository(管理yum源)
yum(yum命令)
get_url(wget命令)1)yum源配置管理模块
| yum源模块 yum_repository | yum源配置文件内容 | |
|---|---|---|
| name | [epel] | yum源中的名字(中括号里面的名字即可) | 
| description | name=xxxxxx | yum源的注释说明 | 
| baseurl | baseurl= | yum源中下载软件包的地址(可以访问到repodata目录) | 
| enabled=yes 或no | enabled=1或0 | 是否启动这个源 | 
| gpgcheck=yes或no | gpgcheck=0 | 是否启动gpgcheck功能 | 
| file (可以不加) | 无 | 指定yum源的文件 自动添加 .repo file=lidao默认与模块名字一致 | 
[root@m01 ~]# cat /etc/yum.repos.d/epel.repo 
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=http://mirrors.aliyun.com/epel/7/$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7案例01-批量添加nginx-yum源
先写好nginx.repo文件
[nginx]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key再模仿写命令
ansible web -m yum_repository -a 'name=nginx description="nginx stable repo" baseurl="http://nginx.org/packages/centos/$releasever/$basearch/" gpgcheck=no enabled=yes'2)yum模块
通过yum命令安装软件
| yum模块 | |
|---|---|
| name | 指定软件包名字 | 
| state | installed 安装(present) removed 删除(absent) latest 安装或更新 | 
| 案例01-安装lrzsz | 
ansible all -a 'yum -e lrzsz'
ansible all -m yum -a 'name=lrzsz state=installed'案例02-web安装sl,cowsay,aalib
ansible web -m yum -a 'name=sl,cowsay,aalib state=installed'3)get_url模块(wget)
| get_url下载功能 | |
|---|---|
| url | 指定要下载的地址 | 
| dest | 下载到哪个目录 | 
| 案例01-下载tengine源码包到web服务器/server/tools/目录下 | 
1.先建目录
ansible web -m file -a 'path=/server/tools/ state=directory'
2.下载
ansible web -m get_url -a 'url=https://tengine.taobao.org/download/tengine-2.3.3.tar.gz dest=/server/tools/'
3.检查
ansible web -a 'tree /server/tools'4)软件包管理模块小结
| 模块名字 | |
|---|---|
| yum_repository | yum源配置文件. | 
| yum | 通过yum命令安装软件. | 
| get_url | wget命令下载软件包/源码,下载文件. | 
7.7系统管理模块
| mount | 挂载nfs模块 | 
|---|---|
| cron | 管理定时任务 | 
| 1)mount模块 | |
| mount | |
| - | - | 
| fstype | 指定文件系统(nfs) | 
| src | 源地址(nfs服务端地址) eg:172.16.1.31/data | 
| path | 挂载点(要把源挂载到那里) | 
| state | 参考下表 | 
| mount模块的state参数可使用的值 | |
|---|---|
| absent | 卸载(umount)并修改fstab(清理配置) | 
| unmounted | 卸载不修改/etc/fstab | 
| present | 仅修改/etc/fstab 不挂载 | 
| mounted | 挂载(用mount命令)并修改/etc/fstab(永久挂载) | 
| remounted | 重新挂载 | 
| 案例01-挂载案例 | 
web01把 nfs共享的目录/data目录挂载到 web01
的/upload_video
1.web01上面创建挂载点/upload_video
ansible web -m file -a 'path=/upload_video state=directory'
2.挂载nfs
ansible web -m mount -a 'fstype=nfs src="172.16.1.31:/data" path=/upload_video state=mounted'
3.检查
ansible web -a 'df -h'2)cron定时任务模块
| cron模块 定时任务模块 | 定时任务配置中的内容 | |
|---|---|---|
| name | #及后面的内容 | 定时任务名字(一定要加上),对应下面注释的内容 | 
| minute | */2 | 分钟minute="*/2",如果没有用到不用填写即可 | 
| hour | 小时 | |
| day | 日期 | |
| month | 月份 | |
| week | 周几 | |
| job | 命令/脚本 | 指定命令或脚本(定向到空) job="/sbin/ntpdate ntp1.aliyun.com &>/dev/null" | 
| state | present 默认是添加,添加定时任务; absent 删除 | 
案例01-添加自动同步时间的定时任务
参照:
#1. sync time hbinz 
*/2 * * * * /sbin/ntpdate ntp1.aliyun.com &>/dev/null
1.备份数据
ansible all -a 'cp /var/spool/cron/root /tmp/'
ansible all -a 'crontab -r'
2.书写定时任务
ansible all -m cron -a 'name="sync time by hbinz 231024" minute="*/2" job="/sbin/ntpdate ntp1.aliyun.com &>/dev/null" state=present'7.8用户管理模块
- group创建组模块
- user创建用户模块
1)user
| user模块 | ||
|---|---|---|
| name | www 用户名 | |
| uid | 指定uid | |
| group | 指定用户组 | |
| shell | 指定命令解释器 | |
| create_home | 是否创建家目录(yes/no) | |
| state | present absent | 
案例01-创建用户hbinz007
ansible all -m user -a 'name=hbinz007'案例02-创建虚拟用户tengine,指定uid为10086
ansible all -m user -a 'name=tengine uid=10086 shell=/sbin/nologin create_home=yes state=present'2)group模块
| group | |
|---|---|
| name | 指定用户组名字 | 
| gid | 指定组的gid | 
| state | present添加 absent 删除 | 
8.ansible实战-部署rsync服务端
8.1列出流程
1.服务部署:yum安装或者更新
2.配置文件
3.虚拟用户rsync
4.密码文件和权限
5.模块对应的目录,改所有者
6.重启服务8.2根据流程找出命令
| 部署rsync服务端 | 命令 | 
|---|---|
| 服务部署:yum 安装或更新 | yum -y install rsync | 
| 配置文件 | 先准备好配置文件,scp 传输到rsync服务器上面 | 
| 虚拟用户 rsync | useradd -s /sbin/nologin -M rsync | 
| 密码文件和权限 | echo 'rsync_backup:1' >/etc/rsync.password;然后修改权限chmod 600 /etc/rsync.password | 
| 模块对应目录,改所有者 | mkdir -p /data chown rsync.rsync /data | 
| 重启服务 | systemctl start rsyncd systemctl enable rsync | 
#配置文件:
#rsyncd.conf start#
#rsyncd 20221111
fake super = yes
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 172.16.1.0/24
#hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
[data]
comment = "backup dir by oldboy hbinz"
path = /data
[backup]
comment = "backup dir by oldboy hbinz"
path = /backup
[nfsbackup]
comment = 'nfsbackup dir by hbinz'
path = /nfsbackup8.3根据命令及对应操作选择ansible模块
| 部署rsync服务端 | 命令 | Ansible模块 | 
|---|---|---|
| 服务部署:yum 安装或更新 | yum install -y rsync | yum模块 | 
| 配置文件 | 先准备好配置文件,scp 传输到rsync服务器上面. | copy模块 | 
| 虚拟用户 rsync | useradd -s /sbin/nologin -M rsync | user模块 | 
| 密码文件和权限 | echo 'rsync_backup:1' >/etc/rsync.password然后修改权限chmod 600 /etc/rsync.password | file模块 | 
| 模块对应目录,改所有者 | mkdir -p /data;chown rsync.rsync /data | file模块 | 
| 重启服务 | systemctl start rsyncd;systemctl enable rsyncd | systemd模块 | 
| 1)服务部署 | 
ansible backup -m yum -a 'name=rsync state=lastest'2)配置文件分发
1.m01创建目录
mkdir -p /server/ans/pro-rsync
2.m01创建配置文件
vi rsyncd.conf
3.分发到backup/etc/下
ansible backup -m copy -a 'src=/server/ans/pro-rsync/rsyncd.conf dest=/etc/rsyncd.conf backup=yes'3)虚拟用户 rsync
ansible backup -m user -a 'name=rsync shell=/sbin/nologin create_home=no state=present'4)密码文件和权限
1.创建文件
ansible backup -m file -a 'path=/etc/rsync.password mode=600 state=touch'
2.追加密码
ansible backup -m lineinfile -a 'path=/etc/rsync.password line="rsync_backup:1"'5)模块对应目录,改所有者
ansible backup -m file -a 'path=/data owner=rsync group=rsync state=directory'6)重启服务
ansible backup -m systemd -a 'name=rsyncd enabled=yes state=started'7)测试
rsync -av /etc/hostname rsync_backup@172.16.1.31::data8)指定hosts文件的位置
ansible默认读/etc/ansible/下的hosts文件,如果后面希望精细化管理host的话,可以-i读绝对路径的host文件。
ansible -i hosts -m command -a 'xxxx'
9 ansible-playbook(剧本)
9.1 概述
我们作为导演,编排一出戏. 通过你设置步骤,让演员做对应动作操作.
剧本核心:指定主机,执行什么任务(模块),什么操作(选项).| 含义 | 应用场景 | |
|---|---|---|
| ad-hoc | ans命令运行对应模块与选项.(类似于命令) | 临时使用,测试使用 | 
| playbook | 通过文件执行(类似于脚本) | 用于批量管理,部署,收集信息,分发...(文件重复执行) | 
| playbook vs shell脚本 | 
| 剧本与脚本 | 应用场景 | 
|---|---|
| playbook脚本 | 批量管理,部署,收集信息 | 
| shell脚本 | 某一台,服务脚本,系统巡检,定时备份 | 
9.2playbook剧本快速使用指南
- 
剧本格式叫yaml格式 
- 
缩进,不要用tab键,空格. 
  
- 
核心格式剧本中所有的内容要对齐 
- 
对齐的时候不能使用tab键 
- 
只能使用空格,2个空格 
- 
hosts用于指定在哪些主机执行指令 
- 
tasks:用于对于这些主机,运行什么模块及选项 
1)案例01-在所有机器的/tmp下面创建hbinz.txt
[root@m01 /server/ans/playbook]# tree ./
./
├── 01.touch.yaml
└── hosts
0 directories, 2 files
    
cat 01.touch.yaml
- hosts: all
  vars:
    filename: hbinz.txt
  tasks:
    - name: touch file
      shell: touch /tmp/{{filename}}2)案例02:添加定时同步时间的定时任务
ansible命令:
ansible all -m cron -a 'name="sync time by hbinz 231024" minute=*/2 job="/sbin/ntpdate ntp1.aliyun.com &>/dev/null" state=present'
playbook初阶:
---
- hosts: all
  tasks:
    - name: add cron sync time
    cron:name="sync time by hbinz 231024" minute=*/2 job="/sbin/ntpdate ntp1.aliyun.com &>/dev/null" state=present
    
playbook优化:
---
- hosts: all
  tasks:
    - name: "add cron sync time"
      cron:
        name: "add cron sync time"
        minute: "*/2"
        job: "/sbin/ntpdate ntp1.aliyun.com &>/dev/null"
        state: present
#检查语法:		
ansible-playbook -i hosts -C 02.add-cron.yaml
#执行:
ansible-playbook -i hosts 02.add-cron.yaml- 在剧本中使用模块和选项
- 选项最好是一行一个选项,选项后面的跟着冒号
- 选项要对齐与缩进.
3)案例03-企业案例-批量下载安装zabbix-agent2-6.0客户端并启动
zabbix-agent2客户端
1.下载rpm包
2.安装rpm包
3.启动服务zabbi-agent2| zabbix-agent2客户端 | 命令 | 模块 | 
|---|---|---|
| 下载rpm包 | wget | get_url | 
| 安装rpm包 | rpm/yum | yum | 
| 启动zabbix-agent2服务 | systemctl | systemd | 
模块对应的命令:
zabbix_url
---
- hosts: all
  tasks:
    - name: 1.download zabbix agent2 rpm
      get_url:
        url: https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/6.0/rhel/7/x86_64/zabbix-agent2-6.0.0-1.el7.x86_64.rpm
        dest: /tmp/
        validate_certs: no  #wget发现不要认证才可以下载
    - name: 2.install zabbix agent2 rpm
      yum:
        name: /tmp/zabbix-agent2-6.0.0-1.el7.x86_64.rpm
        state: installed
    - name: 3.start zabbix agent2 service
      systemd:
        name: zabbix-agent2
        enabled: yes
        state: started案例04-部署rsync服务端
- 
准备:当前目录包含,rsyncd.conf配置文件。 
 命令:1) 服务部署:yum 安装或更新ansible backup -m yum -a 'name=rsync state=lastest' 2) 配置文件分发mkdir -p /server/ans/pro-rsync 
 准备配置文件存放在 上面目录中 rsyncd.conf
 ansible backup -m copy -a 'src=/server/ans/pro-rsync/rsyncd.conf dest=/etc/rsyncd.conf backup=yes'3) 虚拟用户 rsyncansible backup -m user -a 'name=rsync shell=/sbin/nologin create_home=no state=present' 4)密码文件和权限创建文件ansible backup -m file -a 'path=/etc/rsync.password mode=600 state=touch' 
 ##增加
 ansible backup -m lineinfile -a'path=/etc/rsync.password line="rsync_backup:1"'5)模块对应目录,改所有者ansible backup -m file -a 'path=/data owner=rsync group=rsync state=directory' 6) 重启服务ansible backup -m systemd -a 'name=rsyncd enabled=yes state=started' 
playbook:04.backup-rsyncd.yaml
---
- hosts: backup
  tasks:
    - name: 1)服务部署:yum 安装或更新
      yum:
        name: rsync
        state: lastest
    - name: 2) 配置文件分发
      copy:
        src: /server/ans/playbook/rsyncd.conf
        dest: /etc/rsyncd.conf
        backup: yes
    - name: 3) 虚拟用户 rsync
      user:
        name: rsync
        shell: /sbin/nologin
        create_home: no
        state: present
    - name: 4)密码文件和权限
      lineinfile:
        path: /etc/rsync.password
        mode: 600
        line: "rsync_backup:1"
        create: true
    - name: 5)模块对应目录,改所有者
      file:
        path: /data
        owner: rsync
        group: rsync
        state: directory
    - name: 5)重启服务
      systemd:
        name: rsyncd
        enabled: yes
        state: started总结
- 模块
- 剧本:熟练掌握一部分模块,熟练掌握服务使用流程
- 任务:
- 完成批量部署zabbix客户端
- 完成部署rsync服务端
- 完成部署nfs服务端