8月4日作业

该篇博客展示了如何使用Ansible进行系统配置管理,包括blockinfile、lineinfile、unarchive、archive、cron、user、yum_repository、service/systemd、firewalld、selinux、nmcli、get_url、uri、parted、lvg、lvol、filesystem、mount等模块的用法,涉及文件操作、服务管理、防火墙设置、用户创建、软件包管理等多个方面,体现了Ansible在自动化运维中的强大能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 blockinfile模块:

服务器:[root@server ~]# systemctl stop firewalld
[root@server .ansible]# ansible node -m blockinfile -a "path=/root/ansible_text block='blockinfile insert content'"
[root@server .ansible]# ansible node -m blockinfile -a "path=/root/ansible_text block='blockinfile with mark'" ker=#{mark} test
[root@server .ansible]# ansible node -m blockinfile -a "path=/root/ansible_text block='insertafter'insertafter='blockinfile insert content'"
[root@server .ansible]# ansible node -m blockinfile -a "path=/root/ansible_text block='insertbefore'insertbefore='blockinfile insert content'"
[root@server .ansible]# ansible node -m blockinfile -a "path=/root/ansible_text block="" marker= #{mark} service to start
[root@server .ansible]# ansible node -m blockinfile -a "path=/root/ansible_text block='HELLOW ANSIBLE HIIII'"
[root@server ~]#  ansible node -m lineinfile -a "path=/root/ansible_test regexp='(H,{4}).*(H.{4})' line='Hiiii' backrefs=yes"

linefile模块

[root@server ~]# ansible node -m lineinfile -a "path=/root/ansible_test line='lineinfile insert content'"
[root@server ~]# ansible node -m lineinfile -a "path=/root/ansible_test regexp='^lineinfile insert content' state=absent"
[root@server ~]# ansible node -m lineinfile -a "path=/root/ansible_test insertafter='lineinfile insert content'"
[root@server ~]# ansible node -m lineinfile -a "path=/root/ansible_test insertbefore='lineinfile insert content'"

unarchive模块

[root@server ~]# ansible all -m unarchive -a "src=/tmp/foo.zip dest=/data copy=no mode =0644"

archive模块

[root@server ~]# ansible websrvs -m archive -a "path=/var/log/ dest= data/log.tar.bz2 format=bz2 owner=student mode=0777"

cron模块

[root@server ~]# ansible node -m cron -a "hour=9 weekday=1-5 name='student' job=/root/alarm_corn"

user模块

[root@server ~]# python3
Python 3.6.8 (default, Jun 23 2022, 19:01:59) 
[GCC 8.5.0 20210514 (Red Hat 8.5.0-13)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import crypt
>>> crypt.crypt("123456")
'$6$gY87RFxBMILVeMDl$QONttsd2UynSPE2FKQhXqGbSkHCtdlH6VJdxymQrKj6DdPaB4Y05Nmdq/6.h1w/d9ByE/HTLCthi8w8l750Tq0'
[root@server ~]# ansible node -m user -a "name=HUYAN passwd='$6$gY87RFxBMILVeMDl$QONttsd2UynSPE2FKQhXqGbSkHCtdlH6VJdxymQrKj6DdPaB4Y05Nmdq/6.h1w/d9ByE/HTLCthi8w8l750Tq0'"
[root@server ~]# ansible node -m user -a "name=HUYAN state=absent remove=yes"

yum_repository

[root@server ~]# ansible node -m yum_repository -a "name=BaseOS baseurl ='file://mnt/my.repo' enable=1 gpgcheck=0 file=redhat description=BaseOS"

 service/systemd模块
 

[root@node2 ~]# ansible all -m systemd -a "name=firewalld state=stoped enabled=yes"
[root@node2 ~]# ansible all -m systemd -a "name=firewalld state=restarted enabled=yes"
[root@node2 ~]# ansible all -m service -a "name=firewalld state=stoped enabled=no"

firewalld模块

[root@server ~]# ansible node -m firewalld -a "prot=22/tcp state=enabled"
[root@server ~]# ansible node -m firewalld -a "service=http state=enabled"
[root@server ~]# ansible node -m firewalld -a "rich_rule='rule family=ipv4 source address=192.168.111.0/24 servicename=http accept' state=enabled"

selinux模块

[root@server ~]# ansible node -m selinux -a "state=permissive"

nmcli模块

[root@server ~]# ansible node -m nmcli -a "con_name=static ifname=ens224 ipv4=192.168.111.150/24 gw4=192.168.111.2 dns4=114.114.114.114 autoconnect=yes type=ethernet state=present "
[root@server ~]# ansible ndoe -m command -a "nmcli c up static"

get_url模块

[root@server ~]# ansible node -m get_url -a "url='https://video.pearvideo.com/mp4/short/20210816/cont-1739036-15747750-hd.mp4' dest=/root/ owner=student group=student mode=0644"

uri模块

[root@server ~]# ansible node -m uri -a "url='https://www.baidu.com' method=get return_content=yes"

parted模块

[root@server ~]# ansible node -m parted -a "device=/dev/nvme0n2 number=1 part_start=1GiB part_end=1GiB state=present"

lvg模块

[root@server ~]# ansible node -m lvg -a "pvs=/dev/nvme0n2p1 ,/dev/nvme0n2p2 pesize=4M vg=myvg"

lvol模块

[root@server ~]# ansible node -m lvol -a "lvs=mylvs vg=myvg size=500M"

filesystem模块

[root@server ~]# ansible node -m filesystem -a "device=/dev/nvme02np2 fstype=ext3"

 mount模块

[root@server ~]# ansible node -m mount -a "src=/dev/nvme0n2p3 dest=/ansible_mount state=mounted fstype=xfs"
[root@server ~]# ansible node -m mount -a "src=/dev/nvme0n2p3 dest=/ansible_mount1 state=present fstype=xfs"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值