ansible和apache練習(xí)

使用ansible的playbook實(shí)現(xiàn)自動(dòng)化安裝httpd

實(shí)驗(yàn)環(huán)境:

主機(jī) os 軟件 ip
ansible節(jié)點(diǎn) centos7.6 安裝ansible 172.16.2.132
ansible-host節(jié)點(diǎn)1 centos7.6 172.16.2.137
ansible-host節(jié)點(diǎn)2 centos7.6 172.16.2.138

*在所有節(jié)點(diǎn)實(shí)現(xiàn)相互之間ssh key驗(yàn)證

實(shí)驗(yàn)設(shè)想

通過ansible 自動(dòng)部署httpd服務(wù),達(dá)到以下效果:

  • ansible-host節(jié)點(diǎn)1:172.16.2.137

建立httpd服務(wù)器,要求提供兩個(gè)基于名稱的虛擬主機(jī):
(1)www.X.com,頁面文件目錄為/web/vhosts/x;錯(cuò)誤日志為/var/log/httpd/x.err,訪問日志為/var/log/httpd/x.access
(2)www.Y.com,頁面文件目錄為/web/vhosts/y;錯(cuò)誤日志為 /var/log/httpd/www2.err,訪問日志為/var/log/httpd/y.access
(3)為兩個(gè)虛擬主機(jī)建立各自的主頁文件index.html,內(nèi)容分別為其對(duì)應(yīng)的主機(jī)名

  • ansible-host節(jié)點(diǎn)2:172.16.2.138

建立httpd服務(wù)器,要求提供兩個(gè)基于名稱的虛擬主機(jī):
(1)www.a.com,頁面文件目錄為/web/vhosts/a;錯(cuò)誤日志為/var/log/httpd/a.err,訪問日志為/var/log/httpd/a.access
(2)www.b.com,頁面文件目錄為/web/vhosts/b;錯(cuò)誤日志為 /var/log/httpd/bwww2.err,訪問日志為/var/log/httpd/b.access
(3)為兩個(gè)虛擬主機(jī)建立各自的主頁文件index.html,內(nèi)容分別為其對(duì)應(yīng)的主機(jī)名

實(shí)現(xiàn)步驟

1.使用epel源 安裝ansible

[root@node2 ~]# yum install ansible -y

2.修改主機(jī)hosts文件

vim /etc/ansible/hosts
[httpdservers]
172.16.2.137
172.16.2.138

3.基于role創(chuàng)建如下目錄及文件

[root@node2 playbook]# tree
.
├── host_vars
│   ├── 172.16.2.137
│   └── 172.16.2.138
├── http_role.yml
└── roles
    └── httpd
        ├── files
        ├── handlers
        │   ├── main.yml
        │   └── restart.yml
        ├── tasks
        │   ├── config.yml
        │   ├── index1.yml
        │   ├── index2.yml
        │   ├── install.yml
        │   ├── main.yml
        │   ├── service.yml
        │   └── webdir.yml
        ├── templates
        │   ├── index1.html.j2
        │   ├── index2.html.j2
        │   └── test.conf.j2
        └── vars

4.準(zhǔn)備需要的templates文件:

配置httpd模板

[root@node2 templates]# vim test.conf.j2
<virtualhost *:80>
documentroot /web/vhosts/{{ webdir1 }}  //配置文件個(gè)性化之處使用變量處理
servername {{ vhost1 }}
<Directory "/web/vhosts/{{ webdir1 }}">
    Require all granted
</Directory>
CustomLog "/var/log/httpd/{{ accesslog1 }}" combined
ErrorLog "/var/log/httpd/{{ errorlog1 }}"
</virtualhost>

<virtualhost *:80>
documentroot /web/vhosts/{{ webdir2 }}
servername {{ vhost2 }}
<Directory "/web/vhosts/{{ webdir2 }}">
    Require all granted
</Directory>
CustomLog "/var/log/httpd/{{ accesslog2 }}" combined
ErrorLog "/var/log/httpd/{{ errorlog2 }}"
</virtualhost>

配置index.html模板

vim index1.html.j2
{{ vhost1 }}

vim index2.html.j2
{{ vhost2 }}

5.編輯task

[root@node2 tasks]# vim install.yml
- name: install    //安裝服務(wù)
 yum: name=httpd
[root@node2 tasks]# vim config.yml
- name: config    //復(fù)制conf到目標(biāo)主機(jī)
 template: src=test.conf.j2 dest=/etc/httpd/conf.d/test.conf
 notify: restart  //關(guān)聯(lián)handles
[root@node2 tasks]# vim webdir.yml
- name: webdir1   //創(chuàng)建網(wǎng)站目錄
 file: path=//web/vhosts/{{webdir1}} state=directory
- name: webdir2
 file: path=//web/vhosts/{{webdir2}} state=directory
[root@node2 tasks]# vim index1.yml
- name: index1    //復(fù)制index文件
 template: src=index1.html.j2 dest=/web/vhosts/{{webdir1}}/index.html
[root@node2 tasks]# vim index2.yml
- name: index2
 template: src=index2.html.j2 dest=/web/vhosts/{{webdir2}}/index.html
[root@node2 tasks]# vim service.yml
- name: service   //啟動(dòng)服務(wù)
 service: name=httpd state=started enabled=yes
 
[root@node2 tasks]# vim main.yml  
- include: install.yml
- include: config.yml
- include: webdir.yml
- include: index1.yml
- include: index2.yml
- include: service.yml

6.編輯handles

[root@node2 handlers]# vim restart.yml
- name: restart
  service: name=httpd state=restarted
  
[root@node2 handlers]# vim main.yml
- include: restart.yml

7.編輯基于各主機(jī)host_vars

*注意host_vars目錄的位置

[root@node2 host_vars]# vim 172.16.2.137
webdir1: x
webdir2: y
errorlog1: "e.err"
errorlog2: "www2.err"
accesslog1: "x.access"
accesslog2: "y.access"
vhost1: "www.X.com"
vhost2: "www.Y.com"

[root@node2 host_vars]# vim 172.16.2.138
webdir1: a
webdir2: b
errorlog1: "ae.err"
errorlog2: "bwww2.err"
accesslog1: "a.access"
accesslog2: "b.access"
vhost1: "www.A.com"
vhost2: "www.B.com"

8.編輯基于role的yml

[root@node2 playbook]# cat http_role.yml
- hosts: httpdservers

  roles:
    - httpd

9.檢查配置

[root@node2 playbook]# ansible-playbook -C  http_role.yml

PLAY [httpdservers] *****************************************************************************************

TASK [Gathering Facts] **************************************************************************************
ok: [172.16.2.137]
ok: [172.16.2.138]

TASK [httpd : install] **************************************************************************************
changed: [172.16.2.137]
changed: [172.16.2.138]

TASK [httpd : config] ***************************************************************************************
changed: [172.16.2.137]
changed: [172.16.2.138]

TASK [httpd : webdir1] **************************************************************************************
changed: [172.16.2.137]
changed: [172.16.2.138]

TASK [httpd : webdir2] **************************************************************************************
changed: [172.16.2.137]
changed: [172.16.2.138]

TASK [httpd : index1] ***************************************************************************************
changed: [172.16.2.137]
changed: [172.16.2.138]

TASK [httpd : index2] ***************************************************************************************
changed: [172.16.2.137]
changed: [172.16.2.138]

TASK [httpd : service] **************************************************************************************
changed: [172.16.2.138]
changed: [172.16.2.137]

RUNNING HANDLER [httpd : restart] ***************************************************************************
changed: [172.16.2.137]
changed: [172.16.2.138]

PLAY RECAP **************************************************************************************************
172.16.2.137               : ok=9    changed=8    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
172.16.2.138               : ok=9    changed=8    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

10.正式部署

[root@node2 playbook]# ansible-playbook  http_role.yml

11.驗(yàn)證

[root@node2 templates]# cat /etc/hosts  //查看管理主機(jī)的hosts文件
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.2.137    www.X.com www.Y.com   //同一個(gè)ip對(duì)應(yīng)兩個(gè)不同的虛擬主機(jī)地址
172.16.2.138    www.a.com www.b.com


[root@node2 templates]# curl www.X.com  使用curl訪問
www.X.com
[root@node2 templates]# curl www.Y.com
www.Y.com
[root@node2 templates]# curl www.a.com
www.A.com
[root@node2 templates]# curl www.b.com
www.B.com

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

實(shí)驗(yàn)環(huán)境:

服務(wù)器端: centos7.6 ip:172.16.2.137 安裝httpd
客戶端: centos7.6 ip:172.16.2.132 修改hosts文件增加兩個(gè)虛擬主機(jī)ip

實(shí)驗(yàn)設(shè)想

建立httpd服務(wù)器,要求提供兩個(gè)基于名稱的虛擬主機(jī):
(1)www.X.com,頁面文件目錄為/web/vhosts/x;錯(cuò)誤日志為/var/log/httpd/x.err,訪問日志為/var/log/httpd/x.access
(2)www.Y.com,頁面文件目錄為/web/vhosts/y;錯(cuò)誤日志為 /var/log/httpd/www2.err,訪問日志為/var/log/httpd/y.access
(3)為兩個(gè)虛擬主機(jī)建立各自的主頁文件index.html,內(nèi)容分別為其對(duì)應(yīng)的主機(jī)名

實(shí)驗(yàn)步驟

1.安裝服務(wù)

 yum install -y httpd

2.編譯配置文檔

[root@node3 etc]# vim /etc/httpd/conf.d/test.conf
<virtualhost *:80>
documentroot /web/vhosts/x   //網(wǎng)站主目錄
servername www.X.com        //設(shè)置域名
<Directory "/web/vhosts/x">
    Require all granted     //權(quán)限
</Directory>
CustomLog "/var/log/httpd/x.access" combined  //設(shè)置log
ErrorLog "/var/log/httpd/e.err"
</virtualhost>

<virtualhost *:80>
documentroot /web/vhosts/y
servername www.Y.com
<Directory "/web/vhosts/y">
    Require all granted
</Directory>
CustomLog "/var/log/httpd/y.access" combined
ErrorLog "/var/log/httpd/www2.err"
</virtualhost>

3.創(chuàng)建目錄:

[root@node3 etc]# mkdir /web/vhosts/{x,y}

4.創(chuàng)建主頁文件:

[root@node3 etc]# cat /web/vhosts/x/index.html
www.X.com
[root@node3 etc]# cat /web/vhosts/y/index.html
www.Y.com

5.啟動(dòng)服務(wù):

systemctl start httpd

6.驗(yàn)證:

[root@node2 templates]# cat /etc/hosts //查看客戶端host是文件
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.2.137    www.X.com www.Y.com  //同一個(gè)ip對(duì)應(yīng)兩個(gè)不同的虛擬主機(jī)地址
[root@node2 templates]# curl www.X.com
www.X.com   
[root@node2 templates]# curl www.Y.com
www.Y.com   
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容