ansible鑷姩鍖栧畨瑁呭強閮ㄧ讲httpd

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ansible鑷姩鍖栧畨瑁呭強閮ㄧ讲httpd相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/rest' title='rest'>rest   杩愯   apr   any   mit   sync   鍛藉悕   0ms   false   

瀹為獙鐜锛氾紙寮€濮嬪疄楠屼箣鍓嶏紝璇蜂繚璇侀槻鐏鍏抽棴锛宻elinux鍏抽棴锛屾椂闂村悓姝ワ級
ansible锛?92.168.2.7 web1锛?92.168.2.7 web2锛?92.168.2.17
OS锛歝entos7 OS锛歝entos7 OS锛歝entos7

瀹炵幇鍔熻兘锛氾紙鑷姩鍖栭儴缃蹭袱鍙板悇鑷富鏈哄懡鍚嶇殑httpd鏈嶅姟鍣級
1銆佷娇鐢╝nsible鐨刾laybook瀹炵幇鑷姩鍖栧畨瑁卙ttpd
2銆佸缓绔媓ttpd鏈嶅姟鍣紝瑕佹眰鎻愪緵鍚勮嚜鍩轰簬涓绘満鍚嶇殑铏氭嫙涓绘満:
(1)www.x.com锛岄〉闈㈡枃浠剁洰褰曚负/web/vhosts;閿欒鏃ュ織涓?br/>/var/log/httpd/x.err锛岃闂棩蹇椾负/var/log/httpd/x.access
(2)www.y.com锛岄〉闈㈡枃浠剁洰褰曚负/web/vhosts;閿欒鏃ュ織涓?/var/log/httpd/y.err锛岃闂棩蹇椾负/var/log/httpd/y.access
(3)涓轰袱涓櫄鎷熶富鏈哄缓绔嬪悇鑷殑涓婚〉鏂囦欢/web/vhosts/index.html锛屽唴瀹瑰垎鍒负鍏跺搴旂殑涓绘満鍚?/p>

娉ㄦ剰锛氳剼鏈槸婧愮爜缂栬瘧瀹夎鐨刪ttpd2.4

ansible瀹夎
閰嶇疆epel婧愶紝yum瀹夎ansible鍗冲彲

[root@ansible ~]$cat /etc/yum.repos.d/epel.repo 
[epel]
name=aliyun epel
baseurl=http://mirrors.aliyun.com/epel/$releasever/$basearch
enabled=1
gpgcheck=0

[root@ansible ~]$yum -y install ansible
....

鍥犱负瑕侀儴缃蹭袱鍙颁富鏈猴紝涓攁nsible鏄緷璧杝sh瀵嗛挜璁よ瘉锛屾墍浠ユ帴涓嬫潵鎿嶄綔ansible鍏嶅瘑鐮佺櫥闄唚eb1鍜寃eb2锛屽悓鏃朵篃闇€瑕佹洿鏀?etc/hosts鏂囦欢

[root@ansible ~]$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:o+OlbC5xbEnPKc3MnhP++eHX78+oN9RPhQTJoihxyUg root@ansible
The key鈥榮 randomart image is:
+---[RSA 2048]----+
|   .Eo .   ..o   |
|    o +   . o .  |
|     o . . . . . |
|    . o .     . .|
|     + OS.     ..|
|    . *.X.    . o|
|     +o+.o  .. o.|
|    .o.+=  o .+oo|
|     +=  oo.++.oB|
+----[SHA256]-----+
[root@ansible ~]$ssh-copy-id 
anaconda-ks.cfg  .bash_history    .bash_logout     .bash_profile    .bashrc          .cshrc           original-ks.cfg  .ssh/            .tcshrc          .viminfo
[root@ansible ~]$ssh-copy-id 192.168.2.17
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 鈥?92.168.2.17 (192.168.2.17)鈥?can鈥榯 be established.
ECDSA key fingerprint is SHA256:bzwVyjsq4E6PnawN+cXFHl3yIXEgmN64I2+pwUKKKkU.
ECDSA key fingerprint is MD5:1d:d8:5a:07:13:a4:5e:dc:a7:69:7c:79:95:69:fe:67.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/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.2.17鈥榮 password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 鈥?92.168.2.17鈥?
and check to make sure that only the key(s) you wanted were added.

[root@ansible ~]$ssh-copy-id 192.168.2.27
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 鈥?92.168.2.27 (192.168.2.27)鈥?can鈥榯 be established.
ECDSA key fingerprint is SHA256:ZWtO06H5nDqNbg12inajd66G9wInPDgEg7OQpiLvSME.
ECDSA key fingerprint is MD5:31:d1:a6:74:bf:e3:d2:09:8b:29:94:f1:53:1f:8c:06.
Are you sure you want to continue connecting (yes/no)? y
Please type 鈥榶es鈥?or 鈥榥o鈥? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/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.2.27鈥榮 password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 鈥?92.168.2.27鈥?
and check to make sure that only the key(s) you wanted were added.

[root@ansible ~]$ssh 192.168.2.17
Last login: Sun Nov 22 12:27:57 2020 from afocl-605021336.lan
[root@web1 ~]$exit
logout
Connection to 192.168.2.17 closed.
[root@ansible ~]$ssh 192.168.2.27
Last login: Sun Nov 22 12:28:10 2020 from afocl-605021336.lan
[root@web2 ~]$exit
logout
Connection to 192.168.2.27 closed.
[root@ansible ~]$vim /etc/hosts
[root@ansible ~]$ping -c1 web1
PING web1 (192.168.2.17) 56(84) bytes of data.
64 bytes from web1 (192.168.2.17): icmp_seq=1 ttl=64 time=0.316 ms

--- web1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.316/0.316/0.316/0.000 ms
[root@ansible ~]$ping -c1 web2
PING web2 (192.168.2.27) 56(84) bytes of data.
64 bytes from web2 (192.168.2.27): icmp_seq=1 ttl=64 time=0.285 ms

--- web2 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.285/0.285/0.285/0.000 ms
[root@ansible ~]$cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.2.17    web1   www.web1.com
192.168.2.27    web2   www.web2.com

[root@ansible ~]$scp /etc/hosts 192.168.2.17:/etc/hosts
hosts                                                                                                                                           
[root@ansible ~]$scp /etc/hosts 192.168.2.27:/etc/hosts
hosts 

閰嶇疆ansible涓绘満娓呭崟锛屽苟娴嬭瘯ping

[root@ansible ~]$tail -3 /etc/ansible/hosts
[webservers]
web1
web2

[root@ansible ~]$ansible webservers -m ping
web1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
web2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

鎺ヤ笅鏉ュ紑濮嬮儴缃茶嚜鍔ㄥ寲瀹夎httpd鍙婇厤缃紝鎬濊矾濡備笅锛?br/>1銆佹嫹璐濇簮鐮佸寘鍒板悇涓绘満/data鐩綍涓?br/>2銆佽繍琛岃剼鏈紙婧愮爜缂栬瘧瀹夎鐨刪ttpd2.4鐗堟湰锛?br/>3銆侀厤缃湇鍔?/p>

鍑嗗宸ヤ綔

[root@ansible ~]$mkdir /data/playbook/roles -pv
mkdir: created directory 鈥?data/playbook鈥?mkdir: created directory 鈥?data/playbook/roles鈥?[root@ansible ~]$cd !:1
cd /data/playbook/roles
[root@ansible roles]$

[root@ansible roles]$mkdir httpd24/{files,tasks,handlers,templates} -pv
mkdir: created directory 鈥榟ttpd24鈥?mkdir: created directory 鈥榟ttpd24/files鈥?mkdir: created directory 鈥榟ttpd24/tasks鈥?mkdir: created directory 鈥榟ttpd24/handlers鈥?mkdir: created directory 鈥榟ttpd24/templates鈥?
[root@ansible roles]$tree -d 
.
鈹斺攢鈹€ httpd24
    鈹溾攢鈹€ files
    鈹溾攢鈹€ handlers
    鈹溾攢鈹€ tasks
    鈹斺攢鈹€ templates

[root@ansible roles]$cd httpd24/tasks/

[root@ansible tasks]$vim main.yml
[root@ansible tasks]$cat main.yml 
- include: time.yml
- include: copypkg.yml
- include: script.yml
- include: dir.yml
- include: config.yml
- include: index.yml

[root@ansible tasks]$awk 鈥榹print $3}鈥?main.yml | while read file; do touch $file ; done
[root@ansible tasks]$ll
total 28
-rw-r--r-- 1 root root 228 Nov 22 19:38 config.yml
-rw-r--r-- 1 root root 211 Nov 22 18:25 copypkg.yml
-rw-r--r-- 1 root root 208 Nov 22 20:14 dir.yml
-rw-r--r-- 1 root root  77 Nov 22 20:23 index.yml
-rw-r--r-- 1 root root   0 Nov 22 17:35 install.yml
-rw-r--r-- 1 root root 127 Nov 22 20:23 main.yml
-rw-r--r-- 1 root root  46 Nov 22 19:34 script.yml
-rw-r--r-- 1 root root 105 Nov 22 18:10 time.yml

缂栧啓鍚勪釜yml鏂囦欢锛屽涓嬶細

[root@ansible tasks]$cat time.yml 
- name: install ntpdate
  yum: name=ntpdate
- name: time synchronization
  shell: ntpdate ntp.aliyun.com

[root@ansible tasks]$cat copypkg.yml 
- name: copy apr package
  copy: src=apr-1.7.0.tar.gz dest=/data
- name: copy apr-util package
  copy: src=apr-util-1.6.1.tar.gz dest=/data
- name: copy httpd package
  copy: src=httpd-2.4.39.tar.bz2 dest=/data

[root@ansible tasks]$cat script.yml 
- name: run script
  script: install_httpd.sh

[root@ansible tasks]$cat dir.yml 
- name: virtual host configure dir
  file: path=/app/httpd24/conf/vhosts state=directory
- name: log dir
  file: path=/var/log/httpd state=directory
- name: data dir
  file: path=/web/vhosts state=directory

[root@ansible tasks]$cat config.yml 
- name: include configure dir
  shell: echo "Include conf/vhosts/*.conf" >> /app/httpd24/conf/httpd.conf
- name: configure virtual host
  template: src=web.conf.j2 dest=/app/httpd24/conf/vhosts/web.conf
  notify: restart httpd

[root@ansible tasks]$cat index.yml 
- name: index page
  template: src=index.html.j2 dest=/web/vhosts/index.html

[root@ansible roles]$vim /data/playbook/roles/role_httpd.yml
[root@ansible roles]$cat /data/playbook/roles/role_httpd.yml
- hosts: webservers

  roles:
    - role: httpd24

寮€濮嬫墽琛?/p>

[root@ansible roles]$pwd
/data/playbook/roles

[root@ansible roles]$ansible-playbook role_httpd.yml

瀹為獙缁撴灉锛屽涓嬶細

[root@web1 ~]$ss -tnl
State       Recv-Q Send-Q                                            Local Address:Port                                                           Peer Address:Port              
LISTEN      0      100                                                   127.0.0.1:25                                                                        *:*                  
LISTEN      0      128                                                           *:22                                                                        *:*                  
LISTEN      0      100                                                       [::1]:25                                                                     [::]:*                  
LISTEN      0      128                                                        [::]:80                                                                     [::]:*                  
LISTEN      0      128                                                        [::]:22
[root@web1 ~]$curl web1
web1
[root@web1 ~]$curl www.web1.com
web1
[root@web1 ~]$systemctl is-enabled httpd24
enabled

[root@web2 ~]$ss -tnl
State       Recv-Q Send-Q                                            Local Address:Port                                                           Peer Address:Port              
LISTEN      0      100                                                   127.0.0.1:25                                                                        *:*                  
LISTEN      0      128                                                           *:22                                                                        *:*                  
LISTEN      0      100                                                       [::1]:25                                                                     [::]:*                  
LISTEN      0      128                                                        [::]:80                                                                     [::]:*                  
LISTEN      0      128                                                        [::]:22 
[root@web2 ~]$curl web2
web2
[root@web2 ~]$curl www.web2.com
web2

[root@web2 ~]$systemctl is-enabled httpd24
enabled

以上是关于ansible鑷姩鍖栧畨瑁呭強閮ㄧ讲httpd的主要内容,如果未能解决你的问题,请参考以下文章

浣跨敤GFS闆嗙兢閮ㄧ讲KVM铏氭嫙鍖栧钩鍙?瀹炰緥!!!)

閲嶆俯澶ф暟鎹?--Hbase閮ㄧ讲浠ュ強鏋舵瀯鍒嗘瀽

锛?锛塸rometheus瀹夎閮ㄧ讲

Springboot 鎵撳寘閮ㄧ讲

Flask 鍜?Google App Engine 閮ㄧ讲妯″瀷鏈嶅姟

mongodb 4.2鍗曟満閮ㄧ讲