ansible自动化运维(六)——ansible palybook部署lamp
ansible 使用playbook搭建 lamp架构
1.主机情况
主机 | ip地址 | 功能 |
---|---|---|
server | 192.168.58.10 | 控制主机(ansible) |
http | 192.168.58.20 | http |
mysql | 192.168.58.30 | mysql |
php | 192.168.58.40 | php |
2.结构树
[root@master ~]# tree /opt/xm/
/opt/xm/
├── 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
3.http
copy原配置文件到apache执行目录中去并进行修改
[root@server apache]# cp /etc/httpd/conf/httpd.conf /opt/xm/web/apache/httpd.j2
[root@server 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.58.20:80>
359 DocumentRoot "/var/www/html/www1"
360 ServerName www.xm.com
361 ProxyRequests off
362 ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.58.40: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@server apache]# cat install.yml
---
- name: install httpd
hosts: httpd
tasks:
-name: httpd is start
service:
name: httpd
state: started
- name: httpd is enabled
service:
name: httpd
enabled: yes
4.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
5.php
通过lineinfile模块对php-fpm配置文件进行修改
//编写playbook文件
[root@server php]# vim install.yml
---
- name:
hosts: php
tasks:
- name: config php
lineinfile:
path: /etc/php-fpm.d/www.conf
regexp: '^listen ='
line: listen = 192.168.58.40:9000
- name: php
lineinfile:
path: /etc/php-fpm.d/www.conf
regexp: '^listen.allowed_clients ='
line: listen.allowed_clients = 192.168.58.40
6.lamp.yml
指定playbook执行目录,替换httpd配置文件并启动httpd
创建web访问目录,添加index.php测试文件
最后启动php服务
---
- name: httpd
import_playbook: /opt/xm/web/apache/install.yml
- name: mysql
inport_playbook: /opt/xm/databases/mysql/install.yml
- name: php
import_playbook: /opt/xm/phpproject/php/install.yml
- hosts: httpd
tasks:
- name: httpd config
template:
src: /opt/xm/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
7.执行lamp
[root@server xm]# ansible-playbook lamp.yml
PLAY [install httpd] *******************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************
Enter passphrase for key '/root/.ssh/id_rsa':
ok: [192.168.58.20]
TASK [httpd is enabled] ****************************************************************************************************************
ok: [192.168.58.20]
PLAY [php] *****************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************
Enter passphrase for key '/root/.ssh/id_rsa':
ok: [192.168.58.40]
TASK [config php] **********************************************************************************************************************
changed: [192.168.58.40]
TASK [php] *****************************************************************************************************************************
changed: [192.168.58.40]
PLAY [httpd] ***************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************
ok: [192.168.58.20]
TASK [httpd config] ********************************************************************************************************************
changed: [192.168.58.20]
TASK [start httpd] *********************************************************************************************************************
ok: [192.168.58.20]
PLAY [php] *****************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************
ok: [192.168.58.40]
TASK [index.php] ***********************************************************************************************************************
changed: [192.168.58.40]
TASK [index] ***************************************************************************************************************************
changed: [192.168.58.40]
PLAY RECAP *****************************************************************************************************************************
192.168.58.20 : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.58.40 : ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0