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

测试题ansible临时命令模块

使用ansible临时命令完成以下操作
1、对node1主机操作,安装httpd服务,网页存放在/www目录中,能够通过curl http://node1访问到网页内容为welcome to luoqi

步骤

1.配置yum仓库

[student@master ansible]$ ansible node1 -m yum_repository -a 'file=server name=aa description=aa1 baseurl=http://ansible.example.com/rhel9/BaseOS enabled=1 gpgcheck=0'
node1 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true,"repo": "aa","state": "present"
}
[student@master ansible]$ ansible node1 -m yum_repository -a 'file=server name=bb description=bb1 baseurl=http://ansible.example.com/rhel9/AppStream enabled=1 gpgcheck=0'
node1 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true,"repo": "bb","state": "present"
}

2.安装httpd

[student@master ansible]$ ansible node1 -m yum -a 'name=httpd state=present'
node1 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true,"msg": "","rc": 0,"results": ["Installed: redhat-logos-httpd-90.4-1.el9.noarch","Installed: mailcap-2.1.49-5.el9.noarch","Installed: httpd-2.4.53-7.el9.x86_64","Installed: apr-1.7.0-11.el9.x86_64","Installed: mod_http2-1.15.19-2.el9.x86_64","Installed: apr-util-1.6.1-20.el9.x86_64","Installed: apr-util-bdb-1.6.1-20.el9.x86_64","Installed: httpd-core-2.4.53-7.el9.x86_64","Installed: httpd-filesystem-2.4.53-7.el9.noarch","Installed: httpd-tools-2.4.53-7.el9.x86_64","Installed: apr-util-openssl-1.6.1-20.el9.x86_64","Installed: mod_lua-2.4.53-7.el9.x86_64"]
}

3.添加httpd到防火墙

[student@master ansible]$ ansible node1 -m firewalld -a 'service=http permanent=yes state=enabled immediate=yes'
node1 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true,"msg": "Permanent and Non-Permanent(immediate) operation, Changed service http to enabled"
}

4.将httpd的配置文件/var/www/html替换为/www

[student@master ansible]$ ansible node1 -m replace -a 'path=/etc/httpd/conf/httpd.conf regexp=/var/www/html replace=/www'
node1 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true,"msg": "2 replacements made","rc": 0
}
[student@master ansible]$ ansible node1 -m replace -a 'path=/etc/httpd/conf/httpd.conf regexp=/var/www replace=/www'
node1 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true,"msg": "2 replacements made","rc": 0
}

5.创建/www目录在index.html写welcome to luoqi

[student@master ansible]$ ansible node1 -m copy -a 'content="welcome to luoqi\n" dest=/www/index.html'
node1 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true,"checksum": "080c07240d102be98b4ba40c353c6be1ed069804","dest": "/www/index.html","gid": 0,"group": "root","md5sum": "fe7dc696d70cc7434ef50c0b2073c16d","mode": "0644","owner": "root","secontext": "unconfined_u:object_r:default_t:s0","size": 17,"src": "/home/student/.ansible/tmp/ansible-tmp-1756347674.2490287-1145-199591374172433/source","state": "file","uid": 0
}

6.修改context值

[student@master ansible]$ ansible node1 -m sefcontext -a 'target="/www(/.*)?" setype=httpd_sys_content_t state=present'
node1 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true,"ftype": "a","serange": "s0","setype": "httpd_sys_content_t","seuser": "system_u","state": "present","target": "/www(/.*)?"
}
[student@master ansible]$ ansible node1 -m shell -a 'restorecon -Rv /www'
node1 | CHANGED | rc=0 >>
Relabeled /www from unconfined_u:object_r:default_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
Relabeled /www/index.html from unconfined_u:object_r:default_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0

7.重启HTTPd服务

[student@master ansible]$ ansible node1 -m service -a 'name=httpd state=restarted enabled=yes'
node1 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"

8.访问curl http://node1

[root@node1 ~]# curl  http://node1
welcome to luoqi

2、对node2主机操作,创建一个1000MiB的分区,格式化成ext4的文件系统,并挂载到/testdir目录下。
使用ansible node2 -m shell -a 'df -Th’验证

1.新建分区

[student@master ansible]$ ansible node2 -m parted -a 'device=/dev/vdb number=1 part_type=primary part_start=10MiB part_end=1100MiB state=present '
node2 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true,"disk": {"dev": "/dev/vdb","logical_block": 512,"model": "Virtio Block Device","physical_block": 512,"size": 20971520.0,"table": "msdos","unit": "kib"},"partitions": [{"begin": 10240.0,"end": 1126400.0,"flags": [],"fstype": "","name": "","num": 1,"size": 1116160.0,"unit": "kib"}],"script": "unit KiB mklabel msdos mkpart primary 10MiB 1100MiB"
}

2.格式化

[student@master ansible]$ ansible node2 -m filesystem -a 'fstype=ext4 dev=/dev/vdb1'
node2 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true
}

3.创建目录

[student@master ansible]$ ansible node2 -m file -a 'path=/testdir state=directory'
node2 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true,"gid": 0,"group": "root","mode": "0755","owner": "root","path": "/testdir","secontext": "unconfined_u:object_r:default_t:s0","size": 6,"state": "directory","uid": 0
}

4.查看uuid与挂载

[student@master ansible]$ ansible node2 -m shell -a 'blkid '
node2 | CHANGED | rc=0 >>
/dev/vdb1: UUID="b0e99531-86aa-40cc-9e9a-6cb1a5fae2ab" TYPE="ext4" PARTUUID="9912f982-01"
/dev/vda2: UUID="34618ec3-66af-4160-8406-33d0b42ef615" TYPE="swap" PARTUUID="8ce7aacc-02"
/dev/vda3: UUID="da83ee92-b95d-4d97-ab27-1a8d07feae26" TYPE="xfs" PARTUUID="8ce7aacc-03"
/dev/vda1: UUID="3b634a5f-acdc-4ae8-a616-14d10a41501a" TYPE="xfs" PARTUUID="8ce7aacc-01"
[student@master ansible]$ ansible node2 -m mount -a 'src="UUID=b0e99531-86aa-40cc-9e9a-6cb1a5fae2ab" path=/testdir fstype=ext4 state=mounted'
node2 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"backup_file": "","boot": "yes","changed": true,"dump": "0","fstab": "/etc/fstab","fstype": "ext4","name": "/testdir","opts": "defaults","passno": "0","src": "UUID=b0e99531-86aa-40cc-9e9a-6cb1a5fae2ab"
}

5.查看挂载

[student@master ansible]$ ansible node2 -m shell -a 'df -h'
4^Hnode2 | CHANGED | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        4.0M     0  4.0M   0% /dev
tmpfs           973M     0  973M   0% /dev/shm
tmpfs           390M  5.5M  384M   2% /run
/dev/vda3        17G  1.2G   16G   7% /
/dev/vda1      1014M  182M  833M  18% /boot
tmpfs           195M     0  195M   0% /run/user/0
tmpfs           195M     0  195M   0% /run/user/1000
/dev/vdb1       1.1G   24K  968M   1% /testdir

3、对node3主机操作创建卷组vg0,逻辑卷lv0,大小为800M,格式化为xfs的文件系统,并挂载到/lv目录下
使用ansible node3 -m shell -a 'df -Th’验证

1.在node3配置yum仓库

[student@master ansible]$ ansible node3 -m yum_repository -a 'file=server name=aa description=aa1 baseurl=http://ansible.example.com/rhel9/BaseOS enabled=1 gpgcheck=0'
node3 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true,"repo": "aa","state": "present"
}
[student@master ansible]$ ansible node3 -m yum_repository -a 'file=server name=bb description=bb1 baseurl=http://ansible.example.com/rhel9/AppStream enabled=1 gpgcheck=0'
node3 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true,"repo": "bb","state": "present"
}

2.下载lvm2安装包物理卷相同

[student@master ansible]$ ansible node3 -m yum -a 'name=lvm2 state=present'
node3 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true,"msg": "","rc": 0,"results": ["Installed: device-mapper-libs-9:1.02.185-3.el9.x86_64","Installed: lvm2-9:2.03.16-3.el9.x86_64","Installed: lvm2-libs-9:2.03.16-3.el9.x86_64","Installed: device-mapper-persistent-data-0.9.0-13.el9.x86_64","Installed: libaio-0.3.111-13.el9.x86_64","Installed: device-mapper-9:1.02.185-3.el9.x86_64","Installed: device-mapper-event-9:1.02.185-3.el9.x86_64","Installed: device-mapper-event-libs-9:1.02.185-3.el9.x86_64","Removed: device-mapper-9:1.02.187-7.el9.x86_64","Removed: device-mapper-libs-9:1.02.187-7.el9.x86_64"]
}

3.新建分区与物理卷相同

[student@master ansible]$ ansible node3 -m parted -a 'device=/dev/vdb number=1 part_type=primary part_start=10MiB part_end=5000MiB state=present'
node3 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true,"disk": {"dev": "/dev/vdb","logical_block": 512,"model": "Virtio Block Device","physical_block": 512,"size": 20971520.0,"table": "msdos","unit": "kib"},"partitions": [{"begin": 10240.0,"end": 5120000.0,"flags": [],"fstype": "","name": "","num": 1,"size": 5109760.0,"unit": "kib"}],"script": "unit KiB mklabel msdos mkpart primary 10MiB 5000MiB"
}

3.添加卷组

[student@master ansible]$ ansible node3 -m lvg -a 'vg=vg0 pvs=/dev/vdb1'
node3 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true
}

4.添加逻辑卷

[student@master ansible]$ ansible node3 -m lvol -a 'lv=lv0 size=800 vg=vg0'
node3 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true,"msg": ""
}

5.格式化

[student@master ansible]$ ansible node3 -m filesystem -a 'fstype=xfs dev=/dev/vg0/lv0'
node3 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true
}

6.创建/lv目录

[student@master ansible]$ ansible node3 -m file -a 'path=/lv state=directory'
node3 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true,"gid": 0,"group": "root","mode": "0755","owner": "root","path": "/lv","secontext": "unconfined_u:object_r:default_t:s0","size": 6,"state": "directory","uid": 0
}

7.查看uuid与挂载

[student@master ansible]$ ansible node3 -m shell -a 'blkid'
node3 | CHANGED | rc=0 >>
/dev/vdb1: UUID="5Dmn6h-fn9t-G0Wr-TSTv-PFiZ-dYKd-KPE1uK" TYPE="LVM2_member" PARTUUID="22cc57db-01"
/dev/mapper/vg0-lv0: UUID="5603032f-b354-4b47-a4af-1ff9994b87d6" TYPE="xfs"
/dev/vda2: UUID="34618ec3-66af-4160-8406-33d0b42ef615" TYPE="swap" PARTUUID="8ce7aacc-02"
/dev/vda3: UUID="da83ee92-b95d-4d97-ab27-1a8d07feae26" TYPE="xfs" PARTUUID="8ce7aacc-03"
/dev/vda1: UUID="3b634a5f-acdc-4ae8-a616-14d10a41501a" TYPE="xfs" PARTUUID="8ce7aacc-01"
[student@master ansible]$ ansible node3 -m mount -a 'src="UUID=5603032f-b354-4b47-a4af-1ff9994b87d6" path=/lv fstype=xfs state=mounted'
node3 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"backup_file": "","boot": "yes","changed": true,"dump": "0","fstab": "/etc/fstab","fstype": "xfs","name": "/lv","opts": "defaults","passno": "0","src": "UUID=5603032f-b354-4b47-a4af-1ff9994b87d6"
}

8.查看挂载点

[student@master ansible]$ ansible node3 -m shell -a 'df -h'
node3 | CHANGED | rc=0 >>
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             4.0M     0  4.0M   0% /dev
tmpfs                973M     0  973M   0% /dev/shm
tmpfs                390M  5.5M  384M   2% /run
/dev/vda3             17G  1.2G   16G   8% /
/dev/vda1           1014M  182M  833M  18% /boot
tmpfs                195M     0  195M   0% /run/user/1000
/dev/mapper/vg0-lv0  794M   38M  757M   5% /lv
http://www.dtcms.com/a/355496.html

相关文章:

  • 自己定义的模型如何用hf的from_pretrained
  • Vue开发避坑:箭头函数与普通函数的正确使用指南
  • R 语言 eulerr 包绘制韦恩图:比例精准
  • 漫谈《数字图像处理》之区域生长和区域分离聚合
  • LeetCode100-41缺失的第一个正数
  • AI实时故障诊断系统(实时采集信号)
  • MySQL 中 tinyint(1)、int(11)、bigint(20) 的数字到底是什么意思?
  • (笔记)输入法框架协作机制深度分析
  • 内网穿透工具【frp】的核心功能底层处理逻辑解析
  • WINTRUST!_ExplodeMessage的作用是赋值psIndirectData
  • Windows 11 中 PowerShell 与 CMD 的深度对比:从定位到实战
  • 集成 A2A Protocol - BeeAI 框架的智能代理通信解决方案
  • 机器人芯片:驱动智能机器的核心技术引擎
  • 有限与无限的游戏 之感
  • 稳石氢能受邀出席2025势银绿氢产业大会,荣获“2025绿氢技术突破奖”!
  • SAP SD模块用户经常遇到的痛点以及解决方案
  • Circuitjs 测试点的使用
  • HTML+CSS、JavaScript、Vue、Ajax
  • Docker 40个自动化管理脚本-2 (40/40)
  • 【动态规划】子数组、子串问题
  • 国产芯力量!贴片式SD卡搭载北京君正Rk瑞芯微,打造嵌入式存储低延迟+高可靠黄金组合​
  • MongoDB 文档模型设计:JSON 结构的灵活性与陷阱
  • Mac训练大模型:MLX-LM框架LoRA训练Qwen3并集成SwanLab进行可视化
  • 基于mac的智能语音处理与应用开发-环境部署
  • 【LangGraph】核心概念速通:State/Node/Edge、通道、事件与流式输出
  • Java8-21的核心特性以及用法
  • FPGA位宽调整模块
  • 跨语言 UDP 聊天程序实现:Go 客户端与 Python 服务端[超简单 入门级聊天程序 包含完整源码]
  • 线段树 (Segment Tree)
  • 理解AI智能体:智能体记忆