playbook部署lamp架构

14 篇文章 0 订阅
3 篇文章 0 订阅

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   


访问

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值