文章目录

  • 使用角色部署lamp架构
  • 部署apache
    • 编写任务
    • 编写脚本
    • 配置变量
    • 配置模板
    • 编写playbook执行
  • 部署mysql
    • 编写任务
    • 配置变量
    • 编写脚本
    • 编写模板
    • 编写playbook执行
  • 部署PHP
    • 编写任务
    • 配置变量
    • 编写脚本
    • 编写模板
    • 编写playbook执行
  • 浏览器访问测试

使用角色部署lamp架构

[root@ansible ansible]# cd roles/
[root@ansible roles]# ansible-galaxy init php
- Role php was created successfully
[root@ansible roles]# ansible-galaxy init apache
- Role apache was created successfully
[root@ansible roles]# ansible-galaxy init mysql
- Role mysql was created successfully

部署apache

编写任务

[root@ansible roles]# cd apache/
[root@ansible apache]# vim tasks/main.yml
---
# tasks file for apache
- name: set yumscript: yum.sh- name: install packagesyum:name: "{{ httpd_pack }}"state: present- name: unzip aprunarchive:src: apr-1.6.5.tar.bz2dest: /usr/src/- name: install aprscript: apr.sh- name: unzip apr-utilunarchive:src: apr-util-1.6.1.tar.bz2dest: /usr/src/- name: install apr-utilscript: apr-util.sh- name: unzip httpdunarchive:src: httpd-2.4.54.tar.bz2dest: /usr/src/- name: install httpdscript: httpd.sh- name: create useruser:name: apachesystem: yescreate_home: noshell: /sbin/nologinstate: present- name: set httpd servicetemplate:src: httpd.service.j2dest: /usr/lib/systemd/system/httpd.service- name: refreshshell:cmd: systemctl daemon-reload- name: start httpd serviceservice:name: httpdstate: startedenabled: yes- name: stop firewalldservice:name: firewalldstate: stoppedenabled: no- name: stop selinuxlineinfile:path: /etc/selinux/configregexp: '^SELINUX='line: SELINUX=disabled- name: stop selinux1shell:cmd: setenforce 0

编写脚本

[root@ansible apache]# vim files/yum.sh
#!/bin/bash
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
yum reinstall -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*[root@ansible apache]# vim files/apr.sh
#!/bin/bash
cd /usr/src/apr-1.6.5
sed -i '/$RM "$cfgfile"/d' configure
./configure --prefix=/usr/local/apr
make
make install[root@ansible apache]# vim files/apr-util.sh
#!/bin/bash
cd /usr/src/apr-util-1.6.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make
make install[root@ansible apache]# vim files/httpd.sh
#!/bin/bash
cd /usr/src/httpd-2.4.54
./configure --prefix=/usr/local/apache \--sysconfdir=/etc/http24 \--enable-so \--enable-ssl \--enable-cgi \--enable-rewrite \--with-zlib \--with-pcre \--with-apr=/usr/local/apr \--with-apr-util=/usr/local/apr-util \--enable-modules=most \--enable-mpms-shared=all \--with-mpm=preforkmake
make install
echo "export PATH=$PATH:/usr/local/apache/bin" > /etc/profile.d/httpd.sh
source /etc/profile.d/httpd.sh
echo "MANDATORY_MANPATH                       /usr/local/apache/man" >>/etc/man_db.conf
ln -s /usr/local/apache/include/ /usr/include/httpd

配置变量

[root@ansible apache]# vim vars/main.yml
---
# vars file for apache
httpd_pack:- openssl-devel- pcre-devel- expat-devel- libtool- gcc- gcc-c++- vim- bzip2- wget- make

配置模板

[root@ansible apache]# vim templates/httpd.service.j2
[Unit]
Description=apache server daemon
After=network.target sshd-keygen.target[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP $MAINPID[Install]
WantedBy=multi-user.target

编写playbook执行

[root@ansible ansible]# vim httpd.yml
---
- name: install apachehosts: node1roles:- apache[root@ansible ansible]# ansible-playbook httpd.ymlPLAY [install apache] ****************************************************************************************************************************************************************************************TASK [Gathering Facts] ***************************************************************************************************************************************************************************************
ok: [node1]TASK [apache : set yum] **************************************************************************************************************************************************************************************
changed: [node1]TASK [apache : install packages] *****************************************************************************************************************************************************************************
ok: [node1]TASK [apache : unzip apr] ************************************************************************************************************************************************************************************
changed: [node1]TASK [apache : install apr] **********************************************************************************************************************************************************************************
changed: [node1]TASK [apache : unzip apr-util] *******************************************************************************************************************************************************************************
ok: [node1]TASK [apache : install apr-util] *****************************************************************************************************************************************************************************
changed: [node1]TASK [apache : unzip httpd] **********************************************************************************************************************************************************************************
changed: [node1]TASK [apache : install httpd] ********************************************************************************************************************************************************************************
changed: [node1]TASK [apache : create user] **********************************************************************************************************************************************************************************
ok: [node1]TASK [apache : set httpd service] ****************************************************************************************************************************************************************************
ok: [node1]TASK [apache : refresh] **************************************************************************************************************************************************************************************
changed: [node1]TASK [apache : start httpd service] **************************************************************************************************************************************************************************
ok: [node1]TASK [apache : stop firewalld] *******************************************************************************************************************************************************************************
ok: [node1]TASK [apache : stop selinux] *********************************************************************************************************************************************************************************
changed: [node1]TASK [apache : stop selinux1] ********************************************************************************************************************************************************************************
changed: [node1]PLAY RECAP ***************************************************************************************************************************************************************************************************
node1                      : ok=16   changed=9    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0[root@node1 ~]# ss -antl
LISTEN                  0                       128                                                    *:80                                                  *:*

部署mysql

编写任务

[root@ansible roles]# cd mysql/
[root@ansible mysql]# vim tasks/main.yml
---
# tasks file for mysql
- name: install packagesyum:name: "{{ mysql_pack }}"state: present- name: unzipunarchive:src: mysql-5.7.38-linux-glibc2.12-x86_64.tar.gzdest: /opt/- name: create useruser:name: mysqlsystem: yescreate_home: noshell: /sbin/nologinstate: present- name: create linkfile:path: /opt/mysqlsrc: /opt/mysql-5.7.38-linux-glibc2.12-x86_64owner: mysqlgroup: mysqlstate: link- name: create directoryfile:path: /opt/mysql_dataowner: mysqlgroup: mysqlstate: directory- name: mysql initialscript: initial.sh- name: cp my.cnftemplate:src: my.cnf.j2dest: /etc/my.cnf- name: scriptscript: script.sh- name: refreshshell:cmd: systemctl daemon-reload- name: restart mysqldservice:name: mysqldstate: startedenabled: yes- name: set mysql passwdshell:cmd: /opt/mysql/bin/mysql -uroot -e "set password = password('123456')"

配置变量

[root@ansible mysql]# vim vars/main.yml
---
# vars file for mysql
mysql_pack:- ncurses-devel- openssl- cmake- ncurses-compat-libs

编写脚本

[root@ansible mysql]# vim files/script.sh
#!/bin/bash
cp -a /opt/mysql/support-files/mysql.server /etc/init.d/mysqld
cat >> /etc/init.d/mysqld <<EOF
basedir=/opt/mysql
datadir=/opt/mysql_data
EOF[root@ansible mysql]# vim files/initial.sh
#!/bin/bash
echo 'export PATH=$PATH:/opt/mysql/bin' >/etc/profile.d/mysql.sh
ln -s /opt/mysql/include  /usr/include/mysql
echo '/opt/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
echo "MANDATORY_MANPATH                       /opt/mysql/man" >> /etc/man_db.conf
source /etc/profile.d/mysql.sh
/opt/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/mysql_data/

编写模板

[root@ansible mysql]# vim templates/my.cnf.j2
[mysqld]
basedir = /opt/mysql
datadir = /opt/mysql_data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/mysql_data/mysql.pid
log-error=/var/log/mysqld.log
user = mysql
skip-name-resolve[root@ansible mysql]# vim templates/mysqld.service.j2
[Unit]
Description=mysqld server daemon
After=network.target sshd-keygen.target[Service]
Type=forking
ExecStart=/etc/init.d/mysqld start
ExecStop=/etc/init.d/mysqld stop
ExecReload=/bin/kill -HUP $MAINPID[Install]
WantedBy=multi-user.target

编写playbook执行

[root@ansible ansible]# vim mysql.yml
---
- name: install mysqlhosts: node1roles:- mysql[root@ansible ansible]# ansible-playbook mysql.ymlPLAY [install mysql] *****************************************************************************************************************************************************************************************TASK [Gathering Facts] ***************************************************************************************************************************************************************************************
ok: [node1]TASK [mysql : install packages] ******************************************************************************************************************************************************************************
ok: [node1]TASK [mysql : unzip] *****************************************************************************************************************************************************************************************
changed: [node1]TASK [mysql : create user] ***********************************************************************************************************************************************************************************
ok: [node1]TASK [mysql : create link] ***********************************************************************************************************************************************************************************
changed: [node1]TASK [mysql : create directory] ******************************************************************************************************************************************************************************
changed: [node1]TASK [mysql initial] *****************************************************************************************************************************************************************************************
changed: [node1]TASK [mysql : cp my.cnf] *************************************************************************************************************************************************************************************
ok: [node1]TASK [mysql : script] ****************************************************************************************************************************************************************************************
changed: [node1]TASK [mysql : refresh] ***************************************************************************************************************************************************************************************
changed: [node1]TASK [restart mysqld] ****************************************************************************************************************************************************************************************
changed: [node1]TASK [set mysql passwd] **************************************************************************************************************************************************************************************
changed: [node1]PLAY RECAP ***************************************************************************************************************************************************************************************************
node1                      : ok=12   changed=8    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

部署PHP

编写任务

[root@ansible roles]# cd php/
[root@ansible php]# vim tasks/main.yml
---
# tasks file for php
- name: install packagesyum:name: "{{ php_pack }}"state: present- name: unzipunarchive:src: php-7.4.30.tar.xzdest: /usr/src- name: install phpscript: php.sh- name: cp templatetemplate:src: php-fpm.service.j2dest: /usr/lib/systemd/system/php-fpm.service- name: refreshshell:cmd: systemctl daemon-reload- name: set php serviceservice:name: php-fpmstate: startedenabled: yes- name: modify apache configreplace:path: /etc/http24/httpd.confregexp: "index.html"replace: "index.php index.html"- name: rm fileshell:cmd: rm -rf /usr/local/apache/htdocs/index.html- name: cp index.phptemplate:src: index.php.j2dest: /usr/local/apache/htdocs/index.phpowner: apachegroup: apache- name: restart httpdservice:name: httpdstate: restarted

配置变量

[root@ansible php]# vim vars/main.yml
---
# vars file for php
php_pack:- openssl- openssl-devel- bzip2-devel- libcurl- libcurl-devel- libicu-devel- libjpeg- libjpeg-devel- libpng- libpng-devel- openldap-devel- freetype- freetype-devel- gmp- gmp-devel- libmcrypt- libmcrypt-devel- readline- readline-devel- libxslt- libxslt-devel- mhash- mhash-devel- php-mysqlnd- sqlite-devel- libzip-devel- libxml2-devel- pcre-devel- http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm

编写脚本

[root@ansible php]# vim files/php.sh
#!/bin/bash
cd /usr/src/php-7.4.30
./configure --prefix=/usr/local/php7 \--with-config-file-path=/etc \--enable-fpm \--enable-inline-optimization \--disable-debug \--disable-rpath \--enable-shared \--enable-soap \--with-openssl \--enable-bcmath \--with-iconv \--with-bz2 \--enable-calendar \--with-curl \--enable-exif \--enable-ftp \--enable-gd \--with-jpeg \--with-zlib-dir \--with-freetype \--with-gettext \--enable-json \--enable-mbstring \--enable-pdo \--with-mysqli=mysqlnd \--with-pdo-mysql=mysqlnd \--with-readline \--enable-shmop \--enable-simplexml \--enable-sockets \--with-zip \--enable-mysqlnd-compression-support \--with-pear \--enable-pcntl \--with-apxs2=/usr/local/apache/bin/apxs \--enable-posixmake
make installecho "export PATH=$PATH:/usr/local/php7/bin" > /etc/profile.d/php.sh
source /etc/profile.d/php.sh
ln -s /usr/local/php7/include/ /usr/include/php
echo "/usr/local/php7/lib" > /etc/ld.so.conf.d/php.conf
ldconfigcp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.confecho "AddType application/x-httpd-php .php" >>/etc/http24/httpd.conf
echo "AddType application/x-httpd-php-source .phps" >>/etc/http24/httpd.confsed -i '/proxy_module/s/#//g' /etc/http24/httpd.conf
sed -i '/proxy_fcgi_module/s/#//g' /etc/http24/httpd.conf

编写模板

[root@ansible php]# vim templates/php-fpm.service.j2
[Unit]
Description=php-fpm server daemon
After=network.target sshd-keygen.target[Service]
Type=forking
ExecStart=/etc/init.d/php-fpm start
ExecStop=/etc/init.d/php-fpm stop
ExecReload=/bin/kill -HUP $MAINPID[Install]
WantedBy=multi-user.target[root@ansible php]# vim templates/index.php.j2
<?php
phpinfo();
?>

编写playbook执行

[root@ansible ansible]# vim php.yml
---
- name: install phphosts: node1roles:- php[root@ansible ansible]# ansible-playbook php.ymlPLAY [install php] *******************************************************************************************************************************************************************************************TASK [Gathering Facts] ***************************************************************************************************************************************************************************************
ok: [node1]TASK [php : install packages] ********************************************************************************************************************************************************************************
changed: [node1]TASK [php : unzip] *******************************************************************************************************************************************************************************************
changed: [node1]TASK [install php] *******************************************************************************************************************************************************************************************
changed: [node1]TASK [php : cp template] *************************************************************************************************************************************************************************************
ok: [node1]TASK [php : refresh] *****************************************************************************************************************************************************************************************
changed: [node1]TASK [set php service] ***************************************************************************************************************************************************************************************
changed: [node1]TASK [php : modify apache config] ****************************************************************************************************************************************************************************
changed: [node1]TASK [php : rm file] *****************************************************************************************************************************************************************************************
[WARNING]: Consider using the file module with state=absent rather than running 'rm'.  If you need to use command because file is insufficient you can add 'warn: false' to this command task or set
'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [node1]TASK [cp index.php] ******************************************************************************************************************************************************************************************
changed: [node1]TASK [php : restart httpd] ***********************************************************************************************************************************************************************************
changed: [node1]PLAY RECAP ***************************************************************************************************************************************************************************************************
node1                      : ok=11   changed=9    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

浏览器访问测试

Ansible使用角色部署LAMP架构相关推荐

  1. ansible角色部署lamp架构

    使用ansible角色部署lamp架构 文章目录 使用ansible角色部署lamp架构 一.部署Apache 1.配置主机并创建角色 2.编写task任务 3.编写脚本 4.调用角色 二.部署mys ...

  2. ansible部署LAMP架构

    简介 ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统配置.批量程序部署.批量 ...

  3. LAMP源码编译安装及一键部署LAMP架构

    文章目录 一.什么是LAMP? 1.LAMP平台概述 2.构建LAMP平台顺序 3.编译安装的优点 4.各组件的主要作用 二.LAMP架构流向 三.编译安装Apache服务 1.先关闭防火墙 2.将软 ...

  4. 如何部署LAMP架构

    LAMP平台的构成组件 一. linux操作系统LAMP架构的基础,Apache网站服务器:LAMP架构的前端,MySQL:LAPM架构后端,PHP/Per/Python网页编程语言:负责解析动态网页 ...

  5. Saltstack练手之部署lamp架构其一: 实现效果

    文章目录 前言 项目总结构 apache安装 mysql安装 php安装 配置apache 前言 一个优秀的状态文件不是一次就能写出来的,而是经过不断的修改优化,但是我们写的时候要有,解耦,模块,幂等 ...

  6. ansible模块独立部署LAMP

    前言:一般部署lamp是用源码安装的方式部署,ansible实现源码安装lamp一般是写入xxx.yml,但是由于它喵的老睡不着,就准备多熟悉熟悉ansible模块,于是就有了下面的基于模块安装的la ...

  7. 通过Playbook部署LAMP(5)

    title: 通过Playbook部署LAMP(5) date: 2018-12-03 13:24:07 tags: Ansible categories: Ansible copyright: tr ...

  8. Apache web服务器(LAMP架构)

    apache介绍 1).世界上使用率最高的网站服务器,最高时可达70%:官方网站:apache.org 2).http 超文本协议 HTML 超文本标记语言 3).URL 统一资源定位符 http:/ ...

  9. 基于ansible Role实现批量部署lamp平台

    一.ansible Role介绍 # ansilbe自1.2版本引入的新特性,用于层次性.结构化地组织playbook. # roles能够根据层次型结构自动装载变量文件.tasks以及handler ...

最新文章

  1. .Net 转战 Android 4.4 日常笔记(7)--apk的打包与反编译
  2. Cissp-【第3章 安全工程】-2021-2-22(269页-289页)
  3. java函数式编程_Java 函数式编程和 lambda 表达式详解
  4. AtCoder Regular Contest 082
  5. scrapy 动态IP、随机UA、验证码
  6. oracle 查看动态性能视图,Oracle 中的V$ 动态性能视图
  7. JSON(1)--- 语法
  8. 零起点英语_【德国零起点】05—变元音字母
  9. Linux中的常用命令
  10. [ NOI 2001 ] 食物链
  11. linux下JMeter安装
  12. Flash MX 2004 帮助CHM 在线版
  13. JS地图经纬度正则表达式校验
  14. Advanced Installer Architect创作工具
  15. puzzle(0151)《24点》
  16. 鸭梨笔记本上市!!!超越苹果和微软!!
  17. Python使用QQ邮箱,实现自动发送邮件
  18. echarts图表宽度为百分比出现的问题
  19. MFC Windows 程序设计[325]之表格控件例程(附源码)
  20. python import相对引用和绝对引用

热门文章

  1. keba驱动器_KEBA控制器说明书.doc
  2. c语言像素点的简单获取
  3. 计算机环模实验报告,PPT实验报告模板2篇
  4. 书签 自动更新浏览器的书签_针对您的浏览器的英语翻译书签
  5. 02.01、移除重复节点
  6. python中如何实现复制粘贴_复制粘贴功能的Python程序 python 中如何实现
  7. RFID资产管理安全追溯解决方案,替代传统资产管理
  8. ant批量修改文件名_Ant Renamer-Ant Renamer(免费批量重命名工具)下载 v2.12官方版--pc6下载站...
  9. 考粒子静态能源公式、太阳系天体运动原理...中国银行笔试题刷屏,网友:这是在招总行行长?...
  10. g723源码详细分析(-)