使用ansible 源码安装httpd

使用ansible 源码安装httpd

1. 环境

服务IP域名版本
ansible192.168.232.129129a.example.comCentOS Stream release 8
httpd192.168.232.134134h.example.comRed Hat Linux 8.5

2. 解析

[root@129a files]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.232.134 134h.example.com
[root@129a files]# 

3. 配置免密登录

ssh-keygen -t rsa
ssh-copy-id root@134h.example.com

4. 配置清单,查看是否ping通

[root@129a apache]# vim ansible.cfg 
[root@129a apache]# cat ansible.cfg|grep inventory
inventory       = inventory
[root@129a httpd]# cat inventory 
[webservers]
134h.example.com
[root@129a httpd]# 


[root@129a httpd]# ansible all -m ping
134h.example.com | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
[root@129a httpd]# 

5. 下载httpd软件包

wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz
wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz
wget https://downloads.apache.org/httpd/httpd-2.4.53.tar.gz
[root@129a httpd]# ls files/package/
apr-1.7.0.tar.gz       httpd-2.4.53.tar.gz
apr-util-1.6.1.tar.gz
[root@129a httpd]# 

6. 创建用户和安装依赖包

定义变量
[root@129a httpd]# cat vars/apache.yml 
apache_depend_pkg:
  - openssl-devel 
  - pcre-devel 
  - expat-devel 
  - libtool
  - make
  - gcc
  - gcc-c++
[root@129a httpd]# 

[root@129a httpd]# cat install.yml 
---
- hosts: webservers
  gather_facts: no
  vars_files:
    - vars/apache.yml
  tasks:
    - name: create user for apache
      user:
        name: apache
        state: present
        create_home: no
        system: yes
        shell: /sbin/nologin
    - name: Delete the yum source
      file:
        path: /etc/yum.repos.d
        state: absent
    - name: Provide the yum source file
      copy:
        src: files/CentOS-Base.repo
        dest: /etc/yum.repos.d/
    - name: install dependency packages
      yum:
        name: '{{ apache_depend_pkg }}'
        state: present
[root@129a httpd]# 

7. 提供软件包

[root@129a httpd]# ls files/
CentOS-Base.repo  game  package
[root@129a httpd]# cd files/package/
[root@129a package]# ls
apr-1.7.0.tar.gz       httpd-2.4.53.tar.gz
apr-util-1.6.1.tar.gz
[root@129a package]# 
[root@129a httpd]# vim vars/apache.yml 
[root@129a httpd]# cat vars/apache.yml 
apache_depend_pkg:
  - openssl-devel 
  - pcre-devel 
  - expat-devel 
  - libtool
  - make
  - gcc
  - gcc-c++
apache_software:
  - files/package/apr-1.7.0.tar.gz
  - files/package/apr-util-1.6.1.tar.gz
  - files/package/httpd-2.4.53.tar.gz
[root@129a httpd]# 

[root@129a httpd]# ansible-playbook install.yml 

PLAY [webservers] ******************************************************************************************************************

TASK [create user for apache] ******************************************************************************************************
ok: [130h.example.com]

TASK [Delete the yum source] *******************************************************************************************************
changed: [130h.example.com]

[root@129a httpd]# ansible-playbook 134h.yml 

PLAY [webservers] ******************************************[root@129a httpd]# ansible-playbook install.yml 

PLAY [webservers] **************************************************************
[root@129a httpd]# ansible-playbook install.yml 

PLAY [webservers] **************************************************************

TASK [create user for apache] **************************************************
ok: [134h.example.com]

TASK [Delete the yum source] ***************************************************
changed: [134h.example.com]

TASK [Provide the yum source file] *********************************************
changed: [134h.example.com]

TASK [install dependency packages] *********************************************
ok: [134h.example.com]

TASK [Provide software packages] ***********************************************
changed: [134h.example.com] => (item=files/package/apr-1.7.0.tar.gz)
changed: [134h.example.com] => (item=files/package/apr-util-1.6.1.tar.gz)
changed: [134h.example.com] => (item=files/package/httpd-2.4.53.tar.gz)

PLAY RECAP *********************************************************************
134h.example.com           : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@129a httpd]# 

8. 编译安装

[root@129a httpd]# cat vars/apache.yml 
apache_depend_pkg:
  - openssl-devel 
  - pcre-devel 
  - expat-devel 
  - libtool
  - make
  - gcc
  - gcc-c++
apache_software:
  - files/package/apr-1.7.0.tar.gz
  - files/package/apr-util-1.6.1.tar.gz
  - files/package/httpd-2.4.53.tar.gz
install_dir: /usr/local/apache
[root@129a httpd]# 

[root@129a httpd]# cat install.yml 
---
- hosts: webservers
  gather_facts: no
  vars_files:
    - vars/apache.yml
  tasks:
    - name: create user for apache
      user:
        name: apache
        state: present
        create_home: no
        system: yes
        shell: /sbin/nologin
    - name: Delete the yum source
      file:
        path: /etc/yum.repos.d
        state: absent
    - name: Provide the yum source file
      copy:
        src: files/CentOS-Base.repo
        dest: /etc/yum.repos.d/
    - name: install dependency packages
      yum:
        name: '{{ apache_depend_pkg }}'
        state: present
    - name: Provide software packages
      copy:
        src: "{{ item }}"
        dest: /usr/src
      loop: "{{ apache_software }}"
    - name: install apache
      shell: cd /usr/src && tar xf apr-1.7.0.tar.gz && tar xf apr-util-1.6.1.tar.gz && tar xf httpd-2.4.53.tar.gz && sed -i '/$RM "$cfgfile"/d' apr-1.7.0/configure && cd apr-1.7.0 && ./configure --prefix=/usr/local/apr && make && make install && cd ../apr-util-1.6.1 && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install && cd ../httpd-2.4.53 && ./configure --prefix={{ install_dir }} --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork && make && make install

9. 测试源码安装httpd,目录存在,我们就跳过,具备幂等性

[root@129a httpd]# cat install.yml 
---
- hosts: webservers
  gather_facts: no
  vars_files:
    - vars/apache.yml
  tasks:
    - name: create user for apache
      user:
        name: apache
        state: present
        create_home: no
        system: yes
        shell: /sbin/nologin
    - name: Delete the yum source
      file:
        path: /etc/yum.repos.d
        state: absent
    - name: Provide the yum source file
      copy:
        src: files/CentOS-Base.repo
        dest: /etc/yum.repos.d/
    - name: install dependency packages
      yum:
        name: '{{ apache_depend_pkg }}'
        state: present
    - name: Provide software packages
      copy:
        src: "{{ item }}"
        dest: /usr/src
      loop: "{{ apache_software }}"
    - name: test {{ install_dir }} is exists
      command: test -d {{ install_dir }}
      register: result
    - name: install apache
      shell: cd /usr/src && tar xf apr-1.7.0.tar.gz && tar xf apr-util-1.6.1.tar.gz && tar xf httpd-2.4.53.tar.gz && sed -i '/$RM "$cfgfile"/d' apr-1.7.0/configure && cd apr-1.7.0 && ./configure --prefix=/usr/local/apr && make && make install && cd ../apr-util-1.6.1 && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install && cd ../httpd-2.4.53 && ./configure --prefix={{ install_dir }} --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork && make && make install
      when: result['rc'] != 0
[root@129a httpd]# 

result['rc'] == 0必须安装,!= 跳过

[root@129a httpd]# ansible-playbook install.yml 

PLAY [webservers] **************************************************************

TASK [create user for apache] **************************************************
ok: [134h.example.com]

TASK [Delete the yum source] ***************************************************
changed: [134h.example.com]

TASK [Provide the yum source file] *********************************************
changed: [134h.example.com]

TASK [install dependency packages] *********************************************
ok: [134h.example.com]

TASK [Provide software packages] ***********************************************
ok: [134h.example.com] => (item=files/package/apr-1.7.0.tar.gz)
ok: [134h.example.com] => (item=files/package/apr-util-1.6.1.tar.gz)
ok: [134h.example.com] => (item=files/package/httpd-2.4.53.tar.gz)

TASK [test /usr/local/apache is exists] ****************************************
changed: [134h.example.com]

TASK [install apache] **********************************************************
skipping: [134h.example.com]

PLAY RECAP *********************************************************************
134h.example.com           : ok=6    changed=3    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   

[root@129a httpd]# 

10. 优化 install.yml

写执行脚本
[root@129a httpd]# cp install.yml files/install.sh
[root@129a httpd]# vim files/install.sh 
[root@129a httpd]# cat files/install.sh 
#!/bin/bash

cd /usr/src 
tar xf apr-1.7.0.tar.gz 
tar xf apr-util-1.6.1.tar.gz 
tar xf httpd-2.4.53.tar.gz 
sed -i '/$RM "$cfgfile"/d' apr-1.7.0/configure 
cd apr-1.7.0 
./configure --prefix=/usr/local/apr && \
make && make install && \ 
cd ../apr-util-1.6.1 
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && \
make && make install && \
cd ../httpd-2.4.53 
./configure --prefix={{ install_dir }} \
        --enable-so \
        --enable-ssl \
        --enable-cgi \
        --enable-rewrite \
        --with-zlib \
        --with-pcre \
        --with-apr=/usr/local/apr \
        --with-apr-util=/usr/local/apr-util/ \
        --enable-modules=most \
        --enable-mpms-shared=all \
        --with-mpm=prefork && \
make && make install
[root@129a httpd]# 

给执行权限
[root@129a httpd]# ll files/install.sh 
-rw-r--r--. 1 root root 685 Jun 13 16:15 files/install.sh
[root@129a httpd]# chmod +x files/install.sh 
[root@129a httpd]# ll files/install.sh 
-rwxr-xr-x. 1 root root 685 Jun 13 16:15 files/install.sh
[root@129a httpd]# 

修改install.yml
[root@129a httpd]# tail -5 install.yml 
      command: test -d {{ install_dir }}
      register: result
    - name: install apache
      script: files/install.sh
      when: result['rc'] != 0
[root@129a httpd]# 

TASK [install apache] **********************************************************
fatal: [134h.example.com]: FAILED! => {"msg": "The conditional check 'result['rc'] != 0' failed. The error was: error while evaluating conditional (result['rc'] != 0): 'dict object' has no attribute 'rc'\n\nThe error appears to be in '/opt/httpd/install.yml': line 34, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n      register: result\n    - name: install apache\n      ^ here\n"}

PLAY RECAP *********************************************************************
134h.example.com           : ok=5    changed=1    unreachable=0    failed=1    skipped=1    rescued=0    ignored=0   

[root@129a httpd]# 
上面的红色,条件不满足,不安装,是对的
 
[root@129a httpd]# tail -5 install.yml 
      register: result
    - name: install apache
      script: files/install.sh
      when: result['rc'] != 0
      ignore_errors: yes
[root@129a httpd]# 
忽略错误,后面得到任务可以执行        

11. 设置开机自启

[root@134h system]# cat sshd.service 
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.target
Wants=sshd-keygen.target

[Service]
Type=notify
EnvironmentFile=-/etc/crypto-policies/back-ends/opensshserver.config
EnvironmentFile=-/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS $CRYPTO_POLICY
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target
[root@134h system]# pwd
/usr/lib/systemd/system
[root@134h system]# 


配置:
[root@129a files]# ls
CentOS-Base.repo  httpd.service.j2  package
game              install.sh
[root@129a files]# cat httpd.service.j2 
[Unit]
Description=httpd server daemon
After=network.target

[Service]
Type=forking
ExecStart={{ install_dir }}/bin/apachectl start
ExecStop={{ install_dir }}/bin/apachectl stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
[root@129a files]# 

[root@129a httpd]# cat install.yml 
---
- hosts: webservers
  gather_facts: no
  vars_files:
    - vars/apache.yml
  tasks:
    - name: create user for apache
      user:
        name: apache
        state: present
        create_home: no
        system: yes
        shell: /sbin/nologin
    - name: Delete the yum source
      file:
        path: /etc/yum.repos.d
        state: absent
    - name: Provide the yum source file
      copy:
        src: files/CentOS-Base.repo
        dest: /etc/yum.repos.d/
    - name: install dependency packages
      yum:
        name: "{{ apache_depend_pkg }}"
        state: present
    - name: Provide software packages
      copy:
        src: "{{ item }}"
        dest: /usr/src
      loop: "{{ apache_software }}"
    - name: test {{ install_dir }} is exists
      command: test -d {{ install_dir }}
      register: result
    - name: install apache
      script: files/install.sh
      when: result['rc'] != 0
      ignore_errors: yes
    - name: provide services files for httpd
      template:
        src: files/httpd.service.j2
        dest: /usr/lib/systemd/system/httpd.service
[root@129a httpd]#

查看
[root@134h system]# ls /usr/lib/systemd/system/httpd.service 
/usr/lib/systemd/system/httpd.service
[root@134h system]# cat /usr/lib/systemd/system/httpd.service 
[Unit]
Description=httpd server daemon
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
[root@134h system]# 

[root@134h ~]# systemctl status httpd
● httpd.service - httpd server daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vend>
   Active: inactive (dead)
[root@134h ~]# systemctl start httpd
[root@134h ~]# systemctl status httpd
● httpd.service - httpd server daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vend>
   Active: active (running) since Mon 2022-06-13 16:58:25 CST; 4s ago
  Process: 100110 ExecStart=/usr/local/apache/bin/apachectl start (code=>
 Main PID: 100453 (httpd)

12. 默认网站

[root@129a httpd]# ls
ansible.cfg  files  install.yml  inventory  vars
[root@129a httpd]# cp install.yml main.yml
[root@129a httpd]# 
install.yml制作成tasks.yml
[root@129a httpd]# vim install.yml 
[root@129a httpd]# cat install.yml 
- name: create user for apache
  user:
    name: apache
    state: present
    create_home: no
    system: yes
    shell: /sbin/nologin
- name: Delete the yum source
  file:
    path: /etc/yum.repos.d
    state: absent
- name: Provide the yum source file
  copy:
    src: files/CentOS-Base.repo
    dest: /etc/yum.repos.d/
- name: install dependency packages
  yum:
    name: "{{ apache_depend_pkg }}"
    state: present
- name: Provide software packages
  copy:
    src: "{{ item }}"
    dest: /usr/src
  loop: "{{ apache_software }}"
- name: test {{ install_dir }} is exists
  command: test -d {{ install_dir }}
  register: result
- name: install apache
  script: files/install.sh
  when: result['rc'] != 0
  ignore_errors: yes
- name: provide services files for httpd
  template:
    src: files/httpd.service.j2
    dest: /usr/lib/systemd/system/httpd.service
[root@129a httpd]# 
配置主要yml
[root@129a httpd]# ls
ansible.cfg  files  install.yml  inventory  main.yml  vars
[root@129a httpd]# cat main.yml 
---
- hosts: webservers
  gather_facts: no
  vars_files:
    - vars/apache.yml
  tasks:
    - include_tasks: install.yml
    - name: start httpd
      service:
        name: httpd
        state: started
        enabled: yes
[root@129a httpd]# 

[root@129a httpd]# ansible-playbook main.yml 
12.1 关闭防火墙
[root@134h ~]# systemctl stop firewalld
[root@134h ~]# ss -antl
State   Recv-Q  Send-Q   Local Address:Port   Peer Address:Port Process  
LISTEN  0       128            0.0.0.0:22          0.0.0.0:*             
LISTEN  0       128                  *:80                *:*             
LISTEN  0       128               [::]:22             [::]:*             
[root@134h ~]# 

  • 访问

在这里插入图片描述

13. 另一个项目 runtime,配置虚拟主机

先复制过来
[root@129a files]# pwd
/opt/playbook/runtime/files
[root@129a files]# scp 134h.example.com://usr/local/apache/conf/original/extra/httpd-vhosts.conf .
httpd-vhosts.conf                      100% 1465    60.2KB/s   00:00    
[root@129a files]# 
[root@129a files]# ls
game  httpd-vhosts.conf
[root@129a files]# mv httpd-vhosts.conf vhosts.conf.j2
[root@129a files]# vim vhosts.conf.j2 
[root@129a files]# cat vhosts.conf.j2 
<VirtualHost *:{{ PORT }}>
    DocumentRoot "{{ install_dir }}/htdocs/game"
    ServerName {{ game_domain }}
    ErrorLog "logs/{{ game_domain }}-error_log"
    CustomLog "logs/{{ game_domain }}-access_log" common
</VirtualHost>
[root@129a files]# 

受控机

[root@134h conf]# cat httpd.conf|grep httpd-vhosts.conf
#Include conf/extra/httpd-vhosts.conf
[root@134h conf]# pwd
/usr/local/apache/conf
[root@134h conf]# 

应用并配置主要main.yml
[root@129a runtime]# cat main.yml 
---
- hosts: 134h.example.com
  gather_facts: no
  vars_files:
    - vars/apache.yml
  tasks:
    - include_tasks: ../httpd/install.yml
    - name: config httpd for runtime
      template:
        src: files/vhosts.conf.j2
        dest: "{{ install_dir }}/conf/extra/vhosts.conf"
      notify:
        - restart httpd for runtime
    - name: include extra for runtime
      lineinfile:
        line: Include conf/extra/vhosts.conf
        path: "{{ install_dir }}/conf/httpd.conf"
        state: present
      notify:
        - restart httpd for runtime
    - name: Provide web site for runtime
      copy:
src: files/game
        dest: "{{ install_dir }}/htdocs"
    - name: close firewalld for runtime
      service:
        name: firewalld
        state: stopped
        enabled: no
    - name: start service for runtime
      service:
        name: httpd
        state: started
        enabled: yes

  handlers:
    - name: restart httpd for runtime
      service:
        name: httpd
        state: restarted      
[root@129a runtime]#

编写变量
[root@129a runtime]# vim vars/apache.yml 
[root@129a runtime]# cat vars/apache.yml 
apache_depend_pkg:
  - openssl-devel 
  - pcre-devel 
  - expat-devel 
  - libtool
  - make
  - gcc
  - gcc-c++
apache_software:
  - files/package/apr-1.7.0.tar.gz
  - files/package/apr-util-1.6.1.tar.gz
  - files/package/httpd-2.4.53.tar.gz
install_dir: /usr/local/apache
PORT: 80
game_domain: game.example.com
[root@129a runtime]# 

14. 提供网站

创建playbook,将httpd移入,并创建另外一个项目

移入网站内容
[root@129a playbook]# ls
httpd
[root@129a playbook]# pwd
/opt/playbook
[root@129a playbook]# 

[root@129a files]# cp -r /opt/playbook/httpd/files/game/ .
[root@129a files]# ls
game
[root@129a files]# ls game/
'#U670d#U52a1#U5668#U4e4b#U5bb6.url'
'#U7cbe#U54c1#U514d#U8d39#U5546#U4e1a#U6e90#U7801#U4e0b#U8f7d.url'
 css
 fonts
 images
 index.html
 js
 m
[root@129a files]# pwd
/opt/playbook/runtime/files
[root@129a files]# 


[root@129a runtime]# vim main.yml 
[root@129a runtime]# cat main.yml 
---
- hosts: 134h.example.com
  gather_facts: no
  vars_files:
    - vars/apache.yml
  tasks:
    - include_tasks: ../httpd/install.yml
    - name: config httpd for runtime
      template:
        src: files/vhosts.conf.j2
        dest: "{{ install_dir }}/conf/extra/vhosts.conf"
      notify:
        - restart httpd for runtime
    - name: include extra for runtime
      lineinfile:
        line: Include conf/extra/vhosts.conf
        path: "{{ install_dir }}/conf/httpd.conf"
        state: present
      notify:
        - restart httpd for runtime
    - name: Provide web site for runtime
      copy:
        src: files/game
        dest: "{{ install_dir }}/htdocs"
    - name: close firewalld for runtime
      service:
        name: firewalld
        state: stopped
        enabled: no
    - name: start service for runtime
      service:
        name: httpd
        state: started
        enabled: yes

  handlers:
    - name: restart httpd for runtime
      service:
        name: httpd
        state: restarted      
[root@129a runtime]#

15. 编写主程序文件

[root@129a httpd]# cat install.yml 
[root@129a httpd]# tail -12 install.yml 
- name: test {{ install_dir }} is exists
  command: test -d {{ install_dir }}
  register: result
  ignore_errors: yes//没安装忽略错误
- name: install apache
  script: files/install.sh
  when: result['rc'] != 0
  ignore_errors: yes//安装之后忽略错误
- name: provide services files for httpd
  template:
    src: files/httpd.service.j2
    dest: /usr/lib/systemd/system/httpd.service
[root@129a httpd]# 

将playbook/httpd/files中的脚本文件制作成模板
[root@129a files]# pwd
/opt/playbook/httpd/files
[root@129a files]# mv install.sh install.sh.j2
[root@129a files]# 
[root@129a files]# pwd
/opt/playbook/httpd/files
[root@129a files]# mv install.sh install.sh.j2
[root@129a files]# 

继续编写httpd的任务
 [root@129a httpd]# ls
ansible.cfg  files  install.yml  inventory  main.yml  vars
[root@129a httpd]# vim install.yml 
[root@129a httpd]# cat install.yml 
- name: create user for apache
  user:
    name: apache
    state: present
    create_home: no
    system: yes
    shell: /sbin/nologin
- name: Delete the yum source
  file:
    path: /etc/yum.repos.d
    state: absent
- name: Provide the yum source file
  copy:
    src: files/CentOS-Base.repo
    dest: /etc/yum.repos.d/
- name: install dependency packages
  yum:
    name: "{{ apache_depend_pkg }}"
    state: present
- name: Provide software packages
  copy:
    src: "{{ item }}"
    dest: /usr/src
  loop: "{{ apache_software }}"
- name: test {{ install_dir }} is exists
  command: test -d {{ install_dir }}
  register: result
  ignore_errors: yes
- name: provide install script for httpd
  template:
    src: files/install.sh.j2
    dest: /tmp/install.sh
    owner: root
    group: root
    mode: 0755
- name: install apache
  shell: /tmp/install.sh
  when: result['rc'] != 0
  ignore_errors: yes
- name: provide services files for httpd
  template:
    src: files/httpd.service.j2
    dest: /usr/lib/systemd/system/httpd.service
[root@129a httpd]# 

16. 验证

[root@134h ~]# ss -antl
State   Recv-Q  Send-Q   Local Address:Port   Peer Address:Port Process  
LISTEN  0       128            0.0.0.0:22          0.0.0.0:*             
LISTEN  0       128                  *:80                *:*             
LISTEN  0       128               [::]:22             [::]:*             
[root@134h ~]# systemctl status httpd
● httpd.service - httpd server daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendo>
   Active: active (running) since Mon 2022-06-13 19:38:47 CST; 1h 22min >
  Process: 453149 ExecStop=/usr/local/apache/bin/apachectl stop (code=ex>
  Process: 453537 ExecStart=/usr/local/apache/bin/apachectl start (code=>
 Main PID: 453883 (httpd)

16.1 访问

在这里插入图片描述

16.2 注意
  • 变量不管是当前写的playbook还是调用的,变量都要写

  • 包含的软件包在引用的项目不用定义

17. 优化

将runtime的模本文件vhosts.conf.j2放在httpd中
[root@129a runtime]# mv files/vhosts.conf.j2 ../httpd/files/
[root@129a runtime]# ls files/
game
[root@129a runtime]# 

[root@129a ~]# cd /opt/playbook/httpd/files/
[root@129a files]# ls
CentOS-Base.repo  httpd.service.j2  package
game              install.sh.j2     vhosts.conf.j2
[root@129a files]# 

[root@129a runtime]# cat main.yml 
---
- hosts: 134h.example.com
  gather_facts: no
  vars_files:
    - vars/apache.yml
  tasks:
    - include_tasks: ../httpd/install.yml
    - name: config httpd for runtime
      template:
        src: ../httpd/files/vhosts.conf.j2
        dest: "{{ install_dir }}/conf/extra/vhosts.conf"
      notify:
        - restart httpd for runtime
    - name: include extra for runtime
      lineinfile:
        line: Include conf/extra/vhosts.conf
        path: "{{ install_dir }}/conf/httpd.conf"
        state: present
      notify:
        - restart httpd for runtime
    - name: Provide web site for runtime
      copy:
        src: files/game
        dest: "{{ install_dir }}/htdocs"
    - name: close firewalld for runtime
      service:
        name: firewalld
        state: stopped
        enabled: no
    - name: start service for runtime
      service:
        name: httpd
        state: started
        enabled: yes

  handlers:
    - name: restart httpd for runtime
      service:
        name: httpd
        state: restarted      
[root@129a runtime]# 

18. 结构

[root@129a opt]# tree
.
`-- playbook
    |-- httpd
    |   |-- ansible.cfg
    |   |-- files
    |   |   |-- CentOS-Base.repo
    |   |   |-- game
    |   |   |   |-- #U670d#U52a1#U5668#U4e4b#U5bb6.url
    |   |   |   |-- #U7cbe#U54c1#U514d#U8d39#U5546#U4e1a#U6e90#U7801#U4e0b#U8f7d.url
    |   |   |   |-- css
    |   |   |   |   |-- font.css
    |   |   |   |   `-- style.css
    |   |   |   |-- fonts
    |   |   |   |   |-- OPPOSans-B.woff2
    |   |   |   |   |-- OPPOSans-M.woff2
    |   |   |   |   |-- OPPOSans-R.woff2
    |   |   |   |   `-- PangMenZhengDao.woff2
    |   |   |   |-- images
    |   |   |   |   |-- icon_APP.gif
    |   |   |   |   |-- icon_VR.gif
    |   |   |   |   |-- icon_WEB.gif
    |   |   |   |   |-- icon_bili.svg
    |   |   |   |   |-- icon_mail.svg
    |   |   |   |   |-- icon_mouse.svg
    |   |   |   |   |-- icon_mouse2.svg
    |   |   |   |   |-- icon_wechat.svg
    |   |   |   |   |-- lock.svg
    |   |   |   |   `-- vx.png
    |   |   |   |-- index.html
    |   |   |   |-- js
    |   |   |   |   |-- clipboard.min.js
    |   |   |   |   |-- jquery.SuperSlide.2.1.1.js
    |   |   |   |   |-- jquery.movebg.js
    |   |   |   |   |-- respond.min.js
    |   |   |   |   `-- uaredirect.js
    |   |   |   `-- m
    |   |   |       |-- css
    |   |   |       |   `-- style.css
    |   |   |       `-- index.html
    |   |   |-- httpd.service.j2
    |   |   |-- install.sh.j2
    |   |   |-- package
    |   |   |   |-- apr-1.7.0.tar.gz
    |   |   |   |-- apr-util-1.6.1.tar.gz
    |   |   |   `-- httpd-2.4.53.tar.gz
    |   |   `-- vhosts.conf.j2
    |   |-- install.yml
    |   |-- inventory
    |   |-- main.yml
    |   `-- vars
    |       `-- apache.yml
    `-- runtime
        |-- ansible.cfg
        |-- files
        |   `-- game
        |       |-- #U670d#U52a1#U5668#U4e4b#U5bb6.url
        |       |-- #U7cbe#U54c1#U514d#U8d39#U5546#U4e1a#U6e90#U7801#U4e0b#U8f7d.url
        |       |-- css
        |       |   |-- font.css
        |       |   `-- style.css
        |       |-- fonts
        |       |   |-- OPPOSans-B.woff2
        |       |   |-- OPPOSans-M.woff2
        |       |   |-- OPPOSans-R.woff2
        |       |   `-- PangMenZhengDao.woff2
        |       |-- images
        |       |   |-- icon_APP.gif
        |       |   |-- icon_VR.gif
        |       |   |-- icon_WEB.gif
        |       |   |-- icon_bili.svg
        |       |   |-- icon_mail.svg
        |       |   |-- icon_mouse.svg
        |       |   |-- icon_mouse2.svg
        |       |   |-- icon_wechat.svg
        |       |   |-- lock.svg
        |       |   `-- vx.png
        |       |-- index.html
        |       |-- js
        |       |   |-- clipboard.min.js
        |       |   |-- jquery.SuperSlide.2.1.1.js
        |       |   |-- jquery.movebg.js
        |       |   |-- respond.min.js
        |       |   `-- uaredirect.js
        |       `-- m
        |           |-- css
        |           |   `-- style.css
        |           `-- index.html
        |-- inventory
        |-- main.yml
        `-- vars
            `-- apache.yml

22 directories, 68 files
[root@129a opt]#
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值