一、提交要求
-
每个模块必须完成对应任务。
-
提交执行命令和关键结果截图。
-
提交完整实验文档。
-
每项任务必须进行结果验证。
二、Ansible 13 个常用模块企业实战
项目名称:企业 Linux 服务器批量自动化运维
企业现有 3 台 Linux 服务器,需要通过 Ansible 统一管理,完成以下 13 项任务。
模块 需求
|机器规划 | 角色 | 主机名 | IP | 操作说明 |
|----|----|----|----|
| 管理端 | ansible‑master|10.8.0.19|所有 ansible 命令全部在这台执行|
| 被控端 | web01|10.8.0.21| 只需要保证 ssh、python,不需要装 ansible|
| 被控端 | db01|10.8.0.22| 只需要保证 ssh、python,不需要装 ansible|
前提:3 台机器网络互通,防火墙放行 22 端口;全部使用 root 用户操作。
1 ping 检查所有被控端是否可以正常连接,确保 SSH 通信正常。
ansible all -m ping

2 command 批量查看所有服务器的主机名、系统时间和磁盘使用情况。
ansible all -m command -a 'hostname'
ansible all -m command -a 'date'
ansible all -m command -a 'df -h'

3 shell 批量获取所有服务器的 IP 地址,并保存到 /opt/server_ip.txt。
ansible all -m shell -a "hostname -I >> /opt/server_ip.txt"
验证:ansible all -m shell -a 'cat /opt/server_ip.txt'

4 copy 将管理端 /etc/hosts 文件分发到所有被控端的 /opt/ 目录。
ansible all -m copy -a "src=/etc/hosts dest=/opt/hosts"

5 file 在所有 Web 服务器创建 /data/www 目录,权限设置为 755。
ansible webservers -m file -a "path=/data/www state=directory mode=755"

6 script 编写初始化脚本,批量关闭防火墙并设置开机不启动。
①管理端先写脚本 init_firewalld.sh
cat > init_firewalld.sh <<EOF
#!/bin/bash
systemctl stop firewalld
systemctl disable firewalld
EOF
chmod +x init_firewalld.sh
② script 模块推送并执行(脚本只存在管理端,不用提前传到被控机)
ansible all -m script -a "./init_firewalld.sh"

7 hostname 将 Web 服务器主机名修改为 web01、web02。
ansible 10.8.0.21 -m hostname -a "name=web01"
ansible 10.8.0.22 -m hostname -a "name=web02"
验证:ansible webservers -m command -a 'hostname'


8 yum 在 Web 服务器批量安装 Nginx。
CentOS7 需要 epel 源,先装 epel 再装 nginx
ansible webservers -m yum -a "name=epel-release state=present"
ansible webservers -m yum -a "name=nginx state=present"

9 service 启动 Nginx 服务,并设置开机自动启动。
ansible webservers -m service -a "name=nginx state=started enabled=yes"

10 user 在所有服务器创建运维用户 ops,UID 设置为 2000。
11 group 在所有服务器创建运维用户组 ops,GID 设置为 2000。
先创建 group 组,再创建 user 用户
ansible all -m group -a "name=ops gid=2000 state=present"
ansible all -m user -a "name=ops uid=2000 group=ops state=present"


#验证
ansible all -a "id ops"

12 cron 配置每天凌晨 2 点执行日志清理任务,清理 /tmp 下 7 天前的临时文件。
ansible all -m cron -a "name='clear tmp old file' minute=0 hour=2 job='find /tmp -type f -mtime +7 -delete'"

验证
ansible all -a 'crontab -l'

删除:靠 name 精确定位
ansible all -m cron -a 'name="clear tmp old file" state=absent'

13 setup 收集所有服务器的主机名、IP、CPU、内存、操作系统版本信息
全部facts
ansible all -m setup

过滤只看需要的信息(常用过滤写法)
ansible all -m setup -a "filter=ansible_hostname,ansible_default_ipv4,ansible_cpu*,ansible_mem*,ansible_distribution*"

三、最终目标:
假设企业有 100 台 Linux 服务器,要求使用 Ansible 批量完成以上运维任务,不能逐台手动操作。
特点:
-
覆盖本次实训全部 13 项任务;
-
增加 tags 标签,可以单独执行某一项任务,不用全部跑;
-
增加错误处理 any_errors_fatal ,一台失败就停止,防止故障扩散到 100 台;
-
分组执行:webservers 组 / 全量 all;
-
幂等设计,重复执行无害;
-
适配 CentOS7,root 执行;
-
注释齐全,适合直接放进实验报告。
前置:
管理端已经配置好 SSH 免密、inventory 主机清单,清单分组和之前一致
vim /etc/ansible/hosts
webservers
10.8.0.21
dbservers
10.8.0.22
all_server:children
webservers
dbservers
- name: Ansible 13模块批量运维任务(适配100台集群,带标签+错误中断)
hosts: all_server
gather_facts: true
any_errors_fatal: true # 错误处理:任意一台失败,立刻终止整个play,
避免大规模事故
become: false # 本次实验直接root操作,不需要sudo
vars:
ops_uid: 2000
ops_gid: 2000
cron_job_name: "clear tmp old file"
tasks:
1 ping连通性检查
- name: 任务1 - 连通性检测 ping
ansible.builtin.ping:
tags:
-
t1
-
ping
2 command:查看主机名、时间、磁盘
- name: 任务2 - command 获取主机名
ansible.builtin.command: hostname
register: res_hostname
tags:
-
t2
-
command
-
name: 任务2 - command 获取系统时间
ansible.builtin.command: date
register: res_date
tags:
-
t2
-
command
-
name: 任务2 - command 查看磁盘使用
ansible.builtin.command: df -h
register: res_df
tags:
-
t2
-
command
3 shell:写入IP到 /opt/server_ip.txt
- name: 任务3 - shell 获取IP并写入文件
ansible.builtin.shell: "hostname -I >> /opt/server_ip.txt"
tags:
-
t3
-
shell
-
name: 任务3 - 验证文件内容
ansible.builtin.shell: cat /opt/server_ip.txt
register: res_ipfile
tags:
-
t3
-
shell
4 copy 分发 /etc/hosts 到 /opt/hosts
- name: 任务4 - copy分发hosts文件
ansible.builtin.copy:
src: /etc/hosts
dest: /opt/hosts
mode: '0644'
tags:
-
t4
-
copy
5 file:仅web组创建 /data/www 755
- name: 任务5 - web服务器创建 /data/www
ansible.builtin.file:
path: /data/www
state: directory
mode: '0755'
when: inventory_hostname in groups"webservers"
tags:
-
t5
-
file
6 script:执行防火墙关闭脚本
- name: 任务6 - script关闭防火墙、开机不自启
ansible.builtin.script: ./init_firewalld.sh
tags:
-
t6
-
script
7 hostname 修改主机名(仅web组)
- name: 任务7 - 设置10.8.0.21主机名为web01
ansible.builtin.hostname:
name: web01
when: inventory_hostname == "10.8.0.21"
tags:
-
t7
-
hostname
-
name: 任务7 - 设置10.8.0.22主机名为web02
ansible.builtin.hostname:
name: web02
when: inventory_hostname == "10.8.0.22"
tags:
-
t7
-
hostname
8 yum web组安装epel、nginx
- name: 任务8 - web组安装epel-release
ansible.builtin.yum:
name: epel-release
state: present
when: inventory_hostname in groups"webservers"
tags:
-
t8
-
yum
-
name: 任务8 - web组安装nginx
ansible.builtin.yum:
name: nginx
state: present
when: inventory_hostname in groups"webservers"
tags:
-
t8
-
yum
9 service web组启动nginx+开机自启
- name: 任务9 - web组启动nginx并设置开机自启
ansible.builtin.service:
name: nginx
state: started
enabled: yes
when: inventory_hostname in groups"webservers"
tags:
-
t9
-
service
11 group:先创建ops组 GID=2000
- name: 任务11 - 创建ops用户组 gid=2000
ansible.builtin.group:
name: ops
gid: "{{ ops_gid }}"
state: present
tags:
-
t11
-
group
10 user:创建ops用户 UID=2000
- name: 任务10 - 创建ops用户 uid=2000,主组ops
ansible.builtin.user:
name: ops
uid: "{{ ops_uid }}"
group: ops
state: present
tags:
-
t10
-
user
12 cron 定时清理/tmp 7天前文件
- name: 任务12 - 创建定时任务 凌晨2点清理tmp
ansible.builtin.cron:
name: "{{ cron_job_name }}"
minute: "0"
hour: "2"
job: "find /tmp -type f -mtime +7 -delete"
state: present
tags:
-
t12
-
cron
13 setup 收集系统事实信息
- name: 任务13 - setup收集主机信息
ansible.builtin.setup:
filter:
-
ansible_hostname
-
ansible_default_ipv4
-
ansible_processor_vcpus
-
ansible_memtotal_mb
-
ansible_distribution
-
ansible_distribution_version
tags:
-
t13
-
setup
配套脚本 init_firewalld.sh,和 yml 放同一个目录
#!/bin/bash
systemctl stop firewalld
systemctl disable firewalld
赋权:
chmod +x init_firewalld.sh
执行命令(重点)
- 全部一次性跑完 13 项任务
ansible-playbook ansible_13_task.yml
- 只跑某一个任务(标签 tags,企业非常常用)
只执行ping连通检测
ansible-playbook ansible_13_task.yml --tags t1
只执行用户和用户组
ansible-playbook ansible_13_task.yml --tags t10,t11
只跑nginx安装启动
ansible-playbook ansible_13_task.yml --tags t8,t9
- 只对 webservers 组执行剧本
ansible-playbook ansible_13_task.yml -l webservers
- 只对单台测试(灰度测试,100 台集群上线前先测单台)
ansible-playbook ansible_13_task.yml -l 10.8.0.21
- 查看剧本里所有标签
ansible-playbook ansible_13_task.yml --list-tags
企业 100 台集群重点说明(拓展)
-
any_errors_fatal: true:生产大规模集群必备,一台报错直接停止,防止一半机器成功一半失败、配置不一致;
-
标签 tags:适合分批上线、故障修复、单独维护,不用每次全量重跑;
-
-l limit 参数:灰度发布!先测 1 台、再测一组,确认没问题再全量执行;
-
模块全部原生 ansible 内置模块,不需要额外插件;
-
幂等:重复执行不会重复创建用户、重复安装软件;
-
并发调优:修改`/etc/ansible/ansible.cfg
forks = 50
100 台机器同时最多并发 50 台,避免 SSH 连接风暴压垮管理机。