ansible playbook -lamp

ansible自动化运维(六)——ansible palybook部署lamp

ansible 使用playbook搭建 lamp架构

1.主机情况

主机ip地址功能
server192.168.58.10控制主机(ansible)
http192.168.58.20http
mysql192.168.58.30mysql
php192.168.58.40php

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   

8.访问

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值