Ansible源码安装Apache
准备两台主机
hostname | ip |
---|---|
Ansible | 192.168.200.152 |
httpd | 192.168.200.154 |
添加域名解析
[root@ansible ~]# 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免密登录
[root@ansible ~]# 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 root@ansible
The key's randomart image is:
+---[RSA 3072]----+
| o+B+.ooBX|
| o=..E+=**|
| .. o+oo.O|
| o o.B +.|
| + S = = |
| = . . o |
| +. . |
| oo |
| oo |
+----[SHA256]-----+
[root@ansible ansible]# ssh-copy-id root@192.168.200.154 #传递密钥
/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
root@192.168.200.154's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.200.154'"
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
[root@ansible ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz
[root@ansible ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.48.tar.gz
[root@localhost ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
[root@ansible ~]# mv apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.48.tar.gz /opt/packages/
编写主机变量文件 syb.yml文件
[root@localhost ~]# 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文件
[root@localhost ~]# 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文件
[root@ansible 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
在浏览器访问