Ansible 使用playbook搭建 lamp架构

“group”: “root”,

“md5sum”: “d06fb7d5709727828bcaba7457ea673e”,

“mode”: “0644”,

“owner”: “root”,

“size”: 2595,

“src”: “/root/.ansible/tmp/ansible-tmp-1626768912.2277684-96978-23118136641273/source”,

“state”: “file”,

“uid”: 0

}

创建httpd-vhosts.conf文件

// 配置辅配置文件

[root@master lamp]# pwd

/root/lamp

[root@master lamp]# cat httpd-vhosts.conf

<VirtualHost *:80>

DocumentRoot “/var/www/html”

ServerName example.com

ProxyRequests Off

ProxyPassMatch ^/(.*.php)$ fcgi://192.168.129.137:9000/var/www/html/$1

<Directory “/var/www/html/”>

Options none

AllowOverride none

Require all granted

构建lamp.yml

[root@master lamp]# cat lamp.yml


  • name: guanbi selinux

hosts: all

tasks:

  • name: fnaghuoqiang

service:

name: firewalld

state: stopped

enabled: false

  • name: selinux

lineinfile:

path: /etc/selinux/config

regexp: “^SELINUX=”

line: “SELINUX=disabled”

  • name: apache

hosts: apache

tasks:

  • name: anzhang httpd

yum:

name: httpd

state: latest

  • name: auto atart

service:

name: httpd

state: started

enabled: true

  • name: php

hosts: php*

tasks:

  • name: anzhuang php*

yum:

name: php*

state: latest

  • name: auto start

service:

name: php-fpm.service

state: started

enabled: true

  • name: mariadb

hosts: mariadb

tasks:

  • name: anzhuang mariadb

yum:

name: mariadb-server

state: latest

  • name: auto start

service:

name: mariadb

state: started

enabled: true

  • name: peizhi httpd.conf

hosts: apache

tasks:

  • name: httpd.conf

copy:

src: /root/lamp/httpd-vhosts.conf

dest: /etc/httpd/conf.d/

  • name: huoquwenjian php

lineinfile:

path: /etc/httpd/conf/httpd.conf

regexp: " DirectoryIndex index.html"

line: " DirectoryIndex index.php index.html"

  • name: apache

hosts: apache

tasks:

  • name: mokuaikaiqi

lineinfile:

path: /etc/httpd/conf.modules.d/00-proxy.conf

regexp: “LoadModule proxy_module modules/mod_proxy.so”

line: “LoadModule proxy_module modules/mod_proxy.so\n LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so”

  • name: apache

hosts: apache

tasks:

  • name: tianjialeixing php

lineinfile:

path: /etc/httpd/conf/httpd.conf

regexp: " AddType application/x-gzip .gz .tgz"

line: " AddType application/x-gzip .gz .tgz \n AddType application x-httpd-php .php \n AddType application x-httpd-php-source.phps"

  • name: php

hosts: php

tasks:

  • name: tianjia duankou

lineinfile:

path: /etc/php-fpm.d/www.conf

regexp: “listen = /run/php-fpm/www.sock”

line: “;listen = /run/php-fpm/www.sock \n listen = 9000”

  • name: tianjaiindex.php

lineinfile:

path: /var/www/html/index.php

line: |

<?php phpinfo(); ?>

create: true

  • name: php

hosts: php

tasks:

  • name: zhiding apache

lineinfile:

path: /etc/php-fpm.d/www.conf

regexp: “listen.allowed_clients = 127.0.0.1”

line: “listen.allowed_clients = 127.0.0.1\nlisten.allowed_clients = 192.168.129.133”

  • name: restart httpd

hosts: apache

tasks:

  • name: restart httpd

service:

name: httpd

state: restarted

  • name: php

hosts: php

tasks:

  • name: restart php

service:

name: php-fpm.service

state: restarted

检查语法


[root@master lamp]# ansible-playbook --syntax-check lamp.yml

playbook: lamp.yml

空运行


[root@master lamp]# ansible-playbook -C lamp.yml

PLAY [guanbi selinux] ************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************

ok: [192.168.129.135]

ok: [192.168.129.137]

ok: [192.168.129.133]

TASK [fnaghuoqiang] **************************************************************************************************

ok: [192.168.129.135]

ok: [192.168.129.133]

ok: [192.168.129.137]

TASK [selinux] *******************************************************************************************************

ok: [192.168.129.133]

ok: [192.168.129.137]

ok: [192.168.129.135]

PLAY [apache] ********************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************

ok: [192.168.129.133]

TASK [anzhang httpd] *************************************************************************************************

ok: [192.168.129.133]

TASK [auto atart] ****************************************************************************************************

ok: [192.168.129.133]

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

TASK [Gathering Facts] ***********************************************************************************************

ok: [192.168.129.137]

TASK [anzhuang php*] *************************************************************************************************

ok: [192.168.129.137]

TASK [auto start] ****************************************************************************************************

ok: [192.168.129.137]

PLAY [mariadb] *******************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************

ok: [192.168.129.135]

TASK [anzhuang mariadb] **********************************************************************************************

ok: [192.168.129.135]

TASK [auto start] ****************************************************************************************************

ok: [192.168.129.135]

PLAY [peizhi httpd.conf] *********************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************

ok: [192.168.129.133]

TASK [httpd.conf] ****************************************************************************************************

ok: [192.168.129.133]

TASK [huoquwenjian php] **********************************************************************************************

ok: [192.168.129.133]

PLAY [apache] ********************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************

ok: [192.168.129.133]

TASK [mokuaikaiqi] ***************************************************************************************************

changed: [192.168.129.133]

PLAY [apache] ********************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************

ok: [192.168.129.133]

TASK [tianjialeixing php] ********************************************************************************************

changed: [192.168.129.133]

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

TASK [Gathering Facts] ***********************************************************************************************

ok: [192.168.129.137]

TASK [tianjia duankou] ***********************************************************************************************

changed: [192.168.129.137]

TASK [tianjaiwenjain php] ********************************************************************************************

ok: [192.168.129.137]

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

TASK [Gathering Facts] ***********************************************************************************************

ok: [192.168.129.137]

TASK [zhiding apache] ************************************************************************************************

changed: [192.168.129.137]

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

TASK [Gathering Facts] ***********************************************************************************************

ok: [192.168.129.133]

TASK [restart httpd] *************************************************************************************************

changed: [192.168.129.133]

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

TASK [Gathering Facts] ***********************************************************************************************

ok: [192.168.129.137]

TASK [restart php] ***************************************************************************************************

changed: [192.168.129.137]

PLAY RECAP ***********************************************************************************************************

192.168.129.133 : ok=15 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

192.168.129.135 : ok=6 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

192.168.129.137 : ok=13 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

运行


[root@master lamp]# ansible-playbook lamp.yml

PLAY [guanbi selinux] ************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************

ok: [192.168.129.135]

ok: [192.168.129.137]

ok: [192.168.129.133]

TASK [fnaghuoqiang] **************************************************************************************************

ok: [192.168.129.137]

ok: [192.168.129.133]

ok: [192.168.129.135]

TASK [selinux] *******************************************************************************************************

ok: [192.168.129.135]

ok: [192.168.129.133]

ok: [192.168.129.137]

PLAY [apache] ********************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************

ok: [192.168.129.133]

TASK [anzhang httpd] *************************************************************************************************

ok: [192.168.129.133]

TASK [auto atart] ****************************************************************************************************

ok: [192.168.129.133]

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

TASK [Gathering Facts] ***********************************************************************************************

ok: [192.168.129.137]

TASK [anzhuang php*] *************************************************************************************************

changed: [192.168.129.137]

TASK [auto start] ****************************************************************************************************

changed: [192.168.129.137]

PLAY [mariadb] *******************************************************************************************************

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值