Ansible 使用playbook编译安装apache
1.关闭防火墙
//关闭防火墙
---
- hosts: apache
tasks:
- name: stop firewalld
service:
name: firewalld
state: stopped
- name:
lineinfile:
path: /etc/selinux/config
regexp: "^SELINUX="
line: "SELINUX=disabled"
- name:
shell:
setenforce 0
//执行playbook
[root@localhost opt]
PLAY [all] *********************************************************************
skipping: no hosts matched
PLAY RECAP ******************************************************************
2.拉取apache软件包
//拉去软件包
[root@localhost opt]
---
- hosts: apache
tasks:
- name:
get_url:
url: https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
dest: /root/
- name:
get_url:
url: https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.48.tar.gz
dest: /root/
- name:
get_url:
url: https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz
dest: /root/
[root@localhost opt]
PLAY [apache] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.200.145]
TASK [get_url] *****************************************************************
changed: [192.168.200.145]
TASK [get_url] *****************************************************************
changed: [192.168.200.145]
TASK [get_url] *****************************************************************
changed: [192.168.200.145]
PLAY RECAP *********************************************************************
192.168.200.145 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
//受控主机
[root@localhost ~]
anaconda-ks.cfg apr-util-1.6.1.tar.gz
apr-1.7.0.tar.gz httpd-2.4.48.tar.gz
3.安装依赖环境编写变量
//编写环境变量
[root@localhost opt]
.
├── ansible.cfg
├── apache.yml
├── group_vars
│ ├── apache
│ └── mysql
├── host_vars
│ ├── 192.168.200.145
│ └── 192.168.200.147
├── inventory
├── lamp
│ ├── firewalld.yml
│ ├── httpd.yml
│ ├── mysql.yml
│ └── php.yml
├── playbook
│ ├── auto.yml
│ ├── test.yml
│ └── user.yml
└── vars_file
├── httpd.yml
└── line.yml
//编写变量
[root@localhost opt]
user: apache
packages:
- openssl-devel
- pcre-devel
- expat-devel
- libtool
- gcc
- gcc-c++
- make
- pcre
- perl-devel
- perl
- '@Development Tools'
apr: " cd /root/apr-1.7.0/ && ./configure --prefix=/usr/local/apr && make && make install "
apr_util: " cd /root/apr-util-1.6.1/ && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install "
httpd: " 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 "
4.编写playbook
---
- hosts: apache
vars_files:
- vars_file/httpd.yml
tasks:
- name: create user
user:
name: "{{ user }}"
shell: sbin/nologin
state: present
- name: Installation dependency environment
yum:
name: "{{ packages }}"
state: present
- name: uncompress app
unarchive:
src: /root/apr-1.7.0.tar.gz
dest: /root/
copy: no
- name: uncompress app
unarchive:
src: /root/apr-util-1.6.1.tar.gz4
dest: /root/
copy: no
- name: uncompress app
unarchive:
src: /root/httpd-2.4.48.tar.gz
dest: /root/
copy: no
- name: config
lineinfile:
path: /root/apr-1.7.0/configure
regexp: '^$RM "$cfgfile"'
line: '# $RM "$cfgfile"'
- name: make
shell: "{{ apr}}"
- name: make2
shell: "{{ apr_util }}"
- name: make3
shell: "{{ httpd }}"
//运行
[root@localhost opt]
PLAY [apache] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.200.145]
TASK [create user] *************************************************************
changed: [192.168.200.145]
TASK [Installation dependency environment] *************************************
changed: [192.168.200.145]
TASK [uncompress app] **********************************************************
changed: [192.168.200.145]
TASK [uncompress app] **********************************************************
changed: [192.168.200.145]
TASK [uncompress app] **********************************************************
changed: [192.168.200.145]
TASK [config] ******************************************************************
changed: [192.168.200.145]
TASK [make] ********************************************************************
changed: [192.168.200.145]
TASK [make2] *******************************************************************
changed: [192.168.200.145]
TASK [make3] *******************************************************************
changed: [192.168.200.145]
PLAY RECAP *********************************************************************
192.168.200.145 : ok=10 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
//启动apache
[root@localhost opt]
192.168.200.145 | CHANGED | rc=0 >>
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
5.访问