我是靠谱客的博主 平淡乌冬面,最近开发中收集的这篇文章主要介绍Ansible 使用playbook搭建 lamp架构Ansible playbook-lamp,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
Ansible 使用playbook搭建 lamp架构
- Ansible playbook-lamp
- 准备环境
- 传递yum源
- 创建httpd-vhosts.conf文件
- 构建lamp.yml
- 检查语法
- 空运行
- 运行
- 效果
Ansible playbook-lamp
准备环境
ip | 服务 | 主机名 |
---|---|---|
192.168.129.250 | ansible主控机 | master |
192.168.129.133 | httpd | httpd |
192.168.129.135 | mariadb | mysql |
192.168.129.137 | php | php |
传递yum源
[root@master lamp]# ansible all -m template -a 'src=/etc/yum.repos.d/CentOS-Base.repo dest=/etc/yum.repos.d/CentOS-Base.repo'
192.168.129.137 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"checksum": "4966466ad015ef3d2a3cc0b8252d43efbdcf2c94",
"dest": "/etc/yum.repos.d/CentOS-Base.repo",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"path": "/etc/yum.repos.d/CentOS-Base.repo",
"size": 2595,
"state": "file",
"uid": 0
}
192.168.129.133 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"checksum": "4966466ad015ef3d2a3cc0b8252d43efbdcf2c94",
"dest": "/etc/yum.repos.d/CentOS-Base.repo",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"path": "/etc/yum.repos.d/CentOS-Base.repo",
"secontext": "system_u:object_r:system_conf_t:s0",
"size": 2595,
"state": "file",
"uid": 0
}
192.168.129.135 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "4966466ad015ef3d2a3cc0b8252d43efbdcf2c94",
"dest": "/etc/yum.repos.d/CentOS-Base.repo",
"gid": 0,
"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
</Directory>
</VirtualHost>
构建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.son 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.1nlisten.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] *******************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************
ok: [192.168.129.135]
TASK [anzhuang mariadb] **********************************************************************************************
changed: [192.168.129.135]
TASK [auto start] ****************************************************************************************************
changed: [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=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.129.137 : ok=13 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
效果
最后
以上就是平淡乌冬面为你收集整理的Ansible 使用playbook搭建 lamp架构Ansible playbook-lamp的全部内容,希望文章能够帮你解决Ansible 使用playbook搭建 lamp架构Ansible playbook-lamp所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复