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

测试题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.xdnf.cn/news/18993.html

相关文章:

  • CuTe C++ 简介01,从示例开始
  • imx6ull-驱动开发篇47——Linux SPI 驱动实验
  • Electron解压缩文件
  • hive on tez为什么写表时,要写临时文件到hdfs目录
  • docker 1分钟 快速搭建 redis 哨兵集群
  • 配置nginx.conf (增加21001端口实例操作)
  • 医疗AI时代的生物医学Go编程:高性能计算与精准医疗的案例分析(三)
  • [灵动微电子 MM32BIN560CN MM32SPIN0280]读懂电机MCU之比较器
  • jQuery 从入门到实践:基础语法、事件与元素操作全解析
  • mac电脑双屏显示时程序坞跑到副屏的解决方法
  • 机器视觉学习-day10-图像添加水印
  • Mybatis 与 Springboot 集成过程详解
  • Kubernetes一EFK日志架构
  • Ovis2.5技术解密:原生分辨率与“反思模式”如何铸就新一代MLLM王者
  • 嵌入式学习日志————实验:串口发送串口发送+接收
  • 2025年渗透测试面试题总结-37(题目+回答)
  • 2024年06月 Python(三级)真题解析#中国电子学会#全国青少年软件编程等级考试
  • 零基础-力扣100题从易到难详解(持续更新1-10题)
  • 【链表 - LeetCode】25. K 个一组翻转链表
  • DAY 58 经典时序预测模型2
  • Kubernetes 的20 个核心命令分类详解
  • Linex系统网络管理(二)
  • 数据结构第8章 排序(竟成)
  • SqlHelper类库的使用方法
  • .NET周刊【8月第3期 2025-08-17】
  • 鸿蒙ArkUI 基础篇-06-组件基础语法-Column/Row/Text
  • 车载诊断架构 --- 基于整车功能的正向诊断需求开发
  • Dubbo加标签方式
  • Vue3 + 高德地图实现车辆电子围栏监控与报警
  • banner这个文件是怎么请求到后端数据的