Ansible源碼安裝Apache
準備兩台主機
hostname | ip |
---|---|
Ansible | 192.168.200.152 |
httpd | 192.168.200.154 |
添加域名解析
[[email protected] ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.154 httpd
配置ssh免密登入
[[email protected] ~]# ssh-keygen -t rsa #生成密鑰,指定加密方式 ,下面預設一路回車即可
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:om7plGVNJSdZEXg3cJekHj8L1cbKyNHldhQxXxfWwko [email protected]
The key's randomart image is:
+---[RSA 3072]----+
| o+B+.ooBX|
| o=..E+=**|
| .. o+oo.O|
| o o.B +.|
| + S = = |
| = . . o |
| +. . |
| oo |
| oo |
+----[SHA256]-----+
[[email protected] ansible]# ssh-copy-id [email protected] #傳遞密鑰
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.200.154 (192.168.200.154)' can't be established.
ECDSA key fingerprint is SHA256:mL+TNgpGPB93O2IhR/URB2fR9AOOJa9/b2UwjjqzjHg.
Are you sure you want to continue connecting (yes/no/[fingerprint])? 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
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
下載下傳源碼包
apr源碼包位址: https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz
httpd源碼包位址: https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.48.tar.gz
apr_util源碼包位址: https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
[[email protected] ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz
[[email protected] ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.48.tar.gz
[[email protected] ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
[[email protected] ~]# mv apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.48.tar.gz /opt/packages/
編寫主機變量檔案 syb.yml檔案
[[email protected] ~]# vim /opt/host_vars/syb.yml
---
# 編譯工具
tools: gcc,gcc-c++,perl,perl-devel,expat-devel,pcre-devel,pcre
# 編譯安裝apr
apr_install: " cd /root/apr-1.7.0/ && ./configure --prefix=/usr/local/apr && make && make install "
# 編譯安裝apr-util
apr_util_intall: " cd /root/apr-util-1.6.1/ && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install "
# 編譯安裝httpd
httpd_install: " cd /root/httpd-2.4.48/ && ./configure --prefix=/usr/local/httpd --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util && make && make install "
編寫源碼安裝apache檔案httpd.yml檔案
[[email protected] ~]# vim /opt/playbook/httpd.yml
---
- hosts: httpd
vars_files:
- /opt/host_vars/syb.yml # 指定變量檔案
tasks:
- name: install server tools # 安裝編譯工具
yum:
name: "{{tools}}"
state: present
- name: copy package apr # 解壓壓縮包,上傳到受管主機
unarchive:
src: /opt/packages/apr-1.7.0.tar.gz
dest: /root/
copy: yes
- name: copy package apr-unil
unarchive:
src: /opt/packages/apr-util-1.6.1.tar.gz
dest: /root/
copy: yes
- name: copy package httpd
unarchive:
src: /opt/packages/httpd-2.4.48.tar.gz
dest: /root/
copy: yes
- name: create group apache # 建立Apache使用的使用者群組
group:
name: apache
system: yes
state: present
- name: cerate user apache
user:
name: apache
system: yes
state: present
- name: install apr
shell: "{{apr_install }}" # 運作變量
- name: install apr-util
shell: "{{apr_util_install }}"
- name: install httpd
shell: "{{httpd_install }}"
- name: start httpd service # 開啟服務
shell: "/usr/local/httpd/bin/apachectl start"
執行playbook檔案
[[email protected] ansible]# ansible-playbook /opt/playbook/httpd.yml
PLAY [httpd] *******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.200.154]
TASK [install server tools] ****************************************************
ok: [192.168.200.154]
TASK [copy package apr] ********************************************************
ok: [192.168.200.154]
TASK [copy package apr-unil] ***************************************************
ok: [192.168.200.154]
TASK [copy package httpd] ******************************************************
ok: [192.168.200.154]
TASK [create group apache] *****************************************************
ok: [192.168.200.154]
TASK [cerate user apache] ******************************************************
ok: [192.168.200.154]
TASK [install apr] *************************************************************
changed: [192.168.200.154]
TASK [intall apr-util] *********************************************************
changed: [192.168.200.154]
TASK [install httpd] ***********************************************************
changed: [192.168.200.154]
TASK [start httpd service] *****************************************************
changed: [192.168.200.154]
PLAY RECAP *********************************************************************
192.168.200.154 : ok=11 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
在浏覽器通路
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIiclRnblN2XjlGcjcmbw5CO1MzY0QjYlVjZ3MTMjFTYwUzMhdzNwcTY4MTMiNWNl9CX0JXZ252bj91Ztl2Lc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)