系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 系统教程 > Linux教程 > 详细页面

Linux系统实现ansible自动化安装配置httpd的方法

时间:2020-02-15来源:系统城作者:电脑系统城

1、使用ansible的playbook实现自动化安装httpd

  1)首先配置好ansible的hosts文件,让其对应主机能够受ansible控制

Linux系统实现ansible自动化安装配置httpd的方法

  提示:我们在主机清单上配置了所管控的主机地址,但是直接用ansible的ping模块去探测主机的存活情况,却显示权限拒绝。从提示上说让我们要指定用什么验证。默认情况ansible是通过ssh的key验证的,所以我们在ansible的主机清单中配置了管控主机的ip是不够的,还要配置ssh基于KEY验证

  2)配置管控主机能够基于SSH key验证


 
  1. [root@test ~]# ip a s enp2s0
  2. 2: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
  3. link/ether 00:30:18:51:af:3c brd ff:ff:ff:ff:ff:ff
  4. inet 192.168.0.99/24 brd 192.168.0.255 scope global noprefixroute enp2s0
  5. valid_lft forever preferred_lft forever
  6. inet 172.16.1.2/16 brd 172.16.255.255 scope global noprefixroute enp2s0:0
  7. valid_lft forever preferred_lft forever
  8. inet6 fe80::230:18ff:fe51:af3c/64 scope link
  9. valid_lft forever preferred_lft forever
  10. [root@test ~]# ssh-keygen
  11. Generating public/private rsa key pair.
  12. Enter file in which to save the key (/root/.ssh/id_rsa):
  13. Created directory '/root/.ssh'.
  14. Enter passphrase (empty for no passphrase):
  15. Enter same passphrase again:
  16. Your identification has been saved in /root/.ssh/id_rsa.
  17. Your public key has been saved in /root/.ssh/id_rsa.pub.
  18. The key fingerprint is:
  19. SHA256:UORxi5JhiKDBOhZP3FsbsZfyCjqUcjwqdl1qcnTyGsw root@test
  20. The key's randomart image is:
  21. +---[RSA 2048]----+
  22. |+.....oo= . |
  23. |.+.o.o B.+.. |
  24. |o + *o=o. |
  25. |o..... ++ |
  26. |.o * + oS. |
  27. | = B B . |
  28. |.o = E o |
  29. |o . = o |
  30. | . |
  31. +----[SHA256]-----+
  32. [root@test ~]# ssh-copy-id 192.168.0.99 -p 41319
  33. /bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
  34. The authenticity of host '[192.168.0.99]:41319 ([192.168.0.99]:41319)' can't be established.
  35. ECDSA key fingerprint is SHA256:W2pD2PA2K9tGKGVK+weiINcVESkUaHjsTI263OVqBh4.
  36. ECDSA key fingerprint is MD5:3a:f8:c9:b1:63:c6:c1:ae:e0:6e:e2:ca:17:4a:20:7a.
  37. Are you sure you want to continue connecting (yes/no)? yes
  38. /bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  39. /bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
  40. root@192.168.0.99's password:
  41.  
  42. Number of key(s) added: 1
  43.  
  44. Now try logging into the machine, with: "ssh -p '41319' '192.168.0.99'"
  45. and check to make sure that only the key(s) you wanted were added.
  46.  
  47. [root@test ~]# scp -r .ssh 192.168.0.10:~/
  48. The authenticity of host '192.168.0.10 (192.168.0.10)' can't be established.
  49. ECDSA key fingerprint is SHA256:EG9nua4JJuUeofheXlgQeL9hX5H53JynOqf2vf53mII.
  50. ECDSA key fingerprint is MD5:57:83:e6:46:2c:4b:bb:33:13:56:17:f7:fd:76:71:cc.
  51. Are you sure you want to continue connecting (yes/no)? yes
  52. Warning: Permanently added '192.168.0.10' (ECDSA) to the list of known hosts.
  53. root@192.168.0.10's password:
  54. id_rsa 100% 1675 677.0KB/s 00:00
  55. id_rsa.pub 100% 391 207.6KB/s 00:00
  56. known_hosts 100% 356 12.2KB/s 00:00
  57. authorized_keys 100% 391 12.6KB/s 00:00
  58. [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认证的详细说明请参考本人博客https://www.jb51.net/article/180381.htm

  测试:用ansible主机通过ssh远程客户端主机


 
  1. [root@test ~]# ssh 192.168.0.10
  2. Last login: Mon Jan 27 04:58:46 2020 from 192.168.0.99
  3. [root@test-centos7-node1 ~]# ip a
  4. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
  5. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  6. inet 127.0.0.1/8 scope host lo
  7. valid_lft forever preferred_lft forever
  8. inet6 ::1/128 scope host
  9. valid_lft forever preferred_lft forever
  10. 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  11. link/ether 00:0c:29:f2:82:0c brd ff:ff:ff:ff:ff:ff
  12. inet 192.168.0.10/24 brd 192.168.0.255 scope global ens33
  13. valid_lft forever preferred_lft forever
  14. inet6 fe80::20c:29ff:fef2:820c/64 scope link
  15. valid_lft forever preferred_lft forever
  16. [root@test-centos7-node1 ~]# exit
  17. 登出
  18. Connection to 192.168.0.10 closed.
  19. [root@test ~]#

  提示:可以看到ansible主机能够正常免密登录远端主机,接下我们在用ansible的ping模块去探测下被管控主机的存活

  提示:能够看到用ansible的ping模块去探测远端主机的存活,返回的状态是SUCCESS,数据是pong说明对端主机上存活的

到此ansible的环境就准备好了,接下来写playbook来安装httpd


 
  1. [root@test ~]# cat install_httpd.yml
  2. ---
  3. - hosts: websers
  4. remote_user: root
  5.  
  6. tasks:
  7. - name: copy epel file
  8. copy: src=/etc/yum.repos.d/CentOS-Base.repo dest=/etc/yum.repos.d/CentOS-Base.repo
  9. - name: install httpd
  10. yum: name=httpd
  11. - name: start httpd
  12. service: name=httpd state=started enabled=yes
  13.  
  14.  
  15. [root@test ~]# ansible-playbook -C install_httpd.yml
  16.  
  17. PLAY [websers]***********************************************************************************************************
  18.  
  19. TASK [copy epel file]****************************************************************************************************
  20. changed: [192.168.0.10]
  21.  
  22. TASK [install httpd]*****************************************************************************************************
  23. changed: [192.168.0.10]
  24.  
  25. TASK [start httpd]*******************************************************************************************************
  26. changed: [192.168.0.10]
  27.  
  28. PLAY RECAP ***************************************************************************************************************
  29. 192.168.0.10 : ok=3 changed=3 unreachable=0 failed=0
  30.  
  31. [root@test ~]#

  提示:以上playbook的主要内容是把本机的yum源复制到远端服务器上,然后通过yum去安装httpd包,最后启动httpd;在写好playbook后,我们用ansible-playbook -C install_httpd.yml 命令对我们写的playbook进行了测试,没有问题,接下来我们使用ansible-playbook来安装httpd


 
  1. [root@test ~]# ansible-playbook install_httpd.yml
  2.  
  3. PLAY [websers]***********************************************************************************************************
  4.  
  5. TASK [copy epel file]****************************************************************************************************
  6. changed: [192.168.0.10]
  7.  
  8. TASK [install httpd]*****************************************************************************************************
  9. changed: [192.168.0.10]
  10.  
  11. TASK [start httpd]*******************************************************************************************************
  12. changed: [192.168.0.10]
  13.  
  14. PLAY RECAP ***************************************************************************************************************
  15. 192.168.0.10 : ok=3 changed=3 unreachable=0 failed=0
  16.  
  17. [root@test ~]#

  提示:从ansible-playbook 对playbook的执行状态来看是成功了,接下来我们直接使用浏览器来访问192.168.0.10,看看httpd是否已经能够正常访问,如果能正常访问说明httpd已经安装好了

Linux系统实现ansible自动化安装配置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的配置文件


 
  1. [root@test ~]# cat x_com.conf
  2. <VirtualHost *:80>
  3. ServerName www.X.com
  4. DocumentRoot "/web/vhosts/x"
  5. <Directory "/web/vhosts/x">
  6. Options None
  7. AllowOverride None
  8. Require all granted
  9. </Directory>
  10. ErrorLog "logs/x.err"
  11. CustomLog "logs/x.access" combined
  12. </VirtualHost>
  13. [root@test ~]#

  提示:我们在ansible主机上把配置文件建立好,待会直接用ansible把文件推送到对应主机的对应目录下即可使用

(2)www.Y.com,页面文件目录为/web/vhosts/y;错误日志为/var/log/httpd/www2.err,访问日志为/var/log/httpd/y.access

  新建虚拟主机www.Y.com的配置文件


 
  1. [root@test ~]# cat y_com.conf
  2. <VirtualHost *:80>
  3. ServerName www.Y.com
  4. DocumentRoot "/web/vhosts/y"
  5. <Directory "/web/vhosts/y">
  6. Options None
  7. AllowOverride None
  8. Require all granted
  9. </Directory>
  10. ErrorLog "logs/www2.err"
  11. CustomLog "logs/y.access" combined
  12. </VirtualHost>
  13. [root@test ~]#

 

(3)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名


 
  1. [root@test ~]# cat x_index.html
  2. <h1> www.X.com</h1>
  3. [root@test ~]# cat y_index.html
  4. <h1> www.Y.com</h1>
  5. [root@test ~]#

  提示:以上文件在ansible主机上准备好了以后,我们接下来写一个playbook把对应的文件直接推送到远端主机即可

 


 
  1. [root@test ~]# cat set_virtualhost_conf_file.yml
  2. ---
  3. - hosts: websers
  4. remote_user: root
  5.  
  6. tasks:
  7. - name: mkdir virtualhost documentroot directory
  8. shell: mkdir -p /web/vhosts/{x,y}
  9.  
  10. - name: copy x_com.conf to remotehost
  11. copy: src=/root/x_com.conf dest=/etc/httpd/conf.d/x_com.conf
  12. - name: copy x_com index file
  13. copy: src=/root/x_index.html dest=/web/vhosts/x/index.html
  14.  
  15. - name: copy y_com.conf to remotehost
  16. copy: src=/root/y_com.conf dest=/etc/httpd/conf.d/y_com.conf
  17. - name: copy y_com index file
  18. copy: src=/root/y_index.html dest=/web/vhosts/y/index.html
  19.  
  20. [root@test ~]#

 提示:以上文件的内容主要把我们建立好的配置文件推送到对应主机的对应目录,接下来我们来检查下我们写的playbook是否语法问题


 
  1. [root@test ~]# ansible-playbook -C set_virtualhost_conf_file.yml
  2.  
  3. PLAY [websers]***********************************************************************************************************
  4.  
  5. TASK [mkdir virtualhost documentroot directory]**************************************************************************
  6. skipping: [192.168.0.10]
  7.  
  8. TASK [copy x_com.conf to remotehost]*************************************************************************************
  9. changed: [192.168.0.10]
  10.  
  11. TASK [copy x_com index file]*********************************************************************************************
  12. changed: [192.168.0.10]
  13.  
  14. TASK [copy y_com.conf to remotehost]*************************************************************************************
  15. changed: [192.168.0.10]
  16.  
  17. TASK [copy y_com index file]*********************************************************************************************
  18. changed: [192.168.0.10]
  19.  
  20. PLAY RECAP ***************************************************************************************************************
  21. 192.168.0.10 : ok=4 changed=4 unreachable=0 failed=0
  22.  
  23. [root@test ~]#

  提示:测试playbook没有问题,接下来我们来运行playbook,把对应的文件推送到httpd服务器上,然后在服务器上检查我们写的配置文件是否正确


 
  1. [root@test ~]# ansible-playbook set_virtualhost_conf_file.yml
  2.  
  3. PLAY [websers]***********************************************************************************************************
  4.  
  5. TASK [mkdir virtualhost documentroot directory]**************************************************************************
  6. [WARNING]: Consider using file module with state=directory rather than running mkdir
  7.  
  8. changed: [192.168.0.10]
  9.  
  10. TASK [copy x_com.conf to remotehost]*************************************************************************************
  11. changed: [192.168.0.10]
  12.  
  13. TASK [copy x_com index file]*********************************************************************************************
  14. changed: [192.168.0.10]
  15.  
  16. TASK [copy y_com.conf to remotehost]*************************************************************************************
  17. changed: [192.168.0.10]
  18.  
  19. TASK [copy y_com index file]*********************************************************************************************
  20. changed: [192.168.0.10]
  21.  
  22. PLAY RECAP ***************************************************************************************************************
  23. 192.168.0.10 : ok=5 changed=5 unreachable=0 failed=0
  24.  
  25. [root@test ~]#

  提示:从上面的返回状态看,都是成功的,只是第一个任务提示我们使用file模块去创建目录会更好一些,接下来我们使用ansible的shell模块去检查远端服务器上的配置文件的语法是否正确


 
  1. [root@test ~]# ansible websers -m shell -a 'httpd -t'
  2. 192.168.0.10 | SUCCESS | rc=0 >>
  3. 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
  4. Syntax OK
  5.  
  6. [root@test ~]#

  提示:我们利用ansible在远程主机上检查httpd的配置文件语法是没有问题,它提示我们没有servername,这个提示可以不用管它,如果你觉得非要处理一下也可以,在httpd的主配置文件中找到ServerName 把对应的servername配置上就好了;接下来我们重启httpd,然后在客户机上访问两个虚拟主机


 
  1. [root@test ~]# ansible websers -m shell -a 'systemctl restart httpd'
  2. 192.168.0.10 | SUCCESS | rc=0 >>
  3.  
  4.  
  5. [root@test ~]#

  在客户端上更改/etc/hosts文件,让其访问www.X.com 和www.Y.com 能够解析到远程主机


 
  1. [root@test ~]# cat /etc/hosts
  2. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  3. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  4. 192.168.0.10 www.X.com www.Y.com
  5. [root@test ~]#

  测试:利用curl 分别访问两个虚拟主机,看看对应的主页文件内容是否不同

 


 
  1. [root@test ~]# curl http://www.X.com/index.html
  2. <h1> www.X.com</h1>
  3. [root@test ~]# curl http://www.Y.com/index.html
  4. <h1> www.Y.com</h1>
  5. [root@test ~]#

 提示:我们利用curl对其各自虚拟主机的主页文件访问,都能看到对应的主页文件内容,当然你也可以更改Windows的hosts文件,把对应的解析内容写进去,然后通过Windows浏览器也是可以的,如下所示:

Linux系统实现ansible自动化安装配置httpd的方法

  提示:在windows上找到hosts文件,然后把对应的记录添加进去,如下

Linux系统实现ansible自动化安装配置httpd的方法

  提示:接下来我们利用Windows的浏览器来访问下两个虚拟主机

Linux系统实现ansible自动化安装配置httpd的方法

Linux系统实现ansible自动化安装配置httpd的方法

  接下我们在看看httpd服务器的日志是否都已生成


 
  1. [root@test ~]# ansible websers -m shell -a 'ls -l /var/log/httpd'
  2. 192.168.0.10 | SUCCESS | rc=0 >>
  3. 总用量 16
  4. -rw-r--r--. 1 root root 2668 1月 27 06:30 access_log
  5. -rw-r--r--. 1 root root 2940 1月 27 07:34 error_log
  6. -rw-r--r--. 1 root root 0 1月 27 07:38 www2.err
  7. -rw-r--r--. 1 root root 500 1月 27 07:52 x.access
  8. -rw-r--r--. 1 root root 0 1月 27 07:38 x.err
  9. -rw-r--r--. 1 root root 500 1月 27 07:52 y.access
  10.  
  11. [root@test ~]# ansible websers -m shell -a 'cat /var/log/httpd/x.access'
  12. 192.168.0.10 | SUCCESS | rc=0 >>
  13. 192.168.0.99 - - [27/Jan/2020:07:39:15 -0500] "GET /index.html HTTP/1.1" 200 20 "-""curl/7.29.0"
  14. 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"
  15. 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"
  16.  
  17. [root@test ~]# ansible websers -m shell -a 'cat /var/log/httpd/y.access'
  18. 192.168.0.10 | SUCCESS | rc=0 >>
  19. 192.168.0.99 - - [27/Jan/2020:07:39:19 -0500] "GET /index.html HTTP/1.1" 200 20 "-""curl/7.29.0"
  20. 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"
  21. 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"
  22.  
  23. [root@test ~]#

  提示:可以看到httpd服务器上都已经生成了对应名称的日志文件。

总结

以上所述是小编给大家介绍的Linux系统实现ansible自动化安装配置httpd的方法,希望对大家有所帮助!

分享到:

相关信息

  • linux定时关机设置教程

    当linux在运作时不能直接关闭电源容易将档案系统损毁,因此需要用shutdown以安全的方式关闭,那么这个操作该怎么实现呢?下面就为大家带来了详细教程。...

    2022-11-07

  • linux强制删除文件教程

    由于linux系统和我们常用的windows系统是不一样的,所以如果是初学者,可能会不知道linux怎么强制删除文件,其实我们只要打开终端,使用命令就可以删除了。...

    2022-11-03

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载