虚拟化专栏3_playbook部署lamp、lnmp电商平台

文章目录

    • playbook剧本简介
    • playbook剧本的编写
    • playbook实现lamp+wordpress项目
    • ansible实现lnmp+tinyshop项目

playbook剧本简介

ansible执行命令有两种方式AD-hoc和playbook剧本,这里可类比于shell命令和shell脚本。ansible里面的AD-hoc类似于一条条shell命令,进行简单的任务执行,适用场景如分发配置文件、获取远程主机简单信息、分发软件安装包等。而ansible里面的playbook类似于shell脚本,将一条条指令写在一个yaml文件中,再使用ansible-playbook命令批量执行。playbook用于批量执行命令,例如在服务器上搭建架构体系,安装步骤繁琐的服务等。

playbook剧本的编写

下面为简单的使用ansible剧本实现Apache的安装配置。

---
- hosts: websvs
  tasks:
    - name: "使用yum模块安装apache"
      yum: name=httpd state=latest
    - name: “使用copy模块复制配置文件”
      copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf   
    - name: “启动apache”
      service: name=httpd state=latest enabled=yes

上文中开头3个“-”相当于shell脚本里面的#!/bin/bash,使系统可将其识别为playbook。
hosts后为要操控的远程主机的主机组
tasks下为远程主机执行的命令
“-name”为各项任务的名称
使用ansible的模块执行各项命令

playbook实现lamp+wordpress项目

下文为playbook编写剧本源码搭建lamp环境。

---
- hosts: websvs
  tasks:
     - name: 安装httpd以及所需依赖
       yum: name=httpd,php,php-mysql,unzip state=latest
     - name: 推送配置文件
       template: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
       notify: stop-firewalld
       notify: stop-Selinux
     - name: 启动apache
       service: name=httpd state=started enabled=yes
     - name: 推送wordpress文件
       unarchive: src=/root/data/wordpress-4.9.4-zh_CN.zip dest=/var/www/html
     - name: 安装mysql及mysql-server
       yum: name=mariadb,mariadb-server state=latest
       notify: stop-firewalld
       notify: stop-Selinux
     - name: 启动mysql
       service: name=mariadb state=started enabled=yes
     - name: 创建wordpress数据库用户
       shell: mysql -e "create database if not exists wordpress charset utf8;"
     - name: 授权
       shell: mysql -e "grant all on wordpress.* to 'admin'@'192.168.238.%' identified by '123';"
  handlers:
     - name: stop-firewalld
       service: name=firewalld state=stopped
     - name: stop-Selinux
       shell: setenforce 0

centos7系统中安装数据库应该为mariadb、mariadb-server。虚拟化专栏3_playbook部署lamp、lnmp电商平台_第1张图片

ansible实现lnmp+tinyshop项目

---
- hosts: websvs
  vars:
    - port: 80
    - nginx: nginx-1.11.2
  tasks:
    - name: 安装php相关服务
      yum: name=php,php-fpm,php-mysql
    - name: 启动php-fpm
      service: name=php-fpm state=started
    - name: 安装nginx所需依赖
      yum: name=gcc,gcc-c++,pcre-devel,openssl-devel,zlib-devel,unzip
    - name: 分发nginx源码包
      unarchive: src=/root/playbook/{{nginx}}.tar.gz dest=/opt/
    - name: 编译安装nginx
      shell: cd /opt/{{nginx}} && ./configure && make && make install
      notify: stop-firewalld
      notify: stop-selinux
    - name: 启动nginx
      shell: /usr/local/nginx/sbin/nginx
    - name: 分发nginx和php整合配置文件
      template: src=/root/playbook/nginx.conf dest=/usr/local/nginx/conf/nginx.conf
      tags: combine
    - name: 推送电商包
      unarchive: src=/root/playbook/tinyshopV2.5_data.zip dest=/usr/local/nginx/html
    - name: 重启nginx
      shell: /usr/local/nginx/sbin/nginx -s reload
      tags: reload_nginx
  handlers:
    - name: stop-firewalld
      service: name=firewalld state=stopped
    - name: stop-selinux
      shell: setenforce 0
- hosts: mysql
  tasks:
    - name: 安装mysql
      yum: name=mariadb,mariadb-server state=latest
      notify: stop-firewalld
      notify: stop-selinux
    - name: 启动mariadb
      service: name=mariadb state=started
    - name: 创建博客数据库
      shell: mysql -e "create database tiny charset utf8;"
    - name: 授权数据库
      shell: mysql -e "grant all on tiny*. to 'admin'@'192.168.238.%' identified by '123';"
  handlers:
    - name: stop-firewalld
      service: name=firewalld state=stopped
    - name: stop-selinux
      shell: setenforce 0

虚拟化专栏3_playbook部署lamp、lnmp电商平台_第2张图片访问服务器ip安装即可
虚拟化专栏3_playbook部署lamp、lnmp电商平台_第3张图片

你可能感兴趣的:(虚拟化,安装lamp,部署wordpress)