Linux系统实现ansible自动化安装配置httpd的方法
1、使用ansible的playbook实现自动化安装httpd
1)首先配置好ansible的hosts文件,让其对应主机能够受ansible控制
提示:我们在主机清单上配置了所管控的主机地址,但是直接用ansible的ping模块去探测主机的存活情况,却显示权限拒绝。从提示上说让我们要指定用什么验证。默认情况ansible是通过ssh的key验证的,所以我们在ansible的主机清单中配置了管控主机的ip是不够的,还要配置ssh基于key验证
2)配置管控主机能够基于ssh key验证
[root@test ~]# ip a s enp2s0 2: enp2s0: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state up group default qlen 1000 link/ether 00:30:18:51:af:3c brd ff:ff:ff:ff:ff:ff inet 192.168.0.99/24 brd 192.168.0.255 scope global noprefixroute enp2s0 valid_lft forever preferred_lft forever inet 172.16.1.2/16 brd 172.16.255.255 scope global noprefixroute enp2s0:0 valid_lft forever preferred_lft forever inet6 fe80::230:18ff:fe51:af3c/64 scope link valid_lft forever preferred_lft forever [root@test ~]# ssh-keygen generating public/private rsa key pair. enter file in which to save the key (/root/.ssh/id_rsa): created directory '/root/.ssh'. enter passphrase (empty for no passphrase): enter same passphrase again: your identification has been saved in /root/.ssh/id_rsa. your public key has been saved in /root/.ssh/id_rsa.pub. the key fingerprint is: sha256:uorxi5jhikdbohzp3fsbszfycjqucjwqdl1qcntygsw root@test the key's randomart image is: +---[rsa 2048]----+ |+.....oo= . | |.+.o.o b.+.. | |o + *o=o. | |o..... ++ | |.o * + os. | | = b b . | |.o = e o | |o . = o | | . | +----[sha256]-----+ [root@test ~]# ssh-copy-id 192.168.0.99 -p 41319 /bin/ssh-copy-id: info: source of key(s) to be installed: "/root/.ssh/id_rsa.pub" the authenticity of host '[192.168.0.99]:41319 ([192.168.0.99]:41319)' can't be established. ecdsa key fingerprint is sha256:w2pd2pa2k9tgkgvk+weiincveskuahjsti263ovqbh4. ecdsa key fingerprint is md5:3a:f8:c9:b1:63:c6:c1:ae:e0:6e:e2:ca:17:4a:20:7a. are you sure you want to continue connecting (yes/no)? yes /bin/ssh-copy-id: info: attempting to log in with the new key(s), to filter out any that are already installed /bin/ssh-copy-id: info: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@192.168.0.99's password: number of key(s) added: 1 now try logging into the machine, with: "ssh -p '41319' '192.168.0.99'" and check to make sure that only the key(s) you wanted were added. [root@test ~]# scp -r .ssh 192.168.0.10:~/ the authenticity of host '192.168.0.10 (192.168.0.10)' can't be established. ecdsa key fingerprint is sha256:eg9nua4jjuueofhexlgqel9hx5h53jynoqf2vf53mii. ecdsa key fingerprint is md5:57:83:e6:46:2c:4b:bb:33:13:56:17:f7:fd:76:71:cc. are you sure you want to continue connecting (yes/no)? yes warning: permanently added '192.168.0.10' (ecdsa) to the list of known hosts. root@192.168.0.10's password: id_rsa 100% 1675 677.0kb/s 00:00 id_rsa.pub 100% 391 207.6kb/s 00:00 known_hosts 100% 356 12.2kb/s 00:00 authorized_keys 100% 391 12.6kb/s 00:00 [root@test ~]#
提示:做ssh基于key验证需要在ansible主机上做,我上面是现在ansible主机上生成一对ssh密钥,然后通过ssh-copy-id 把公钥复制给本机生成authorized_keys文件,然后在把.ssh目录复制给远端客户机,这样一来ansible主机可以通过ssh基于key免密登录远端客户机,同时远端客户机也可以通过ssh免密登录ansible主机,这样就实现了双向的ssh基于key验证,如果你只想单向的通过ssh基于key认证,你可以在ansible主机上生成密钥对,然后把公钥发给对方即可。有关ssh基于key认证的详细说明请参考本人博客
测试:用ansible主机通过ssh远程客户端主机
[root@test ~]# ssh 192.168.0.10 last login: mon jan 27 04:58:46 2020 from 192.168.0.99 [root@test-centos7-node1 ~]# ip a 1: lo: <loopback,up,lower_up> mtu 65536 qdisc noqueue state unknown qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state up qlen 1000 link/ether 00:0c:29:f2:82:0c brd ff:ff:ff:ff:ff:ff inet 192.168.0.10/24 brd 192.168.0.255 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fef2:820c/64 scope link valid_lft forever preferred_lft forever [root@test-centos7-node1 ~]# exit 登出 connection to 192.168.0.10 closed. [root@test ~]#
提示:可以看到ansible主机能够正常免密登录远端主机,接下我们在用ansible的ping模块去探测下被管控主机的存活
提示:能够看到用ansible的ping模块去探测远端主机的存活,返回的状态是success,数据是pong说明对端主机上存活的
到此ansible的环境就准备好了,接下来写playbook来安装httpd
[root@test ~]# cat install_httpd.yml --- - hosts: websers remote_user: root tasks: - name: copy epel file copy: src=/etc/yum.repos.d/centos-base.repo dest=/etc/yum.repos.d/centos-base.repo - name: install httpd yum: name=httpd - name: start httpd service: name=httpd state=started enabled=yes [root@test ~]# ansible-playbook -c install_httpd.yml play [websers] *********************************************************************************************************** task [copy epel file] **************************************************************************************************** changed: [192.168.0.10] task [install httpd] ***************************************************************************************************** changed: [192.168.0.10] task [start httpd] ******************************************************************************************************* changed: [192.168.0.10] play recap *************************************************************************************************************** 192.168.0.10 : ok=3 changed=3 unreachable=0 failed=0 [root@test ~]#
提示:以上playbook的主要内容是把本机的yum源复制到远端服务器上,然后通过yum去安装httpd包,最后启动httpd;在写好playbook后,我们用ansible-playbook -c install_httpd.yml 命令对我们写的playbook进行了测试,没有问题,接下来我们使用ansible-playbook来安装httpd
[root@test ~]# ansible-playbook install_httpd.yml play [websers] *********************************************************************************************************** task [copy epel file] **************************************************************************************************** changed: [192.168.0.10] task [install httpd] ***************************************************************************************************** changed: [192.168.0.10] task [start httpd] ******************************************************************************************************* changed: [192.168.0.10] play recap *************************************************************************************************************** 192.168.0.10 : ok=3 changed=3 unreachable=0 failed=0 [root@test ~]#
提示:从ansible-playbook 对playbook的执行状态来看是成功了,接下来我们直接使用浏览器来访问192.168.0.10,看看httpd是否已经能够正常访问,如果能正常访问说明httpd已经安装好了
提示:可以看到我们用浏览器是直接可以访问到192.168.0.10的测试页面,说明httpd在192.168.0.10上安装成功
2、建立httpd服务器,要求提供两个基于名称的虚拟主机:
(1)www.x.com,页面文件目录为/web/vhosts/x;错误日志为/var/log/httpd/x.err,访问日志为/var/log/httpd/x.access
新建虚拟主机www.x.com的配置文件
[root@test ~]# cat x_com.conf <virtualhost *:80> servername www.x.com documentroot "/web/vhosts/x" <directory "/web/vhosts/x"> options none allowoverride none require all granted </directory> errorlog "logs/x.err" customlog "logs/x.access" combined </virtualhost> [root@test ~]#
提示:我们在ansible主机上把配置文件建立好,待会直接用ansible把文件推送到对应主机的对应目录下即可使用
(2)www.y.com,页面文件目录为/web/vhosts/y;错误日志为/var/log/httpd/www2.err,访问日志为/var/log/httpd/y.access
新建虚拟主机www.y.com的配置文件
[root@test ~]# cat y_com.conf <virtualhost *:80> servername www.y.com documentroot "/web/vhosts/y" <directory "/web/vhosts/y"> options none allowoverride none require all granted </directory> errorlog "logs/www2.err" customlog "logs/y.access" combined </virtualhost> [root@test ~]#
(3)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名
[root@test ~]# cat x_index.html <h1> www.x.com</h1> [root@test ~]# cat y_index.html <h1> www.y.com</h1> [root@test ~]#
提示:以上文件在ansible主机上准备好了以后,我们接下来写一个playbook把对应的文件直接推送到远端主机即可
[root@test ~]# cat set_virtualhost_conf_file.yml --- - hosts: websers remote_user: root tasks: - name: mkdir virtualhost documentroot directory shell: mkdir -p /web/vhosts/{x,y} - name: copy x_com.conf to remotehost copy: src=/root/x_com.conf dest=/etc/httpd/conf.d/x_com.conf - name: copy x_com index file copy: src=/root/x_index.html dest=/web/vhosts/x/index.html - name: copy y_com.conf to remotehost copy: src=/root/y_com.conf dest=/etc/httpd/conf.d/y_com.conf - name: copy y_com index file copy: src=/root/y_index.html dest=/web/vhosts/y/index.html [root@test ~]#
提示:以上文件的内容主要把我们建立好的配置文件推送到对应主机的对应目录,接下来我们来检查下我们写的playbook是否语法问题
[root@test ~]# ansible-playbook -c set_virtualhost_conf_file.yml play [websers] *********************************************************************************************************** task [mkdir virtualhost documentroot directory] ************************************************************************** skipping: [192.168.0.10] task [copy x_com.conf to remotehost] ************************************************************************************* changed: [192.168.0.10] task [copy x_com index file] ********************************************************************************************* changed: [192.168.0.10] task [copy y_com.conf to remotehost] ************************************************************************************* changed: [192.168.0.10] task [copy y_com index file] ********************************************************************************************* changed: [192.168.0.10] play recap *************************************************************************************************************** 192.168.0.10 : ok=4 changed=4 unreachable=0 failed=0 [root@test ~]#
提示:测试playbook没有问题,接下来我们来运行playbook,把对应的文件推送到httpd服务器上,然后在服务器上检查我们写的配置文件是否正确
[root@test ~]# ansible-playbook set_virtualhost_conf_file.yml play [websers] *********************************************************************************************************** task [mkdir virtualhost documentroot directory] ************************************************************************** [warning]: consider using file module with state=directory rather than running mkdir changed: [192.168.0.10] task [copy x_com.conf to remotehost] ************************************************************************************* changed: [192.168.0.10] task [copy x_com index file] ********************************************************************************************* changed: [192.168.0.10] task [copy y_com.conf to remotehost] ************************************************************************************* changed: [192.168.0.10] task [copy y_com index file] ********************************************************************************************* changed: [192.168.0.10] play recap *************************************************************************************************************** 192.168.0.10 : ok=5 changed=5 unreachable=0 failed=0 [root@test ~]#
提示:从上面的返回状态看,都是成功的,只是第一个任务提示我们使用file模块去创建目录会更好一些,接下来我们使用ansible的shell模块去检查远端服务器上的配置文件的语法是否正确
[root@test ~]# ansible websers -m shell -a 'httpd -t' 192.168.0.10 | success | rc=0 >> ah00558: httpd: could not reliably determine the server's fully qualified domain name, using fe80::20c:29ff:fef2:820c. set the 'servername' directive globally to suppress this message syntax ok [root@test ~]#
提示:我们利用ansible在远程主机上检查httpd的配置文件语法是没有问题,它提示我们没有servername,这个提示可以不用管它,如果你觉得非要处理一下也可以,在httpd的主配置文件中找到servername 把对应的servername配置上就好了;接下来我们重启httpd,然后在客户机*问两个虚拟主机
[root@test ~]# ansible websers -m shell -a 'systemctl restart httpd' 192.168.0.10 | success | rc=0 >> [root@test ~]#
在客户端上更改/etc/hosts文件,让其访问www.x.com 和www.y.com 能够解析到远程主机
[root@test ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.0.10 www.x.com www.y.com [root@test ~]#
测试:利用curl 分别访问两个虚拟主机,看看对应的主页文件内容是否不同
[root@test ~]# curl http://www.x.com/index.html <h1> www.x.com</h1> [root@test ~]# curl http://www.y.com/index.html <h1> www.y.com</h1> [root@test ~]#
提示:我们利用curl对其各自虚拟主机的主页文件访问,都能看到对应的主页文件内容,当然你也可以更改windows的hosts文件,把对应的解析内容写进去,然后通过windows浏览器也是可以的,如下所示:
提示:在windows上找到hosts文件,然后把对应的记录添加进去,如下
提示:接下来我们利用windows的浏览器来访问下两个虚拟主机
接下我们在看看httpd服务器的日志是否都已生成
[root@test ~]# ansible websers -m shell -a 'ls -l /var/log/httpd' 192.168.0.10 | success | rc=0 >> 总用量 16 -rw-r--r--. 1 root root 2668 1月 27 06:30 access_log -rw-r--r--. 1 root root 2940 1月 27 07:34 error_log -rw-r--r--. 1 root root 0 1月 27 07:38 www2.err -rw-r--r--. 1 root root 500 1月 27 07:52 x.access -rw-r--r--. 1 root root 0 1月 27 07:38 x.err -rw-r--r--. 1 root root 500 1月 27 07:52 y.access [root@test ~]# ansible websers -m shell -a 'cat /var/log/httpd/x.access' 192.168.0.10 | success | rc=0 >> 192.168.0.99 - - [27/jan/2020:07:39:15 -0500] "get /index.html http/1.1" 200 20 "-" "curl/7.29.0" 192.168.0.232 - - [27/jan/2020:07:52:31 -0500] "get / http/1.1" 200 20 "-" "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/69.0.3497.100 safari/537.36" 192.168.0.232 - - [27/jan/2020:07:52:31 -0500] "get /favicon.ico http/1.1" 404 209 "http://www.x.com/" "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/69.0.3497.100 safari/537.36" [root@test ~]# ansible websers -m shell -a 'cat /var/log/httpd/y.access' 192.168.0.10 | success | rc=0 >> 192.168.0.99 - - [27/jan/2020:07:39:19 -0500] "get /index.html http/1.1" 200 20 "-" "curl/7.29.0" 192.168.0.232 - - [27/jan/2020:07:52:48 -0500] "get / http/1.1" 200 20 "-" "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/69.0.3497.100 safari/537.36" 192.168.0.232 - - [27/jan/2020:07:52:48 -0500] "get /favicon.ico http/1.1" 404 209 "http://www.y.com/" "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/69.0.3497.100 safari/537.36" [root@test ~]#
提示:可以看到httpd服务器上都已经生成了对应名称的日志文件。
总结
以上所述是小编给大家介绍的linux系统实现ansible自动化安装配置httpd的方法,希望对大家有所帮助!
下一篇: 祛斑小窍门,教你最天然无害的祛斑方法!
推荐阅读
-
Linux系统实现ansible自动化安装配置httpd的方法
-
监控软件Nagios的服务器端在Linux系统上的安装和配置方法
-
G40/50-70、SR1000随机Linux改安装Win7系统的实现方法
-
VMware中安装Linux系统(Redhat8)及虚拟机的网络配置方法
-
Oracle VM VirtualBox 在linux系统下安装增强插件实现访问主机的共享文档方法
-
linux下SVN配置实现项目目录自动更新以及源码安装的操作方法
-
Linux系统实现ansible自动化安装配置httpd的方法
-
监控软件Nagios的服务器端在Linux系统上的安装和配置方法
-
VMware中安装Linux系统(Redhat8)及虚拟机的网络配置方法
-
G40/50-70、SR1000随机Linux改安装Win7系统的实现方法