编写Apache角色;使用源码安装
在files中下载扩展包和安装包
[root@localhost project ]# ls roles/httpd/files/
apr-1.6 .5 .tar.gz apr-util-1.6 .1 .tar.gz httpd-2.4 .46 .tar.gz pcre-8.40 .tar.gz
在taskstasks目录中写入main.yml文件
[root@localhost project ]
--
- name: Install environment
yum:
name: "{{ item }} "
state: present
loop:
- make
- gcc
- gcc-c++
- expat-devel
- wget
- name: Install apr package
copy:
src: ../files/apr-1.6.5.tar.gz
dest: /root/
notify:
- compile apr
- name: Install apr-util package
copy:
src: ../files/apr-util-1.6.1.tar.gz
dest: /root/
notify:
- compile apr-util
- name: Install pcre package
copy:
src: ../files/pcre-8.40.tar.gz
dest: /root/
notify:
- complie pcre
- name: Install Apache package
copy:
src: ../files/httpd-2.4.46.tar.gz
dest: /root/
notify:
- Complie Apache
- Changed configuration files
- Start Apache
- set up link
- name: unzip package
shell: "{{ item }} "
loop:
- tar -zxvf /root/apr-1.6.5.tar.gz
- tar -zxvf /root/apr-util-1.6.1.tar.gz
- tar -zxvf /root/pcre-8.40.tar.gz
- tar -zxvf /root/httpd-2.4.46.tar.gz
- name: Create user for Apache
user:
name: "{{ user_name }} "
state: present
create_home: no
system: yes
在handlers目录中写入main.yml文件
[root@localhost project ]
---
- name: compile apr
shell: "{{ item }} "
loop:
- ./configure --prefix=/usr/local/apr
- make && make install
args:
chdir: "{{ package_apr }} "
- name: compile apr-util
shell: "{{ item }} "
loop:
- ./configure --prefix=/usr/local/apr-util -with-apr=/usr/local/apr
- make && make install
args:
chdir: "{{ package_apr_util }} "
- name: complie pcre
shell: "{{ item }} "
loop:
- ./configure --prefix=/usr/local/pcre
- make && make install
args:
chdir: "{{ package_pcre }} "
- name: Complie Apache
shell: "{{ item }} "
loop:
- ./configure --prefix=/usr/local/httpd --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre
- make && make install
args:
chdir: "{{ package_httpd }} "
- name: Changed configuration files
shell: sed -i "s/#ServerName www.example.com:80/ServerName localhost:80/" httpd.conf
args:
chdir: /usr/local/httpd/conf
- name: set up link
shell: 'ln -s /usr/local/httpd/bin/apachectl /usr/local/bin/apachectl'
- name: Start Apache
shell: apachectl start
在vars目录中写变量文件
[root@localhost project ]
---
user_name: apache
package_apr: apr-1.6.5
package_apr_util: apr-util-1.6.1
package_pcre: pcre-8.40
package_httpd: httpd-2.4.46
写需要执行的playbook.ymlyml文件
[root@localhost project ]
---
- name: Install Httpd
hosts: all
roles:
- httpd
执行playbook.yml文件
[root@localhost project]# ansible-playbook playbook.yml
PLAY [Install Httpd] **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **
TASK [Gathering Facts] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **
ok: [client.example.com]
TASK [httpd : Install environment] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **
changed: [client.example.com] => (item=make)
changed: [client.example.com] => (item=gcc)
changed: [client.example.com] => (item=gcc-c++)
changed: [client.example.com] => (item=expat-devel)
changed: [client.example.com] => (item=wget)
............
执行完后,在浏览器页面访问
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步