ansible自动化安装及部署httpd

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 epel
baseurl=http://mirrors.aliyun.com/epel/$releasever/$basearch
enabled=1
gpgcheck=0

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

因为要部署两台主机,且ansible是依赖ssh密钥认证,所以接下来操作ansible免密码登陆web1和web2,同时也需要更改/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‘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-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 ‘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 keys
root@192.168.2.17‘s password: 

Number of key(s) added: 1

Now 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)? y
Please 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 keys
root@192.168.2.27‘s password: 

Number of key(s) added: 1

Now 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.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
192.168.2.27    web2

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

准备工作

[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 ‘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]$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

开始执行

[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的主要内容,如果未能解决你的问题,请参考以下文章

ansible安装及使用

ansible自动化运维详解ansible的安装部署参数使用清单管理配置文件参数及用户级ansible操作环境构建

ansible自动化运维详解ansible的安装部署参数使用清单管理配置文件参数及用户级ansible操作环境构建

ansible自动化运维详解ansible的安装部署参数使用清单管理配置文件参数及用户级ansible操作环境构建

Ansible 的安装及部署

自动化运维Ansible安装部署及使用