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

ansible常用命令的简单练习

ansible常用命令的简单练习

用ansible临时命令完成以下操作

1、对node1主机操作,安装httpd服务,网页存放在/www目录中,能够通过curl http://node1访问到网页内容为welcome to luoqi
(1)配置yum仓库并安装httpd服务
[student@master ansible]$ ansible node1 -m yum_repository -a 'file=server name=aa description=aa1 baseurl=http://ansible.example.com/rhel9/BaseOS enabled=yes gpgcheck=no'
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=yes gpgcheck=no'
node1 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true,"repo": "bb","state": "present"
}
[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"]
}
(2)创建/www的目录设置其context值为httpd_sys_content_t
[student@master ansible]$ ansible node1 -m file -a 'path=/www state=directory setype=httpd_sys_content_t'
node1 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true,"gid": 0,"group": "root","mode": "0755","owner": "root","path": "/www","secontext": "unconfined_u:object_r:httpd_sys_content_t:s0","size": 6,"state": "directory","uid": 0
}
(3)在/www下写入一个index.html的文件,其内容为welcome to luoqi,设置其context值为httpd_sys_content_t
[student@master ansible]$ ansible node1 -m copy -a 'content="welcome to luoqi" dest=/www/index.html setype=httpd_sys_content_t'
node1 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true,"checksum": "2433ad103a9ed17b296822118d46955ba31ad8ef","dest": "/www/index.html","gid": 0,"group": "root","md5sum": "fd2ab3cea037b6fdd568d5014b787c14","mode": "0644","owner": "root","secontext": "system_u:object_r:httpd_sys_content_t:s0","size": 16,"src": "/home/student/.ansible/tmp/ansible-tmp-1756436213.0637414-868-126882287786795/source","state": "file","uid": 0
}
(4)替换配置文件中的内容(http访问的目录改为/www,有两个地方需要修改)
[student@master ansible]$ ansible node1 -m replace -a 'path=/etc/httpd/conf/httpd.conf regexp="DocumentRoot "/var/www/html"" replace="DocumentRoot "/www""'
node1 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true,"msg": "1 replacements made","rc": 0
}
[student@master ansible]$ ansible node1 -m replace -a 'path=/etc/httpd/conf/httpd.conf regexp="<Directory "/var/www">" replace="<Directory "/www">"'
node1 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true,"msg": "1 replacements made","rc": 0
}
(5)设置防火墙(允许访问http服务)
[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"
}
(6)起服务
[student@master ansible]$ ansible node1 -m service -a 'name=httpd state=restarted enabled=yes'
node1 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},
......[student@master ansible]$ curl http://node1
welcome to luoqi[student@master ansible]$ 
2、对node2主机操作,创建一个1000MiB的分区,格式化成ext4的文件系统,并挂载到/testdir目录下。

使用ansible node2 -m shell -a 'df -Th’验证

(1)添加一块硬盘(略)
(2)创建分区
[student@master ansible]$ ansible node2 -m parted -a 'device=/dev/vdb number=1 part_type=primary part_start=10MiB part_end=1010MiB 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": 1034240.0,"flags": [],"fstype": "","name": "","num": 1,"size": 1024000.0,"unit": "kib"}],"script": "unit KiB mklabel msdos mkpart primary 10MiB 1010MiB"
}
(3)格式化(文件系统),并查看UUID
[student@master ansible]$ ansible node2 -m filesystem -a 'dev=/dev/vdb1 fstype=ext4'
node2 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true
}
[student@master ansible]$ ansible node2 -m shell -a 'blkid /dev/vdb1'
node2 | CHANGED | rc=0 >>
/dev/vdb1: UUID="ab990f13-b8d4-4215-aebb-85f5cf2f519a" TYPE="ext4" PARTUUID="88bd718e-01"
(4)创建挂载目录,并挂载
[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
}
[student@master ansible]$ ansible node2 -m mount -a 'src=UUID=ab990f13-b8d4-4215-aebb-85f5cf2f519a 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=ab990f13-b8d4-4215-aebb-85f5cf2f519a"
}
[student@master ansible]$ ansible node2 -m shell -a 'df -Th'
node2 | CHANGED | rc=0 >>
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  4.0M     0  4.0M   0% /dev
tmpfs          tmpfs     985M     0  985M   0% /dev/shm
tmpfs          tmpfs     394M  5.6M  389M   2% /run
/dev/vda3      xfs        17G  1.2G   16G   7% /
/dev/vda1      xfs      1014M  182M  833M  18% /boot
tmpfs          tmpfs     197M     0  197M   0% /run/user/0
tmpfs          tmpfs     197M     0  197M   0% /run/user/1000
/dev/vdb1      ext4      966M   24K  900M   1% /testdir
3、对node3主机操作创建卷组datastorage,逻辑卷database,大小为800M,格式化为xfs的文件系统,并挂载到/lv目录下

使用ansible node3 -m shell -a 'df -Th’验证

(1)添加一块硬盘
(2)创建分区
[student@master ansible]$ ansible node3 -m parted -a 'device=/dev/vdb number=1 part_type=primary part_start=10MiB part_end=1010MiB 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": 1034240.0,"flags": [],"fstype": "","name": "","num": 1,"size": 1024000.0,"unit": "kib"}],"script": "unit KiB mklabel msdos mkpart primary 10MiB 1010MiB"
}
(3)配置yum仓库并安装lvm2
[student@master ansible]$ ansible node3 -m yum_repository -a 'file=server name=aa description=aa1 baseurl=http://ansible.example.com/rhel9/BaseOS enabled=yes gpgcheck=no'
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=yes gpgcheck=no'
node3 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true,"repo": "bb","state": "present"
}
[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"]
}
(4)创建卷组
[student@master ansible]$ ansible node3 -m lvg -a 'vg=datasorage pvs=/dev/vdb1'
node3 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true
}
(5)创建逻辑卷
[student@master ansible]$ ansible node3 -m lvol -a 'lv=database vg=datasorage size=800M'node3 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true,"msg": ""
}
(6)格式化(文件系统),并查询UUID
[student@master ansible]$ ansible node3 -m filesystem -a 'dev=/dev/datasorage/database fstype=xfs'
node3 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": true
}
[student@master ansible]$ ansible node3 -m shell -a 'blkid /dev/datasorage/database'
node3 | CHANGED | rc=0 >>
/dev/datasorage/database: UUID="4af0ca97-a6ec-441c-a36f-9e85352eac4b" TYPE="xfs"
(7)创建挂载目录,并挂载
[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
}
[student@master ansible]$ ansible node3 -m mount -a 'src="UUID=4af0ca97-a6ec-441c-a36f-9e85352eac4b" 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=4af0ca97-a6ec-441c-a36f-9e85352eac4b"
}
[student@master ansible]$ ansible node3 -m shell -a 'df -Th'
node3 | CHANGED | rc=0 >>
Filesystem                      Type      Size  Used Avail Use% Mounted on
devtmpfs                        devtmpfs  4.0M     0  4.0M   0% /dev
tmpfs                           tmpfs     985M     0  985M   0% /dev/shm
tmpfs                           tmpfs     394M  5.6M  389M   2% /run
/dev/vda3                       xfs        17G  1.2G   16G   8% /
/dev/vda1                       xfs      1014M  182M  833M  18% /boot
tmpfs                           tmpfs     197M     0  197M   0% /run/user/0
tmpfs                           tmpfs     197M     0  197M   0% /run/user/1000
/dev/mapper/datasorage-database xfs       794M   38M  757M   5% /lv
http://www.dtcms.com/a/355555.html

相关文章:

  • Linux系统 -- 多线程的控制(互斥与同步)
  • 数学思维好题(冯哈伯公式)-平方根下取整求和
  • 个人博客运行3个月记录
  • 了解ADS中信号和电源完整性的S参数因果关系
  • Typora 教程:从零开始掌握 Markdown 高效写作
  • MySQL事务ACID特性
  • JavaScript中的BOM,DOM和事件
  • 英语单词:ad-hoc
  • BugKu Web渗透之成绩查询
  • 白杨SEO:网站优化怎么做?应用举例和适合哪些企业做?参考
  • 速成Javascript(二)
  • 新书速览|SQL Server运维之道
  • 【第三方网站运行环境测试:服务器配置(如Nginx/Apache)的WEB安全测试重点】
  • 激活函数篇(3):Softmax
  • maven scope 详解
  • 通信原理实验之线性均衡器-迫零算法
  • dht11传感器总结
  • [灵动微电子 MM32BIN560CN MM32SPIN0280]读懂电机MCU之串口DMA
  • 【C++游记】子承父业——乃继承也
  • 91美剧网官网入口 - 最新美剧资源在线观看网站
  • 保姆级教程 | 在Ubuntu上部署Claude Code Plan Mode全过程
  • 【论文阅读】MotionXpert:基于肌电信号的优化下肢运动检测分类
  • Spring事务管理机制深度解析:从JDBC基础到Spring高级实现
  • [灵动微电子MM32SPIN0280]从灵动微电子看电机专用MCU
  • Deeplizard 深度学习课程(五)—— 模型训练
  • 数据结构01:顺序表
  • react Antd Table 多选大数据量 UI渲染很慢的解决方案
  • 每日五个pyecharts可视化图表日历图和箱线图:从入门到精通
  • ChatGPT登录,拒绝访问,错误1020解决办法
  • THM Whats Your Name WP