实例学习ansible系列 9 常用模块之yum/service

本文详细介绍了如何使用Ansible的yum模块进行httpd服务器的安装、卸载,以及使用service模块进行服务的启动、停止和重启操作。通过具体示例展示了Ansible在自动化运维中的应用,包括依赖项处理、服务状态检查和开机自启设置。

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

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.youkuaiyun.com/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                       
 

知识点:yum模块用于yum安装包安装和卸载等操作。
  知识点:service模块用于系统服务管理操作,比如启动停止等操作。

使用yum模块安装httpd

 

事前确认未曾安装

[root@host31 ~]# ansible host31 -m shell -a "rpm -qa |grep httpd"host31 | FAILED | rc=1 >>[root@host31 ~]#
  
  • 1
  • 2
  • 3
  • 4
 

安装
  [root@host31 ~]# ansible host31 -m yum -a “name=httpd”
  host31 | SUCCESS => {
      “changed”: true,
      “msg”: “”,
      “rc”: 0,
      “results”: [
          “Loaded plugins: fastestmirror, langpacks\nLoading mirror speeds from cached hostfile\n * base: mirrors.yun-idc.com\n * epel: mirrors.ustc.edu.cn\n * extras: mirrors.btte.net\n * updates: mirrors.btte.net\nResolving Dependencies\n–> Running transaction check\n—> Package httpd.x86_64 0:2.4.6-40.el7.centos.4 will be installed\n–> Processing Dependency: httpd-tools = 2.4.6-40.el7.centos.4 for package: httpd-2.4.6-40.el7.centos.4.x86_64\n–> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-40.el7.centos.4.x86_64\n–> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-40.el7.centos.4.x86_64\n–> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-40.el7.centos.4.x86_64\n–> Running transaction check\n—> Package apr.x86_64 0:1.4.8-3.el7 will be installed\n—> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed\n—> Package httpd-tools.x86_64 0:2.4.6-40.el7.centos.4 will be installed\n—> Package mailcap.noarch 0:2.1.41-2.el7 will be installed\n–> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package           Arch         Version                     Repository     Size\n================================================================================\nInstalling:\n httpd             x86_64       2.4.6-40.el7.centos.4       updates       2.7 M\nInstalling for dependencies:\n apr               x86_64       1.4.8-3.el7                 base          103 k\n apr-util          x86_64       1.5.2-6.el7                 base           92 k\n httpd-tools       x86_64       2.4.6-40.el7.centos.4       updates        83 k\n mailcap           noarch       2.1.41-2.el7                base           31 k\n\nTransaction Summary\n================================================================================\nInstall  1 Package (+4 Dependent packages)\n\nTotal download size: 3.0 M\nInstalled size: 10 M\nDownloading packages:\n——————————————————————————–\nTotal                                              343 kB/s | 3.0 MB  00:08     \nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Installing : apr-1.4.8-3.el7.x86_64                                       1/5 \n  Installing : apr-util-1.5.2-6.el7.x86_64                                  2/5 \n  Installing : httpd-tools-2.4.6-40.el7.centos.4.x86_64                     3/5 \n  Installing : mailcap-2.1.41-2.el7.noarch                                  4/5 \n  Installing : httpd-2.4.6-40.el7.centos.4.x86_64                           5/5 \n  Verifying  : httpd-tools-2.4.6-40.el7.centos.4.x86_64                     1/5 \n  Verifying  : apr-1.4.8-3.el7.x86_64                                       2/5 \n  Verifying  : mailcap-2.1.41-2.el7.noarch                                  3/5 \n  Verifying  : httpd-2.4.6-40.el7.centos.4.x86_64                           4/5 \n  Verifying  : apr-util-1.5.2-6.el7.x86_64                                  5/5 \n\nInstalled:\n  httpd.x86_64 0:2.4.6-40.el7.centos.4                                          \n\nDependency Installed:\n  apr.x86_64 0:1.4.8-3.el7                      apr-util.x86_64 0:1.5.2-6.el7   \n  httpd-tools.x86_64 0:2.4.6-40.el7.centos.4    mailcap.noarch 0:2.1.41-2.el7   \n\nComplete!\n”
      ]
  }
  [root@host31 ~]#

>确认安装[root@host31 ~]#ansible host31 -m shell -a "rpm -qa |grep httpd"host31 | SUCCESS | rc=0 >>httpd-tools-2.4.6-40.el7.centos.4.x86_64httpd-2.4.6-40.el7.centos.4.x86_64[root@host31 ~]#
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

使用service启动httpd服务

 

事前未曾启动

[root@host31 ~]# ansible host31 -m shell -a "systemctl status httpd"host31 | FAILED | rc=3 >>● httpd.service - The Apache HTTP Server   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)   Active: inactive (dead)     Docs: man:httpd(8)           man:apachectl(8)Jul 30 17:24:04 host31 systemd[1]: Starting The Apache HTTP Server...Jul 30 17:24:04 host31 httpd[63250]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.32.31. Set the 'ServerName' directive globally to suppress this messageJul 30 17:24:04 host31 systemd[1]: Started The Apache HTTP Server.Jul 30 17:25:15 host31 systemd[1]: Stopping The Apache HTTP Server...Jul 30 17:25:16 host31 systemd[1]: Stopped The Apache HTTP Server.[root@host31 ~]#
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
 

使用service模块启动httpd

[root@host31 ~]# ansible host31 -m service -a "name=httpd state=started"host31 | SUCCESS => {    "changed": true,    "name": "httpd",    "state": "started"}[root@host31 ~]#
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
 

确认启动状态

[root@host31 ~]# ansible host31 -m shell -a "systemctl status httpd"host31 | SUCCESS | rc=0 >>● httpd.service - The Apache HTTP Server   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)   Active: active (running) since Sat 2016-07-30 17:27:53 EDT; 1min 32s ago     Docs: man:httpd(8)           man:apachectl(8) Main PID: 64517 (httpd)   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"   CGroup: /system.slice/httpd.service           tq64517 /usr/sbin/httpd -DFOREGROUND           tq64521 /usr/sbin/httpd -DFOREGROUND           tq64522 /usr/sbin/httpd -DFOREGROUND           tq64523 /usr/sbin/httpd -DFOREGROUND           tq64524 /usr/sbin/httpd -DFOREGROUND           mq64528 /usr/sbin/httpd -DFOREGROUNDJul 30 17:27:53 host31 systemd[1]: Starting The Apache HTTP Server...Jul 30 17:27:53 host31 httpd[64517]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.32.31. Set the 'ServerName' directive globally to suppress this messageJul 30 17:27:53 host31 systemd[1]: Started The Apache HTTP Server.[root@host31 ~]#[root@host31 ~]#
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
 

确认启动httpd
  httpd

使用service 停止httpd

[root@host31 ~]# ansible host31 -m service -a "name=httpd state=stopped"host31 | SUCCESS => {    "changed": true,    "name": "httpd",    "state": "stopped"}[root@host31 ~]# ansible host31 -m command -a "systemctl status httpd"host31 | FAILED | rc=3 >>● httpd.service - The Apache HTTP Server   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)   Active: inactive (dead)     Docs: man:httpd(8)           man:apachectl(8)Jul 30 17:24:04 host31 systemd[1]: Starting The Apache HTTP Server...Jul 30 17:24:04 host31 httpd[63250]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.32.31. Set the 'ServerName' directive globally to suppress this messageJul 30 17:24:04 host31 systemd[1]: Started The Apache HTTP Server.Jul 30 17:25:15 host31 systemd[1]: Stopping The Apache HTTP Server...Jul 30 17:25:16 host31 systemd[1]: Stopped The Apache HTTP Server.Jul 30 17:27:53 host31 systemd[1]: Starting The Apache HTTP Server...Jul 30 17:27:53 host31 httpd[64517]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.32.31. Set the 'ServerName' directive globally to suppress this messageJul 30 17:27:53 host31 systemd[1]: Started The Apache HTTP Server.Jul 30 17:35:14 host31 systemd[1]: Stopping The Apache HTTP Server...Jul 30 17:35:15 host31 systemd[1]: Stopped The Apache HTTP Server.[root@host31 ~]#
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

使用service模块重启httpd并设定开机自启

 

设定httpd,使得其能开机自启

[root@host31 ~]# ansible host31 -m service -a "name=httpd enabled=yes state=restarted"host31 | SUCCESS => {    "changed": true,    "enabled": true,    "name": "httpd",    "state": "started"}[root@host31 ~]# ansible host31 -m shell -a "systemctl status httpd"host31 | SUCCESS | rc=0 >>● httpd.service - The Apache HTTP Server   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)   Active: active (running) since Sat 2016-07-30 17:38:15 EDT; 24s ago     Docs: man:httpd(8)           man:apachectl(8) Main PID: 66574 (httpd)   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"   CGroup: /system.slice/httpd.service           tq66574 /usr/sbin/httpd -DFOREGROUND           tq66575 /usr/sbin/httpd -DFOREGROUND           tq66576 /usr/sbin/httpd -DFOREGROUND           tq66577 /usr/sbin/httpd -DFOREGROUND           tq66578 /usr/sbin/httpd -DFOREGROUND           mq66579 /usr/sbin/httpd -DFOREGROUNDJul 30 17:38:14 host31 systemd[1]: Starting The Apache HTTP Server...Jul 30 17:38:15 host31 httpd[66574]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.32.31. Set the 'ServerName' directive globally to suppress this messageJul 30 17:38:15 host31 systemd[1]: Started The Apache HTTP Server.[root@host31 ~]#
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

使用yum模块删除httpd

 

可以看出其能正常remove httpd-2.4.6-40.el7.centos.4.x86_64

[root@host31 ~]# ansible host31 -m shell -a "rpm -qa |grep httpd"host31 | SUCCESS | rc=0 >>httpd-2.4.6-40.el7.centos.4.x86_64httpd-tools-2.4.6-40.el7.centos.4.x86_64[root@host31 ~]#[root@host31 ~]# ansible host31 -m yum -a "name=httpd state=absent"host31 | SUCCESS => {    "changed": true,    "msg": "",    "rc": 0,    "results": [        "Loaded plugins: fastestmirror, langpacks\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-40.el7.centos.4 will be erased\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package      Arch          Version                       Repository       Size\n================================================================================\nRemoving:\n httpd        x86_64        2.4.6-40.el7.centos.4         @updates        9.4 M\n\nTransaction Summary\n================================================================================\nRemove  1 Package\n\nInstalled size: 9.4 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Erasing    : httpd-2.4.6-40.el7.centos.4.x86_64                           1/1 \n  Verifying  : httpd-2.4.6-40.el7.centos.4.x86_64                           1/1 \n\nRemoved:\n  httpd.x86_64 0:2.4.6-40.el7.centos.4                                          \n\nComplete!\n"    ]}[root@host31 ~]# ansible host31 -m shell -a "rpm -qa |grep httpd"host31 | SUCCESS | rc=0 >>httpd-tools-2.4.6-40.el7.centos.4.x86_64[root@host31 ~]#
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
           

给我老师的人工智能教程打call!http://blog.youkuaiyun.com/jiangjunshow
这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值