AI智能
改变未来

ansible自动化安装及部署httpd

实验环境:(开始实验之前,请保证防火墙关闭,selinux关闭,时间同步)
ansible:192.168.2.7 web1:192.168.2.7 web2:192.168.2.17
OS:centos7 OS:centos7 OS:centos7

实现功能:(自动化部署两台各自主机命名的httpd服务器)
1、使用ansible的playbook实现自动化安装httpd
2、建立httpd服务器,要求提供各自基于主机名的虚拟主机:
(1)www.x.com,页面文件目录为/web/vhosts;错误日志为
/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,内容分别为其对应的主机名

注意:脚本是源码编译安装的httpd2.4

ansible安装
配置epel源,yum安装ansible即可

[root@ansible ~]$cat /etc/yum.repos.d/epel.repo[epel]name=aliyun epelbaseurl=http://mirrors.aliyun.com/epel/$releasever/$basearchenabled=1gpgcheck=0[root@ansible ~]$yum -y install ansible....

因为要部署两台主机,且ansible是依赖ssh密钥认证,所以接下来操作ansible免密码登陆web1和web2,同时也需要更改/etc/hosts文件

[root@ansible ~]$ssh-keygenGenerating 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@ansibleThe key\'s 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-idanaconda-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 \'192.168.2.17 (192.168.2.17)\' can\'t 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 keysroot@192.168.2.17\'s password:Number of key(s) added: 1Now try logging into the machine, with:   \"ssh \'192.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 \'192.168.2.27 (192.168.2.27)\' can\'t 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)? yPlease type \'yes\' or \'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 keysroot@192.168.2.27\'s password:Number of key(s) added: 1Now try logging into the machine, with:   \"ssh \'192.168.2.27\'\"and check to make sure that only the key(s) you wanted were added.[root@ansible ~]$ssh 192.168.2.17Last login: Sun Nov 22 12:27:57 2020 from afocl-605021336.lan[root@web1 ~]$exitlogoutConnection to 192.168.2.17 closed.[root@ansible ~]$ssh 192.168.2.27Last login: Sun Nov 22 12:28:10 2020 from afocl-605021336.lan[root@web2 ~]$exitlogoutConnection to 192.168.2.27 closed.[root@ansible ~]$vim /etc/hosts[root@ansible ~]$ping -c1 web1PING 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 0msrtt min/avg/max/mdev = 0.316/0.316/0.316/0.000 ms[root@ansible ~]$ping -c1 web2PING 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 0msrtt min/avg/max/mdev = 0.285/0.285/0.285/0.000 ms[root@ansible ~]$cat /etc/hosts127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4::1         localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.2.17    web1   www.web1.com192.168.2.27    web2   www.web2.com[root@ansible ~]$scp /etc/hosts 192.168.2.17:/etc/hostshosts[root@ansible ~]$scp /etc/hosts 192.168.2.27:/etc/hostshosts

配置ansible主机清单,并测试ping

[root@ansible ~]$tail -3 /etc/ansible/hosts[webservers]web1web2[root@ansible ~]$ansible webservers -m pingweb1 | 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及配置,思路如下:
1、拷贝源码包到各主机/data目录下
2、运行脚本(源码编译安装的httpd2.4版本)
3、配置服务

准备工作

[root@ansible ~]$mkdir /data/playbook/roles -pvmkdir: created directory ‘/data/playbook’mkdir: created directory ‘/data/playbook/roles’[root@ansible ~]$cd !:1cd /data/playbook/roles[root@ansible roles]$[root@ansible roles]$mkdir httpd24/{files,tasks,handlers,templates} -pvmkdir: created directory ‘httpd24’mkdir: created directory ‘httpd24/files’mkdir: created directory ‘httpd24/tasks’mkdir: created directory ‘httpd24/handlers’mkdir: created directory ‘httpd24/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]$lltotal 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 ntpdateyum: name=ntpdate- name: time synchronizationshell: ntpdate ntp.aliyun.com[root@ansible tasks]$cat copypkg.yml- name: copy apr packagecopy: src=apr-1.7.0.tar.gz dest=/data- name: copy apr-util packagecopy: src=apr-util-1.6.1.tar.gz dest=/data- name: copy httpd packagecopy: src=httpd-2.4.39.tar.bz2 dest=/data[root@ansible tasks]$cat script.yml- name: run scriptscript: install_httpd.sh[root@ansible tasks]$cat dir.yml- name: virtual host configure dirfile: path=/app/httpd24/conf/vhosts state=directory- name: log dirfile: path=/var/log/httpd state=directory- name: data dirfile: path=/web/vhosts state=directory[root@ansible tasks]$cat config.yml- name: include configure dirshell: echo \"Include conf/vhosts/*.conf\" >> /app/httpd24/conf/httpd.conf- name: configure virtual hosttemplate: src=web.conf.j2 dest=/app/httpd24/conf/vhosts/web.confnotify: restart httpd[root@ansible tasks]$cat index.yml- name: index pagetemplate: 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: webserversroles:- role: httpd24

开始执行

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

实验结果,如下:

[root@web1 ~]$ss -tnlState       Recv-Q Send-Q                                            Local Address:Port                                                           Peer Address:PortLISTEN      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 web1web1[root@web1 ~]$curl www.web1.comweb1[root@web1 ~]$systemctl is-enabled httpd24enabled[root@web2 ~]$ss -tnlState       Recv-Q Send-Q                                            Local Address:Port                                                           Peer Address:PortLISTEN      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 web2web2[root@web2 ~]$curl www.web2.comweb2[root@web2 ~]$systemctl is-enabled httpd24enabled
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » ansible自动化安装及部署httpd