知识点:yum模块用于yum安装包安装和卸载等操作。
知识点:service模块用于系统服务管理操作,比如启动停止等操作。
使用yum模块安装httpd
事前确认未曾安装
[root@host31 ~]# ansible host31 -m shell -a "rpm -qa |grep httpd"
host31 | FAILED | rc=1 >>
[root@host31 ~]#
安装
[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_64
httpd-2.4.6-40.el7.centos.4.x86_64
[root@host31 ~]#
使用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 message
Jul 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 ~]#
使用service模块启动httpd
[root@host31 ~]# ansible host31 -m service -a "name=httpd state=started"
host31 | SUCCESS => {
"changed": true,
"name": "httpd",
"state": "started"
}
[root@host31 ~]#
确认启动状态
[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 -DFOREGROUND
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 message
Jul 30 17:27:53 host31 systemd[1]: Started The Apache HTTP Server.
[root@host31 ~]#
[root@host31 ~]#
确认启动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 message
Jul 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 message
Jul 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 ~]#
使用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 -DFOREGROUND
Jul 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 message
Jul 30 17:38:15 host31 systemd[1]: Started The Apache HTTP Server.
[root@host31 ~]#
使用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_64
httpd-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 ~]#