其他分享
首页 > 其他分享> > 3.Ansible playbook实现apache批量部署,并对不同主机提供以各自IP地址为内容的index.html

3.Ansible playbook实现apache批量部署,并对不同主机提供以各自IP地址为内容的index.html

作者:互联网

3.Ansible playbook实现apache批量部署,并对不同主机提供以各自IP地址为内容的index.html

准备:

(1)到https://mirrors.tuna.tsinghua.edu.cn/查询各个软件的现有版本。

(2)实现ansible主机连接互联网。

 

 

1.实现ansible主机与其它机器的基于key验证,ansible检查服务端到远程主机的健康性,准备hosts清单。

[root@ansible ~]# cat ssh_key.sh

#!/bin/bash

IPLIST="

10.0.0.8

10.0.0.6

10.0.0.5

"

rpm -q sshpass &> /dev/null || yum -y install sshpass

[ -f /root/.ssh/id_rsa ] || ssh-keygen -f /root/.ssh/id_rsa -P ''

export SSHPASS=123456789

for IP in $IPLIST;do

    { sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $IP; } &

done

Wait

 

[root@ansible ~]# ./ssh_key.sh

 

 

[root@ansible ~]# ansible all -m ping

10.0.0.8 | SUCCESS => {

    "ansible_facts": {

        "discovered_interpreter_python": "/usr/libexec/platform-python"

    },

    "changed": false,

    "ping": "pong"

}

 

[root@ansible ~]# cat /etc/ansible/hosts

[websrvs]

10.0.0.8

 

 

2.编写playbook剧本

[root@ansible ~]# cat install_httpd.yml

---

- hosts: websrvs

  remote_user: root

  vars:

    download_dir: /usr/local/src

    install_dir: /apps/httpd

    httpd_version: httpd-2.4.54

    apr_version: apr-1.7.0

    apr_util_version: apr-util-1.6.1

    httpd_url: https://mirrors.tuna.tsinghua.edu.cn/apache/httpd

    apr_url: https://mirrors.tuna.tsinghua.edu.cn/apache/apr

 

  tasks:

    - name: install packages

      yum: name=gcc,make,pcre-devel,openssl-devel,expat-devel,bzip2 state=installed

    - name: download httpd file

      unarchive: src="{{ httpd_url }}/{{ httpd_version }}.tar.bz2" dest={{ download_dir }} owner=root remote_src=yes

    - name: download apr file

      unarchive: src="{{ apr_url }}/{{ apr_version }}.tar.bz2" dest={{ download_dir }} owner=root remote_src=yes

    - name: download apr_util file

      unarchive: src="{{ apr_url }}/{{ apr_util_version }}.tar.bz2" dest={{ download_dir }} owner=root remote_src=yes

    - name: prepare apr dir

      shell: chdir={{ download_dir }} mv {{ apr_version }} {{ download_dir }}/{{ httpd_version }}/srclib/apr

    - name: prepare apr_util dir

      shell: chdir={{ download_dir }} mv {{ apr_util_version }} {{ download_dir }}/{{ httpd_version }}/srclib/apr-util

    - name: build httpd

      shell: chdir={{ download_dir }}/{{ httpd_version }} ./configure --prefix={{ install_dir }} --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork && make -j {{ ansible_processor_vcpus }} && make install

    - name: create group

      group: name=apache gid=80 system=yes

    - name: create user

      user: name=apache uid=80 group=apache shell=/sbin/nologin system=yes create_home=no home={{ install_dir }}/conf/httpd

    - name: set httpd user

      lineinfile: path={{ install_dir }}/conf/httpd.conf regexp='^User' line='User apache'

    - name: set httpd group

      lineinfile: path={{ install_dir }}/conf/httpd.conf regexp='^Group' line='Group apache'

    - name: set variable PATH

      shell: echo PATH={{ install_dir }}/bin:$PATH >> /etc/profile.d/httpd.sh

    - name: prepare service file

      template: src=./httpd.service.j2 dest=/usr/lib/systemd/system/httpd.service

    - name: start service

      service: name=httpd state=started enabled=yes

 

 

3. 准备模版目录及相关文件

实验中,install_httpd.yml在放在哪里,我们就以此为准建立tmplates目录,并把xxxx.j2的模版文件放在tmplates目录下。

例如:

[root@ansible ~]# tree

.

├── install_httpd.yml

├── ssh_key.sh

└── templates

└── httpd.service.j2

install_httpd.yml templates处于一个目录下,而hppt.service.j2在template下。

 

[root@ansible ~]# cat ./templates/httpd.service.j2

 

[Unit]

Description=The Apache HTTP Server

After=network.target remote-fs.target nss-lookup.target

Documentation=man:httpd(8)

Documentation=man:apachectl(8)

 

[Service]

Type=forking

#EnvironmentFile=/etc/sysconfig/httpd

ExecStart={{ install_dir }}/bin/apachectl start

#ExecStart={{ install_dir }}/bin/httpd $OPTIONS -k start

ExecReload={{ install_dir }}/bin/apachectl graceful

#ExecReload={{ install_dir }}/bin/httpd $OPTIONS -k graceful

ExecStop={{ install_dir }}/bin/apachectl stop

KillSignal=SIGCONT

PrivateTmp=true

[Install]

WantedBy=multi-user.target

 

4. 执行过程

[root@ansible ~]# ansible-playbook install_httpd.yml

 

PLAY [websrvs] ********************************************************************************************************************************************************

 

TASK [Gathering Facts] ************************************************************************************************************************************************

ok: [10.0.0.8]

 

TASK [install packages] ***********************************************************************************************************************************************

ok: [10.0.0.8]

 

TASK [download httpd file] ********************************************************************************************************************************************

changed: [10.0.0.8]

 

TASK [download apr file] **********************************************************************************************************************************************

changed: [10.0.0.8]

 

TASK [download apr_util file] *****************************************************************************************************************************************

changed: [10.0.0.8]

 

TASK [prepare apr dir] ************************************************************************************************************************************************

changed: [10.0.0.8]

 

TASK [prepare apr_util dir] *******************************************************************************************************************************************

changed: [10.0.0.8]

 

TASK [build httpd] ****************************************************************************************************************************************************

changed: [10.0.0.8]

 

TASK [create group] ***************************************************************************************************************************************************

ok: [10.0.0.8]

 

TASK [create user] ****************************************************************************************************************************************************

ok: [10.0.0.8]

 

TASK [set httpd user] *************************************************************************************************************************************************

ok: [10.0.0.8]

 

TASK [set httpd group] ************************************************************************************************************************************************

ok: [10.0.0.8]

 

TASK [set variable PATH] **********************************************************************************************************************************************

changed: [10.0.0.8]

 

TASK [prepare service file] *******************************************************************************************************************************************

changed: [10.0.0.8]

 

TASK [start service] **************************************************************************************************************************************************

changed: [10.0.0.8]

 

PLAY RECAP ************************************************************************************************************************************************************

10.0.0.8                   : ok=15   changed=9    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

 

 

 

[root@CentOS8 ~]# ls /usr/local/src/

httpd-2.4.54

 

[root@CentOS8 ~]# systemctl start httpd

 

[root@CentOS8 ~]# ss -ntpl | grep 80

LISTEN 0      128                *:80              *:*    users:(("httpd",pid=91167,fd=4),("httpd",pid=91166,fd=4),("httpd",pid=91165,fd=4),("httpd",pid=91164,fd=4),("httpd",pid=91163,fd=4),("httpd",pid=91162,fd=4))

 

 

 

[root@ansible ~]# curl 10.0.0.8

<html><body><h1>10.0.0.8</h1></body></html>

 

标签:index,10.0,apr,httpd,0.8,html,install,IP地址,dir
来源: https://www.cnblogs.com/biaoming534/p/16545228.html