ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-08-02 21:35:06  阅读:156  来源: 互联网

标签:index 10.0 apr httpd 0.8 html install IP地址 dir


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有