playbook部署lamp架构
环境
主机 | IP |
---|
主控机 | 192.168.72.131 |
httpd(受控主机) | 192.168.72.132 |
mysql(受控主机) | 192.168.72.136 |
php(受控主机) | 192.168.72.135 |
结构树
[root@master ~]# tree /opt/xk/
/opt/xk/
├── ansible.cfg
├── base
│ └── files
│ ├── Centos-8.repo
│ └── CentOS-Base.repo
├── databases
│ └── mysql
│ └── install.yml
├── inventory
├── lamp.yml
├── phpproject
│ └── php
│ └── install.yml
└── web
└── apache
├── httpd.j2
└── install.yml
httpd
[root@master apache]# cp /etc/httpd/conf/httpd.conf /opt/xk/web/apache/httpd.j2
[root@master apache]# vim httpd.j2
166 <IfModule dir_module>
167 DirectoryIndex index.php index.html #添加index.php
168 </IfModule>
169
286 AddType application/x-compress .Z
287 AddType application/x-gzip .gz .tgz
288 AddType application/x-httpd-php .php #添加这两行
289 AddType application/x-httpd-php-source .phps #添加这两行
//在文本最后添加这几行
358 <VirtualHost 192.168.72.132:80>
359 DocumentRoot "/var/www/html/www1"
360 ServerName www.xx.com
361 ProxyRequests off
362 ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.72.135:9000/var/www/html/$1
363 <Directory "/var/www/html">
364 Options None
365 AllowOverride None
366 Order allow,deny
367 Allow from all
368 </Directory>
369 </VirtualHost>
370
//编写playbook
[root@master apache]# cat install.yml
---
- name: install httpd
hosts: httpd
tasks:
- name: httpd is enabled
service:
name: httpd
enabled: yes
state: started
mysql
---
- name: mariabd server is enabled
hosts: mysql
tasks:
- name: Install mariadb
yum:
name: mariadb , mariadb-server
state: present
- name: mariadb is enabled
service:
name: mariadb
enabled: yes
state: started
php
//编写playbook文件
[root@master php]# vim install.yml
---
- name: php config
hosts: php
tasks:
- name: config php
lineinfile:
path: /etc/php-fpm.d/www.conf
regexp: '^listen ='
line: listen = 192.168.72.135:9000
- name: php
lineinfile:
path: /etc/php-fpm.d/www.conf
regexp: '^listen.allowed_clients ='
line: listen.allowed_clients = 192.168.72.132
编写lamp.yml
---
- name: httpd
import_playbook: /opt/xk/web/apache/install.yml
- name: mysql
inport_playbook: /opt/xk/databases/mysql/install.yml
- name: php
import_playbook: /opt/xk/phpproject/php/install.yml
- hosts: httpd
tasks:
- name: httpd config
template:
src: /opt/xk/web/apache/httpd.j2
dest: /etc/httpd/conf/httpd.conf
- name: start httpd
service:
name: httpd
enabled: yes
state: started
- hosts: php
tasks:
- name: index.php
file:
path: /var/www/html/index.php
state: touch
- name: index
lineinfile:
path: /var/www/html/index.php
line: |
<?php
phpinfo();
?>
state: present
- name: start php
service:
name: php-fpm
state: started
enabled: yes
执行
[root@master xk]# ansible-playbook lamp.yml
PLAY [install httpd] *******************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************
Enter passphrase for key '/root/.ssh/id_rsa':
ok: [192.168.72.132]
TASK [httpd is enabled] ****************************************************************************************************************
ok: [192.168.72.132]
PLAY [php] *****************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************
Enter passphrase for key '/root/.ssh/id_rsa':
ok: [192.168.72.135]
TASK [config php] **********************************************************************************************************************
changed: [192.168.72.135]
TASK [php] *****************************************************************************************************************************
changed: [192.168.72.135]
PLAY [httpd] ***************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************
ok: [192.168.72.132]
TASK [httpd config] ********************************************************************************************************************
changed: [192.168.72.132]
TASK [start httpd] *********************************************************************************************************************
ok: [192.168.72.132]
PLAY [php] *****************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************
ok: [192.168.72.135]
TASK [index.php] ***********************************************************************************************************************
changed: [192.168.72.135]
TASK [index] ***************************************************************************************************************************
changed: [192.168.72.135]
PLAY RECAP *****************************************************************************************************************************
192.168.72.132 : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.72.135 : ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
访问