ansible playbook,yum部署lamp

1 主机配置

主机ip地址
server192.168.143.10
http192.168.143.30
mysql192.168.143.40
php192.168.143.50

2 结构

[root@master ~]# tree /opt/hy/
/opt/hy/
├── 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/hy/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.143.30:80>
359         DocumentRoot "/var/www/html/www1"
360         ServerName www.hy.com
361         ProxyRequests off
362         ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.143.50: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.143.50:9000

    - name: php
      lineinfile:
        path: /etc/php-fpm.d/www.conf
        regexp: '^listen.allowed_clients ='
        line: listen.allowed_clients = 192.168.143.50


6 lamp.yml

指定playbook执行目录,替换httpd配置文件并启动httpd
创建web访问目录,添加index.php测试文件,启动php服务

---
- name: httpd
  import_playbook: /opt/hy/web/apache/install.yml

- name: mysql
  inport_playbook: /opt/hy/databases/mysql/install.yml

- name: php
  import_playbook: /opt/hy/phpproject/php/install.yml

- hosts: httpd
  tasks:
    - name: httpd config
      template:
        src: /opt/hy/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 hy]# ansible-playbook lamp.yml 

PLAY [install httpd] *******************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************
Enter passphrase for key '/root/.ssh/id_rsa': 
ok: [192.168.143.30]

TASK [httpd is enabled] ****************************************************************************************************************
ok: [192.168.143.30]

PLAY [php] *****************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************
Enter passphrase for key '/root/.ssh/id_rsa': 
ok: [192.168.143.50]

TASK [config php] **********************************************************************************************************************
changed: [192.168.143.50]

TASK [php] *****************************************************************************************************************************
changed: [192.168.143.50]

PLAY [httpd] ***************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************
ok: [192.168.143.30]

TASK [httpd config] ********************************************************************************************************************
changed: [192.168.143.30]

TASK [start httpd] *********************************************************************************************************************
ok: [192.168.143.30]

PLAY [php] *****************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************
ok: [192.168.143.50]

TASK [index.php] ***********************************************************************************************************************
changed: [192.168.143.50]

TASK [index] ***************************************************************************************************************************
changed: [192.168.143.50]

PLAY RECAP *****************************************************************************************************************************
192.168.143.30          : ok=5    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.143.50            : ok=6    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


8 访问浏览器

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值