基础模块的应用作业描述1、在第3台机器上新建一个帐号并将新建的帐号添加至wheel组2、在第3台机器上新建帐号的家目录中新建一个空文件并往文件中添加“hello,ansible”内容3、将第3台机器上的新建的文件取回至管理机管理机就是第一台机子。4、将取回管理机的文件传送至第2台机器5、将第2台机器的光驱挂载并设置为开机自动挂载。前言逐题拆解 直接可复制执行命令 验证方法完全适配的环境管理机test01第 1 台被管理机test02第 2 台、test03第 3 台模块用到user、copy、file、fetch、mount、shell作业 1在第 3 台机器新建账号加入 wheel 组命令直接执行bash运行ansible test03 -m user -a namework03 statepresent groupswheel appendyes --become说明namework03用户名可自己改groupswheel加入 wheel 组appendyes不覆盖原有组只追加重要--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 ownerwork03 groupwork03 --become方法 B用 file shell纯远程创建bash运行# 创建空文件 ansible test03 -m file -a path/home/work03/hello.txt statetouch ownerwork03 groupwork03 --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 的文件取回管理机 test01fetch 拉取命令bash运行ansible test03 -m fetch -a src/home/work03/hello.txt dest/tmp/ flatyes --become验证在管理机执行bash运行cat /tmp/hello.txt作业 4把管理机文件传到 test02copy 推送bash运行ansible test02 -m copy -a src/tmp/hello.txt dest/tmp/ --become验证bash运行ansible test02 -m shell -a cat /tmp/hello.txt作业 5test02 挂载光驱并设置开机自动挂载1创建挂载点bash运行ansible test02 -m file -a path/mnt/cdrom statedirectory --become2挂载光驱bash运行ansible test02 -m shell -a mount /dev/sr0 /mnt/cdrom --become3写入 fstab 开机自动挂载bash运行ansible test02 -m shell -a echo /dev/sr0 /mnt/cdrom iso9660 defaults 0 0 /etc/fstab --become4验证挂载 开机挂载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 namework03 statepresent groupswheel appendyes --become # 2. test03 家目录创建文件并写入内容 echo hello,ansible /tmp/hello.txt ansible test03 -m copy -a src/tmp/hello.txt dest/home/work03/hello.txt ownerwork03 groupwork03 --become # 3. 从 test03 取回文件到管理机 ansible test03 -m fetch -a src/home/work03/hello.txt dest/tmp/ flatyes --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 statedirectory --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 如果嫌麻烦测试环境常用如果是测试环境想快速做完作业可以临时关闭 SELinuxbash运行# 临时关闭重启后恢复 ansible test02 -m shell -a setenforce 0 --become # 永久关闭写入配置文件 ansible test02 -m shell -a sed -i s/^SELINUX.*/SELINUXdisabled/ /etc/selinux/config --become关闭后再重新执行 copy 命令也能成功。✅ 验证结果执行成功后用以下命令验证文件是否送达bash运行ansible test02 -m shell -a cat /tmp/hello.txt如果输出hello,ansible说明任务完成