使用ansible角色部署lamp架构


文章目录

  • 使用ansible角色部署lamp架构
    • 一、部署Apache
      • 1、配置主机并创建角色
      • 2、编写task任务
      • 3、编写脚本
      • 4、调用角色
    • 二、部署mysql
      • 1、编写task任务
      • 2、编写脚本
      • 3、调用角色
    • 三、部署php
      • 1、编写task任务
      • 2、编写脚本
      • 3、调用角色

一、部署Apache


1、配置主机并创建角色

[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# vim hosts
node1
[root@ansible ansible]# ls
ansible.cfg  hosts  roles
[root@ansible ansible]# cd roles/
[root@ansible roles]# ansible-galaxy init apache
- Role apache was created successfully
[root@ansible roles]# ansible-galaxy init mysql
- Role mysql was created successfully
[root@ansible roles]# ansible-galaxy init php
- Role php was created successfully
[root@ansible roles]# ls
apache  mysql  php

2、编写task任务

[root@ansible apache]# vim tasks/main.yml
---
# tasks file for apache
- name: set yumscript: yum.sh- name: install packagesyum:name: "{{ httpdpkgs }}"state: present- name: unzip aprunarchive:src: apr-1.6.5.tar.bz2dest: /usr/src/- name: unzip apr-utilunarchive:src: apr-util-1.6.1.tar.bz2dest: /usr/src/- name: unzip httpdunarchive:src: httpd-2.4.54.tar.bz2dest: /usr/src/- name: install httpdscript: httpd.sh- name: apache.shscript: apache.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

3、编写脚本

[root@ansible files]# ls
apache.sh  apr-1.6.5.tar.bz2  apr-util-1.6.1.tar.bz2  httpd-2.4.54.tar.bz2  httpd.sh  yum.sh
[root@ansible files]# vim yum.sh
#!/bin/bash
/usr/bin/curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
yum reinstall -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
/usr/bin/sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
/usr/bin/sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*[root@ansible apache]# vim vars/main.yml
---
# vars file for apache
httpdpkgs:- bzip2- make- wget- openssl-devel- pcre-devel- expat-devel- libtool- gcc- gcc-c++- libxml2-devel[root@ansible files]# vim httpd.sh
#/bin/bash
cd /opt/apr-1.6.5
sed -i '/$RM "$cfgfile"/d' configure
./configure --prefix=/usr/local/apr
make
make installcd /opt/apr-util-1.6.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make
make installcd /opt/httpd-2.4.54
./configure --prefix=/usr/local/apache \--sysconfdir=/etc/httpd24 \--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=prefork
make
make install[root@ansible files]# vim apache.sh
export PATH=/usr/local/apache/bin/:$PATH[root@ansible apache]# vim templates/httpd.service.j2
[Unit]
Description=httpd server daemon
After=network.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

4、调用角色

[root@ansible ansible]# cat httpd.yml
---
- name: use apache rolehosts: node1roles:- apache[root@ansible ansible]# ansible-playbook httpd.ymlPLAY [use apache role] *************************************************************************************************************************************************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 : unzip apr-util] *****************************************************************************************************************************************
ok: [node1]TASK [apache : unzip httpd] ********************************************************************************************************************************************
changed: [node1]TASK [apache : install httpd] ******************************************************************************************************************************************
changed: [node1]TASK [apache.sh] *******************************************************************************************************************************************************
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] *******************************************************************************************************************************************
ok: [node1]PLAY RECAP *************************************************************************************************************************************************************
node1                      : ok=14   changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

二、部署mysql


1、编写task任务

[root@ansible roles]# cd mysql/
[root@ansible mysql]# ls
defaults  files  handlers  meta  README.md  tasks  templates  tests  vars
[root@ansible mysql]# cd tasks/
[root@ansible tasks]# ls
main.yml
[root@ansible tasks]# vim main.yml
---
# tasks file for mysql
- name: create user mysqluser:name: mysqlsystem: yesshell: /sbin/nologincreate_home: nostate: present- name: install pkgsyum:name: libncurses*state: present- name: unzipunarchive:src: mysql-5.7.37-linux-glibc2.12-x86_64.tar.gzdest: /usr/local/- name: create linkfile:src: /usr/local/mysql-5.7.37-linux-glibc2.12-x86_64dest: /usr/local/mysqlowner: mysqlgroup: mysqlstate: link- name: create directoryfile:path: /opt/dataowner: mysqlgroup: mysqlstate: directory- name: mysql initialization.shscript: initialization.sh- name: copy configtemplate:src: my.cnf.j2dest: /etc/my.cnf- name: replace file1replace:path: /usr/local/mysql/support-files/mysql.serverregexp: "#^(basedir=).*"replace: "basedir=/usr/local/mysql"- name: replace file2replace:path: /usr/local/mysql/support-files/mysql.serverregexp: "#^(datadir=).*"replace: "datadir=/opt/data"- name: copy mysql.servicetemplate:src: mysqld.service.j2dest: /usr/lib/systemd/system/mysqld.service- name: reload configshell:cmd: systemctl daemon-reload- name: restart mysqldservice:name: mysqldstate: startedenabled: yes- name: set mysql passwdshell:cmd: /usr/local/mysql/bin/mysql -uroot -e "set password=password('123.com')"- name: set mysql envscript: mysql.sh

2、编写脚本

//mysql初始化
[root@ansible mysql]# vim files/initialization.sh
#!/bin/bash
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data/
ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
ldconfig//环境变量
[root@ansible files]# cat mysql.sh
echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile.d/mysql.sh//模板文件
[root@ansible templates]# vim my.cnf.j2
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve[root@ansible templates]# vim mysqld.service.j2
[Unit]
Description=mysql server daemon
After=network.targe[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP \$MAINPID[Install]
WantedBy=multi-user.target
[root@ansible templates]#

3、调用角色

[root@ansible ansible]# vim mysql.yml
---
- name: use mysql rolehosts: node1roles:- mysql
[root@ansible ansible]# ansible-playbook mysql.ymlPLAY [use mysql role] **************************************************************************************************************************************************TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [node1]TASK [create user mysql] ***********************************************************************************************************************************************
ok: [node1]TASK [mysql : install pkgs] ********************************************************************************************************************************************
ok: [node1]TASK [mysql : unzip] ***************************************************************************************************************************************************
changed: [node1]TASK [mysql : create link] *********************************************************************************************************************************************
ok: [node1]TASK [mysql : create directory] ****************************************************************************************************************************************
ok: [node1]TASK [mysql initialization.sh] *****************************************************************************************************************************************
changed: [node1]TASK [mysql : copy config] *********************************************************************************************************************************************
changed: [node1]TASK [mysql : replace file1] *******************************************************************************************************************************************
ok: [node1]TASK [mysql : replace file2] *******************************************************************************************************************************************
ok: [node1]TASK [copy mysql.service] **********************************************************************************************************************************************
ok: [node1]TASK [mysql : reload config] *******************************************************************************************************************************************
changed: [node1]TASK [restart mysqld] **************************************************************************************************************************************************
changed: [node1]TASK [set mysql passwd] ************************************************************************************************************************************************
changed: [node1]TASK [set mysql env] ***************************************************************************************************************************************************
changed: [node1]PLAY RECAP *************************************************************************************************************************************************************
node1                      : ok=15   changed=7    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0//在node1上查看
[root@node1 ~]# systemctl status mysqld
● mysqld.service - mysql server daemonLoaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)Active: active (running) since Mon 2022-11-07 16:07:59 CST; 59min agoMain PID: 15518 (mysqld_safe)Tasks: 29 (limit: 11175)Memory: 189.3MCGroup: /system.slice/mysqld.service├─15518 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid└─15708 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=node1.e>

三、部署php


1、编写task任务

[root@ansible php]# vim tasks/main.yml
---
# tasks file for php
- name: install packagesyum:name: "{{ php_pack }}"state: present- name: unzipunarchive:src: php-7.1.10.tar.gzdest: /usr/local- 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/httpd24/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

2、编写脚本

[root@ansible php]# vim files/php.sh
#!/bin/bash
cd /usr/local/php-7.1.10
./configure \--prefix=/usr/local/php  \--with-apxs2=/usr/local/apache/bin/apxs \--with-mysql-sock=/tmp/mysql.sock \--with-mysqli \--with-zlib \--with-curl \--with-gd \--with-jpeg-dir \--with-png-dir \--with-freetype-dir \--with-openssl \--enable-mbstring \--enable-xml \--enable-session \--enable-ftp \--enable-pdo \--enable-tokenizer \--enable-zipmake
make installecho 'export PATH=/usr/local/php/bin:$PATH' > /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/httpd24/httpd.conf
echo "AddType application/x-httpd-php-source .phps" >>/etc/httpd24/httpd.confsed -i 's/index.html/index\.php index\.html/' /etc/httpd24/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 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 templates/index.php.j2
<?php
phpinfo();
?>

3、调用角色

[root@ansible ansible]# vim php.yml
---
- name: use php rolehosts: node1roles:- php[root@ansible ansible]# ansible-playbook php.ymlPLAY [use php role] ****************************************************************************************************************************************************TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [node1]TASK [php : install packages] ******************************************************************************************************************************************
ok: [node1]TASK [php : unzip] *****************************************************************************************************************************************************
ok: [node1]TASK [install php] *****************************************************************************************************************************************************
changed: [node1]TASK [php : cp template] ***********************************************************************************************************************************************
ok: [node1]TASK [php : refresh] ***************************************************************************************************************************************************
changed: [node1]TASK [set php service] *************************************************************************************************************************************************
ok: [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] ****************************************************************************************************************************************************
ok: [node1]TASK [php : restart httpd] *********************************************************************************************************************************************
changed: [node1]PLAY RECAP *************************************************************************************************************************************************************
node1                      : ok=11   changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0[root@node1 ~]# ss -antl
State               Recv-Q              Send-Q                           Local Address:Port                           Peer Address:Port             Process
LISTEN              0                   128                                  127.0.0.1:9000                                0.0.0.0:*
LISTEN              0                   128                                    0.0.0.0:22                                  0.0.0.0:*
LISTEN              0                   80                                           *:3306                                      *:*
LISTEN              0                   128                                          *:80                                        *:*
LISTEN              0                   128                                       [::]:22                                     [::]:*
[root@node1 ~]#

//访问网页

ansible角色部署lamp架构相关推荐

  1. Ansible使用角色部署LAMP架构

    文章目录 使用角色部署lamp架构 部署apache 编写任务 编写脚本 配置变量 配置模板 编写playbook执行 部署mysql 编写任务 配置变量 编写脚本 编写模板 编写playbook执行 ...

  2. ansible部署LAMP架构

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

  3. ansible角色部署mysql主从

    linux ansible角色部署mysql主从 ansible主机创建角色 重新定义主机清单 [root@ansible ~]# cd /etc/ansible/ [root@ansible ans ...

  4. ansible角色部署mysql主从复制

    ansible角色部署mysql主从复制 创建角色mariadb [root@ansible roles]# ansible-galaxy init mariadb 编写角色执行任务 [root@an ...

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

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

  6. 如何部署LAMP架构

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

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

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

  8. ansible分离部署lamp

    Ansible部署lanm 添加受管主机 [root@localhost ansible]# cat cctv [http] 192.168.100.42 ansible_user=root ansi ...

  9. 通过Playbook部署LAMP(5)

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

最新文章

  1. MIT:大脑如何跟踪运动中的物体?
  2. urllib的实现---cookie处理
  3. 《赛博朋克 2077》与《对马岛之魂》的「不自由感」究竟从何而来?
  4. C# GC 垃圾回收机制原理
  5. java ee 代码,JavaEE用户登录注册代码(免费)
  6. 输入一批整数,输出最大最小值,输入0结束
  7. 【渝粤题库】 广东开放大学 劳动和社会保障法 形成性考核
  8. iTerm2 如何设置以单词为单位快速移动光标?
  9. 基于alpine用dockerfile创建的ssh镜像
  10. 下列软件包有未满足的依赖关系,依赖: libxxx(= 2.2.10) 但是 2.3.0正要被安装
  11. 什么叫手机android密码,手机这个密码必须设,99%的人不知道!
  12. android 华为打开usb调试模式,华为H60-L01 开启USB调试模式
  13. python numpy 矩阵加减规则 ValueError: operands could not be broadcast together with shapes
  14. 常见的十大恶意软件类型
  15. 购物商城网站建设费用到底贵不贵?
  16. Error running ‘Tomcat8.5‘ port out of range-1 (moments ago)
  17. 文本动画过渡效果AE模板
  18. [健康]肾的保健按摩
  19. Shell监控jvm发短信
  20. [解读REST] 1.REST的起源

热门文章

  1. 中国研发磁悬浮高铁新技术 再次实现自我超越
  2. 线性空间计量模型与stata操作
  3. 使用Jexus实现ASP.NET在Linux平台下的部署
  4. 通过苹果设计和小米商业,重新理解互联网产品创新
  5. Cookie Session Token 与 JWT 解析
  6. divi模板下载_Divi WordPress主题:更改博客方式
  7. Delphi 版的 Ping
  8. Unity3D 大型游戏 MOBA类手机游戏 部分重点 登陆过程(17)
  9. Unity3D 大型游戏 MOBA类手机游戏 状态机在游戏中的应用(18)
  10. 程序员自学Python,走过不少弯路,给大家一点经验建议