当前位置: 首页 > news >正文

Ansible 基础模块实战作业详细教程

基础模块的应用作业描述

1、在第3台机器上新建一个帐号,并将新建的帐号添加至wheel组

2、在第3台机器上新建帐号的家目录中新建一个空文件,并往文件中添加“hello,ansible”内容

3、将第3台机器上的新建的文件取回至管理机,管理机就是第一台机子。

4、将取回管理机的文件传送至第2台机器

5、将第2台机器的光驱挂载并设置为开机自动挂载。

前言

逐题拆解 + 直接可复制执行命令 + 验证方法,完全适配的环境:

  • 管理机:test01(第 1 台)
  • 被管理机:test02(第 2 台)、test03(第 3 台)
  • 模块用到:usercopyfilefetchmountshell

作业 1:在第 3 台机器新建账号,加入 wheel 组

命令(直接执行)

bash

运行

ansible test03 -m user -a "name=work03 state=present groups=wheel append=yes" --become

说明

  • name=work03:用户名(可自己改)
  • groups=wheel:加入 wheel 组
  • append=yes不覆盖原有组,只追加(重要!)
  • --become:提权 root

验证

bash

运行

ansible test03 -m shell -a "id work03"

作业 2:在 test03 新用户家目录创建文件并写入内容

方法 A:用 copy 模块(最简单、推荐)

bash

运行

# 本地先创建文件 echo "hello,ansible" > /tmp/hello.txt # 推送到 test03 的 /home/work03/ ansible test03 -m copy -a "src=/tmp/hello.txt dest=/home/work03/hello.txt owner=work03 group=work03" --become

方法 B:用 file + shell(纯远程创建)

bash

运行

# 创建空文件 ansible test03 -m file -a "path=/home/work03/hello.txt state=touch owner=work03 group=work03" --become # 写入内容 ansible test03 -m shell -a "echo 'hello,ansible' > /home/work03/hello.txt" --become

验证

bash

运行

ansible test03 -m shell -a "cat /home/work03/hello.txt"

作业 3:把 test03 的文件取回管理机 test01

fetch 拉取命令

bash

运行

ansible test03 -m fetch -a "src=/home/work03/hello.txt dest=/tmp/ flat=yes" --become

验证(在管理机执行)

bash

运行

cat /tmp/hello.txt

作业 4:把管理机文件传到 test02

copy 推送

bash

运行

ansible test02 -m copy -a "src=/tmp/hello.txt dest=/tmp/" --become

验证

bash

运行

ansible test02 -m shell -a "cat /tmp/hello.txt"

作业 5:test02 挂载光驱并设置开机自动挂载

1)创建挂载点

bash

运行

ansible test02 -m file -a "path=/mnt/cdrom state=directory" --become

2)挂载光驱

bash

运行

ansible test02 -m shell -a "mount /dev/sr0 /mnt/cdrom" --become

3)写入 fstab 开机自动挂载

bash

运行

ansible test02 -m shell -a "echo '/dev/sr0 /mnt/cdrom iso9660 defaults 0 0' >> /etc/fstab" --become

4)验证挂载 & 开机挂载

bash

运行

# 查看挂载 ansible test02 -m shell -a "df -h | grep cdrom" # 验证 fstab ansible test02 -m shell -a "cat /etc/fstab | tail -5"


全套一键汇总版(可直接复制跑)

bash

运行

# 1. test03 创建用户并加入wheel组 ansible test03 -m user -a "name=work03 state=present groups=wheel append=yes" --become # 2. test03 家目录创建文件并写入内容 echo "hello,ansible" > /tmp/hello.txt ansible test03 -m copy -a "src=/tmp/hello.txt dest=/home/work03/hello.txt owner=work03 group=work03" --become # 3. 从 test03 取回文件到管理机 ansible test03 -m fetch -a "src=/home/work03/hello.txt dest=/tmp/ flat=yes" --become # 4. 推送到 test02 ansible test02 -m copy -a "src=/tmp/hello.txt dest=/tmp/" --become # 5. test02 挂载光驱并开机自动挂载 ansible test02 -m file -a "path=/mnt/cdrom state=directory" --become ansible test02 -m shell -a "mount /dev/sr0 /mnt/cdrom" --become ansible test02 -m shell -a "echo '/dev/sr0 /mnt/cdrom iso9660 defaults 0 0' >> /etc/fstab" --become

报错解决方法

这是SELinux 环境依赖报错

原因:test02 开启了SELinux,但缺少 Python 插件libselinux-python,导致 Ansible 的copy模块无法写入文件。在 openEuler 系统中,这个包的名字叫libselinux-python3


⚡️ 最快解决方法(直接复制执行)

步骤 1:先给 test02 安装依赖包

bash

运行

ansible test02 -m shell -a "dnf install -y libselinux-python3" --become
  • 必须加--become(提权到 root)
  • 安装完成后,依赖就补齐了

步骤 2:再次执行你的 copy 命令

bash

运行

ansible test02 -m copy -a "src=/tmp/hello.txt dest=/tmp/" --become

这次就会显示CHANGED,文件成功推送到 test02!


🌿 如果嫌麻烦(测试环境常用)

如果是测试环境,想快速做完作业,可以临时关闭 SELinux

bash

运行

# 临时关闭(重启后恢复) ansible test02 -m shell -a "setenforce 0" --become # 永久关闭(写入配置文件) ansible test02 -m shell -a "sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config" --become

关闭后再重新执行 copy 命令也能成功。


✅ 验证结果

执行成功后,用以下命令验证文件是否送达:

bash

运行

ansible test02 -m shell -a "cat /tmp/hello.txt"

如果输出hello,ansible,说明任务完成!

http://www.jsqmd.com/news/602374/

相关文章:

  • Jellyfin Bangumi插件终极指南:5分钟打造完美动漫媒体库
  • Linux C编程基础知识(日期与时间操作)
  • 第一题笔记 以及第七题和第九题可检测通过的代码
  • Tesseract OCR:开源文字识别引擎的终极完整指南
  • 阿里CosyVoice3镜像功能体验:自然语言控制语音风格,情感表达更精准
  • 告别模拟器:APK-Installer让Windows运行安卓应用的创新方案
  • 终极指南:三步解锁Cursor Pro完整功能,免费体验AI编程无限潜力
  • 小龙虾大战傀儡师
  • 电脑端微信防撤回
  • 如何高效下载B站视频?Downkyi五步法轻松掌握
  • Eigen库FFT实战:如何用自带FFT替代FFTW3提升计算效率(附避坑指南)
  • 2025届学术党必备的五大AI辅助写作网站推荐
  • 揭秘济南时行水旋柜,品牌和服务在行业排名如何? - 工业品网
  • AI智能文档扫描仪部署教程:支持多种文档类型的通用方案
  • Python编写Flask接口如何防止爬虫抓取_使用User-Agent与频率限制
  • EmuDeck:革新Steam Deck模拟器体验的一站式配置工具
  • 2026年4月全球工程管理系统推荐:TOP5口碑产品评测比较知名 - 品牌推荐
  • 高尔夫球检测数据集VOC+YOLO格式9489张1类别
  • 活字格低代码 —— 企业级数字化转型的首选利器
  • 5分钟掌握ModTheSpire:打造你的专属Slay The Spire模组体验
  • PEAL+: Enhancing Low-overlap Point Cloud Registration with Prior-guided Attention and Iterative Refi
  • 2026年南京及周边专业机构名录盘点 - 资讯焦点
  • YOLO26最新创新改进系列:YOLO26+自动计数+自动统计各个类别数量!弯道超车,丰富文章工作量!!
  • 从安装到批量预测:手把手带你用Uni-Mol Docking V2完成一次虚拟筛选(附ABL1案例代码)
  • 2026年分切复卷机好用推荐,设备精良的制造厂哪家更值得选 - mypinpai
  • 一键生成全篇论文!精选7款AI写论文工具亲测,期刊论文写作不愁!
  • 高效掌握BilibiliDown:B站音视频全流程下载指南
  • Pixel Mind Decoder 效果深度评测:多场景文本情绪识别准确率对比
  • Phi-4-mini-reasoning实战手册:日志分析+错误定位+服务健康检查
  • 彻底修复Windows更新问题的终极指南:Reset Windows Update Tool详解