欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Linux系統(tǒng)實現(xiàn)ansible自動化安裝配置httpd的方法

 更新時間:2020年02月14日 08:45:46   作者:1874  
這篇文章主要介紹了Linux系統(tǒng)實現(xiàn)ansible自動化安裝配置httpd的方法,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下

1、使用ansible的playbook實現(xiàn)自動化安裝httpd

  1)首先配置好ansible的hosts文件,讓其對應(yīng)主機(jī)能夠受ansible控制

  提示:我們在主機(jī)清單上配置了所管控的主機(jī)地址,但是直接用ansible的ping模塊去探測主機(jī)的存活情況,卻顯示權(quán)限拒絕。從提示上說讓我們要指定用什么驗證。默認(rèn)情況ansible是通過ssh的key驗證的,所以我們在ansible的主機(jī)清單中配置了管控主機(jī)的ip是不夠的,還要配置ssh基于KEY驗證

  2)配置管控主機(jī)能夠基于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主機(jī)上做,我上面是現(xiàn)在ansible主機(jī)上生成一對ssh密鑰,然后通過ssh-copy-id 把公鑰復(fù)制給本機(jī)生成authorized_keys文件,然后在把.ssh目錄復(fù)制給遠(yuǎn)端客戶機(jī),這樣一來ansible主機(jī)可以通過ssh基于key免密登錄遠(yuǎn)端客戶機(jī),同時遠(yuǎn)端客戶機(jī)也可以通過ssh免密登錄ansible主機(jī),這樣就實現(xiàn)了雙向的ssh基于key驗證,如果你只想單向的通過ssh基于key認(rèn)證,你可以在ansible主機(jī)上生成密鑰對,然后把公鑰發(fā)給對方即可。有關(guān)ssh基于key認(rèn)證的詳細(xì)說明請參考本人博客http://www.dbjr.com.cn/article/180381.htm

  測試:用ansible主機(jī)通過ssh遠(yuǎn)程客戶端主機(jī)

[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主機(jī)能夠正常免密登錄遠(yuǎn)端主機(jī),接下我們在用ansible的ping模塊去探測下被管控主機(jī)的存活

  提示:能夠看到用ansible的ping模塊去探測遠(yuǎn)端主機(jī)的存活,返回的狀態(tài)是SUCCESS,數(shù)據(jù)是pong說明對端主機(jī)上存活的

到此ansible的環(huán)境就準(zhǔn)備好了,接下來寫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的主要內(nèi)容是把本機(jī)的yum源復(fù)制到遠(yuǎn)端服務(wù)器上,然后通過yum去安裝httpd包,最后啟動httpd;在寫好playbook后,我們用ansible-playbook -C install_httpd.yml 命令對我們寫的playbook進(jìn)行了測試,沒有問題,接下來我們使用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的執(zhí)行狀態(tài)來看是成功了,接下來我們直接使用瀏覽器來訪問192.168.0.10,看看httpd是否已經(jīng)能夠正常訪問,如果能正常訪問說明httpd已經(jīng)安裝好了

  提示:可以看到我們用瀏覽器是直接可以訪問到192.168.0.10的測試頁面,說明httpd在192.168.0.10上安裝成功

2、建立httpd服務(wù)器,要求提供兩個基于名稱的虛擬主機(jī):

(1)www.X.com,頁面文件目錄為/web/vhosts/x;錯誤日志為/var/log/httpd/x.err,訪問日志為/var/log/httpd/x.access

  新建虛擬主機(jī)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主機(jī)上把配置文件建立好,待會直接用ansible把文件推送到對應(yīng)主機(jī)的對應(yīng)目錄下即可使用

(2)www.Y.com,頁面文件目錄為/web/vhosts/y;錯誤日志為/var/log/httpd/www2.err,訪問日志為/var/log/httpd/y.access

  新建虛擬主機(jī)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)為兩個虛擬主機(jī)建立各自的主頁文件index.html,內(nèi)容分別為其對應(yīng)的主機(jī)名

[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主機(jī)上準(zhǔn)備好了以后,我們接下來寫一個playbook把對應(yīng)的文件直接推送到遠(yuǎn)端主機(jī)即可

 

[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 ~]#

 提示:以上文件的內(nèi)容主要把我們建立好的配置文件推送到對應(yīng)主機(jī)的對應(yīng)目錄,接下來我們來檢查下我們寫的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沒有問題,接下來我們來運(yùn)行playbook,把對應(yīng)的文件推送到httpd服務(wù)器上,然后在服務(wù)器上檢查我們寫的配置文件是否正確

[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 ~]#

  提示:從上面的返回狀態(tài)看,都是成功的,只是第一個任務(wù)提示我們使用file模塊去創(chuàng)建目錄會更好一些,接下來我們使用ansible的shell模塊去檢查遠(yuǎn)端服務(wù)器上的配置文件的語法是否正確

[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在遠(yuǎn)程主機(jī)上檢查httpd的配置文件語法是沒有問題,它提示我們沒有servername,這個提示可以不用管它,如果你覺得非要處理一下也可以,在httpd的主配置文件中找到ServerName 把對應(yīng)的servername配置上就好了;接下來我們重啟httpd,然后在客戶機(jī)上訪問兩個虛擬主機(jī)

[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 能夠解析到遠(yuǎn)程主機(jī)

[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 分別訪問兩個虛擬主機(jī),看看對應(yīng)的主頁文件內(nèi)容是否不同

 

[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對其各自虛擬主機(jī)的主頁文件訪問,都能看到對應(yīng)的主頁文件內(nèi)容,當(dāng)然你也可以更改Windows的hosts文件,把對應(yīng)的解析內(nèi)容寫進(jìn)去,然后通過Windows瀏覽器也是可以的,如下所示:

  提示:在windows上找到hosts文件,然后把對應(yīng)的記錄添加進(jìn)去,如下

  提示:接下來我們利用Windows的瀏覽器來訪問下兩個虛擬主機(jī)

  接下我們在看看httpd服務(wù)器的日志是否都已生成

[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服務(wù)器上都已經(jīng)生成了對應(yīng)名稱的日志文件。

總結(jié)

以上所述是小編給大家介紹的Linux系統(tǒng)實現(xiàn)ansible自動化安裝配置httpd的方法,希望對大家有所幫助!

相關(guān)文章

  • Linux 常用命令操作大全(推薦收藏)

    Linux 常用命令操作大全(推薦收藏)

    這篇文章主要介紹了Linux 常用命令操作大全,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Linux服務(wù)器如何查看每個用戶或者當(dāng)前用戶的磁盤占用量及文件同步

    Linux服務(wù)器如何查看每個用戶或者當(dāng)前用戶的磁盤占用量及文件同步

    這篇文章主要介紹了Linux服務(wù)器如何查看每個用戶或者當(dāng)前用戶的磁盤占用量及文件同步問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • linux jexus服務(wù)設(shè)置開機(jī)啟動

    linux jexus服務(wù)設(shè)置開機(jī)啟動

    這篇文章主要為大家詳細(xì)介紹了linux jexus服務(wù)設(shè)置開機(jī)啟動,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Linux下安裝Keepalived及原理分析

    Linux下安裝Keepalived及原理分析

    這篇文章主要介紹了在Linux環(huán)境下安裝Keepalived及原理分析,有興趣的朋友學(xué)習(xí)參考下吧。
    2017-12-12
  • Linux系統(tǒng)中jdk環(huán)境配置方式

    Linux系統(tǒng)中jdk環(huán)境配置方式

    這篇文章主要介紹了Linux系統(tǒng)中jdk環(huán)境配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Linux 進(jìn)程通信之FIFO的實現(xiàn)

    Linux 進(jìn)程通信之FIFO的實現(xiàn)

    這篇文章主要介紹了Linux 進(jìn)程通信之FIFO的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • Ubuntu環(huán)境下SSH的安裝及使用詳解

    Ubuntu環(huán)境下SSH的安裝及使用詳解

    這篇文章主要介紹了Ubuntu環(huán)境下SSH的安裝及使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-02-02
  • Linux下批量修改服務(wù)器用戶密碼方法步驟

    Linux下批量修改服務(wù)器用戶密碼方法步驟

    本文給大家介紹Linux下批量修改服務(wù)器用戶密碼方法步驟,批量修改服務(wù)器用戶密碼,由于機(jī)器數(shù)量比較多,因此采用批量修改密碼的方式進(jìn)行處理比較快,具體操作步驟大家通過本文學(xué)習(xí)吧
    2018-03-03
  • Ubuntu環(huán)境安裝Anaconda3完整步驟

    Ubuntu環(huán)境安裝Anaconda3完整步驟

    大家好,本篇文章主要講的是Ubuntu環(huán)境安裝Anaconda3完整步驟,感興趣的同學(xué)快來看一看吧,對你有幫助的話記得收藏一下哦
    2021-11-11
  • Apache安全配置之禁止目錄訪問的配置方法

    Apache安全配置之禁止目錄訪問的配置方法

    這篇文章主要介紹了Apache禁止目錄訪問的配置方法,這是一個常見的、非常有必要的apache安全配置之一,需要的朋友可以參考下
    2014-05-05

最新評論