利用角色roles上线wordpress项目

角色订制:roles

① 简介

对于以上所有的方式有个弊端就是无法实现复用假设在同时部署Web、db、ha 时或不同服务器组合不同的应用就需要写多个yml文件。很难实现灵活的调用。   roles 用于层次性、结构化地组织playbook。roles 能够根据层次型结构自动装载变量文件、tasks以及handlers等。要使用roles只需要在playbook中使用include指令即可。简单来讲,roles就是通过分别将变量(vars)、文件(file)、任务(tasks)、模块(modules)及处理器(handlers)放置于单独的目录中,并可以便捷地include它们的一种机制。角色一般用于基于主机构建服务的场景中,但也可以是用于构建守护进程等场景中。

② 角色集合

角色集合:roles/ mysql/ httpd/ nginx/ files/:存储由copy或script等模块调用的文件; tasks/:此目录中至少应该有一个名为main.yml的文件,用于定义各task;其它的文件需要由main.yml进行“包含”调用; handlers/:此目录中至少应该有一个名为main.yml的文件,用于定义各handler;其它的文件需要由main.yml进行“包含”调用; vars/:此目录中至少应该有一个名为main.yml的文件,用于定义各variable;其它的文件需要由main.yml进行“包含”调用; templates/:存储由template模块调用的模板文本; meta/:此目录中至少应该有一个名为main.yml的文件,定义当前角色的特殊设定及其依赖关系;其它的文件需要由main.yml进行“包含”调用; default/:此目录中至少应该有一个名为main.yml的文件,用于设定默认变量;

实验:

192.168.231.210 安装ansible机器 管理机

192.168.231.214 

192.168.231.215 被管理的俩台机器

在roles目录下生成对应的目录结构

[root@localhost ~]# mkdir roles
[root@localhost ~]# cd  roles[root@localhost roles]# ansible-galaxy init nginx 
- Role nginx was created successfully
[root@localhost roles]# ansible-galaxy init mysql
- Role mysql was created successfully
[root@localhost roles]# ansible-galaxy init php
- Role php was created successfully[root@localhost roles]# tree
.
├── mysql
│   ├── defaults
│   │   └── main.yml
│   ├── files
│   ├── handlers
│   │   └── main.yml
│   ├── meta
│   │   └── main.yml
│   ├── README.md
│   ├── tasks
│   │   └── main.yml
│   ├── templates
│   ├── tests
│   │   ├── inventory
│   │   └── test.yml
│   └── vars
│       └── main.yml
├── nginx
│   ├── defaults
│   │   └── main.yml
│   ├── files
│   ├── handlers
│   │   └── main.yml
│   ├── meta
│   │   └── main.yml
│   ├── README.md
│   ├── tasks
│   │   └── main.yml
│   ├── templates
│   ├── tests
│   │   ├── inventory
│   │   └── test.yml
│   └── vars
│       └── main.yml
└── php├── defaults│   └── main.yml├── files├── handlers│   └── main.yml├── meta│   └── main.yml├── README.md├── tasks│   └── main.yml├── templates├── tests│   ├── inventory│   └── test.yml└── vars└── main.yml27 directories, 24 files

分别定义配置文件 ,nginx配置文件

[root@localhost roles]# vim nginx/tasks/main.yml 
---
# tasks file for nginx- name: 下载yum源shell: curl -o /opt/yum-server.sh http://10.36.192.100/yum-server.sh- name: 安装yum源shell: sh /opt/yum-server.sh- name: 安装nginxyum: name=nginx state=present- name: nginx配置文件template: src=/root/nginx.conf dest=/etc/nginx/- name: 拷贝phpcopy: src=/root/roles/wp-config.php  dest=/usr/share/nginx/html/wordpress- name: 启动Nginxservice: name=nginx state=started enabled=true- name: 拷贝wordpress源代码unarchive: src=/root/wordpress-6.4.1-zh_CN.tar.gz dest=/usr/share/nginx/html
  • 创建 /root/roles/wp-config.php文件写入一下内容

[root@localhost roles]# vim  wp-config.php 
<?php
/*** The base configuration for WordPress** The wp-config.php creation script uses this file during the installation.* You don't have to use the web site, you can copy this file to "wp-config.php"* and fill in the values.** This file contains the following configurations:** * Database settings* * Secret keys* * Database table prefix* * ABSPATH** @link https://wordpress.org/documentation/article/editing-wp-config-php/** @package WordPress*/// ** Database settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpress' );/** Database username */
define( 'DB_USER', 'wordpress' );/** Database password */
define( 'DB_PASSWORD', 'Qianfeng@123' );/** Database hostname */
define( 'DB_HOST', '192.168.231.214' );/** Database charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8mb4' );/** The database collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );/**#@+* Authentication unique keys and salts.** Change these to different unique phrases! You can generate these using* the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}.** You can change these at any point in time to invalidate all existing cookies.* This will force all users to have to log in again.** @since 2.6.0*/
define( 'AUTH_KEY',         'U^UE ~}t)n(9.~=_qMaI:,K`X;Iji<Bsuh[b%yL-VpltA#_zRAX<~kJ>`D;PB:g4' );
define( 'SECURE_AUTH_KEY',  'mZcTrBj{U/-M#+AX:U +&?.&sXlV~/Dlm:rO|=/XL66gI+pl#IcwWUe(<p6HzYs1' );
define( 'LOGGED_IN_KEY',    'D(a}-NENZ{u[& =;|fSIDZt]`z aVKYyt|wt$5[jC#<yvgaM9mvvZCn|W(Wo=8rY' );
define( 'NONCE_KEY',        'MRC+QQ+o6`uE?S^`&#7A1cwBP1I2jC#S?%xoR9dQSSs1+S}n.F{Cf7l*%c:dUSbo' );
define( 'AUTH_SALT',        'Ai6`CEc,KaGUmk-/mLFEU $:b0#!Mjg6_qTB)+*tx=SRsuD6+;z[Ji6gL|@8.TRn' );
define( 'SECURE_AUTH_SALT', '`NL:<P=!HtaQ> q]*}_TB9LA!%Kmh7CjXeM-s-/;>U0o8huC7&KPq{i_wNX%3OI{' );
define( 'LOGGED_IN_SALT',   'q9p;a=$q|m}NI*z!?UKgMzs*xKsF)L.TPhnZ/|U[5bw+RzDv.|$W#vMzu<g5!xYb' );
define( 'NONCE_SALT',       'F+/f&;FTu yZ^(qtdT3 zY%s~FN6lS71iic:X1&q4f^pzi4w3e/$b %,tRt?),$4' );/**#@-*//*** WordPress database table prefix.** You can have multiple installations in one database if you give each* a unique prefix. Only numbers, letters, and underscores please!*/
$table_prefix = 'wp_';/*** For developers: WordPress debugging mode.** Change this to true to enable the display of notices during development.* It is strongly recommended that plugin and theme developers use WP_DEBUG* in their development environments.** For information on other constants that can be used for debugging,* visit the documentation.** @link https://wordpress.org/documentation/article/debugging-in-wordpress/*/
define( 'WP_DEBUG', false );/* Add any custom values between this line and the "stop editing" line. *//* That's all, stop editing! Happy publishing. *//** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {define( 'ABSPATH', __DIR__ . '/' );
}/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';

修改变量文件

[root@localhost roles]# vim    nginx/vars/main.yml 
---
# vars file for nginxnginx_user: rootnginx_port: 80

放置我们所需要的文件到指定目录

直接 cp /root/nginx.conf  /root/roles/nginx/templates/nginx.conf
也可以直接写
[root@localhost roles]# vim nginx/templates/nginx.conf 
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/user {{ nginx_user }};
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;events {worker_connections 1024;
}http {log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log  /var/log/nginx/access.log  main;sendfile            on;tcp_nopush          on;tcp_nodelay         on;keepalive_timeout   65;types_hash_max_size 4096;include             /etc/nginx/mime.types;default_type        application/octet-stream;# Load modular configuration files from the /etc/nginx/conf.d directory.# See http://nginx.org/en/docs/ngx_core_module.html#include# for more information.include /etc/nginx/conf.d/*.conf;server {listen       {{ nginx_port }};listen       [::]:80;server_name  _;root         /usr/share/nginx/html/wordpress;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;location / {root /usr/share/nginx/html/wordpress;index index.php;}location ~ \.php$ {root          /usr/share/nginx/html/wordpress;  #指定网站目录fastcgi_pass   127.0.0.1:9000;    #指定访问地址fastcgi_index  index.php;		#指定默认文件fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;include        fastcgi_params;  #包含nginx常量定义}  error_page 404 /404.html;location = /404.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}}# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }}

php配置文件

[root@localhost roles]# vim php/tasks/main.yml 
---
# tasks file for php- name: 安装PHPyum: name=php80-php-xsl,php80-php,php80-php-cli,php80-php-devel,php80-php-gd,php80-php-pdo,php80-php-mysql,php80-php-fpm state=present- name: 启动PHPservice: name=php80-php-fpm state=started enabled=true

mysql配置文件

[root@localhost roles]# cat mysql/tasks/main.yml 
---
# tasks file for mysql- name: 安装数据库yum: name=mariadb-server,mariadb state=present disablerepo=mysql-5.7-community- name: 启动数据库service: name=mariadb state=started enabled=true- name: 配置数据库shell: mysql -e "create database {{ db_name }}; grant all on wordpress.* to 'wordpress'@'%' identified by '{{ db_passwd }}'; flush privileges" 

mysql变量文件

[root@localhost roles]# vim mysql/vars/main.yml 
---
# vars file for mysql
db_passwd: 'Qianfeng@123'
db_name: 'wordpress'

定义剧本文件

接下来,我们就来定义剧本文件,由于大部分设置我们都单独配置在了roles里面,所以,接下来剧本就只需要写一点点内容即可:

[root@localhost roles]# vim roles.yml 
---- hosts: webremote_user: rootroles:- nginx- php- mysql

启动服务

[root@localhost roles]# ansible-playbook roles.ymlPLAY [web] ********************************************************************************TASK [Gathering Facts] ********************************************************************
ok: [web1]
ok: [web2]TASK [nginx : 下载yum源] *********************************************************************
[WARNING]: Consider using the get_url or uri module rather than running 'curl'.  If you
need to use command because get_url or uri 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: [web1]
changed: [web2]TASK [nginx : 安装yum源] *********************************************************************
changed: [web1]
changed: [web2]TASK [安装nginx] ****************************************************************************
changed: [web1]
changed: [web2]TASK [nginx配置文件] **************************************************************************
changed: [web1]
changed: [web2]TASK [nginx : 启动Nginx] ********************************************************************
changed: [web2]
changed: [web1]TASK [nginx : 拷贝wordpress源代码] *************************************************************
changed: [web2]
changed: [web1]TASK [php : 安装PHP] ************************************************************************
changed: [web1]
changed: [web2]TASK [php : 启动PHP] ************************************************************************
changed: [web1]
changed: [web2]TASK [mysql : 安装数据库] **********************************************************************
changed: [web1]
changed: [web2]TASK [mysql : 启动数据库] **********************************************************************
changed: [web1]
changed: [web2]TASK [mysql : 配置数据库] **********************************************************************
changed: [web1]
changed: [web2]PLAY RECAP ********************************************************************************
web1                       : ok=12   changed=11   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
web2                       : ok=12   changed=11   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

查看所有服务的端口

[root@localhost roles]# ansible web -m shell -a 'ss -nplt'
web1 | CHANGED | rc=0 >>
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128    127.0.0.1:9000                     *:*                   users:(("php-fpm",pid=2504,fd=9),("php-fpm",pid=2503,fd=9),("php-fpm",pid=2502,fd=9),("php-fpm",pid=2501,fd=9),("php-fpm",pid=2500,fd=9),("php-fpm",pid=2499,fd=7))
LISTEN     0      50           *:3306                     *:*                   users:(("mysqld",pid=2986,fd=14))
LISTEN     0      128          *:80                       *:*                   users:(("nginx",pid=2057,fd=6),("nginx",pid=2056,fd=6),("nginx",pid=2055,fd=6))
LISTEN     0      128          *:22                       *:*                   users:(("sshd",pid=922,fd=3))
LISTEN     0      100    127.0.0.1:25                       *:*                   users:(("master",pid=1115,fd=13))
LISTEN     0      128       [::]:80                    [::]:*                   users:(("nginx",pid=2057,fd=7),("nginx",pid=2056,fd=7),("nginx",pid=2055,fd=7))
LISTEN     0      128       [::]:22                    [::]:*                   users:(("sshd",pid=922,fd=4))
LISTEN     0      100      [::1]:25                    [::]:*                   users:(("master",pid=1115,fd=14))
web2 | CHANGED | rc=0 >>
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128    127.0.0.1:9000                     *:*                   users:(("php-fpm",pid=2499,fd=9),("php-fpm",pid=2498,fd=9),("php-fpm",pid=2497,fd=9),("php-fpm",pid=2496,fd=9),("php-fpm",pid=2495,fd=9),("php-fpm",pid=2494,fd=7))
LISTEN     0      50           *:3306                     *:*                   users:(("mysqld",pid=2983,fd=14))
LISTEN     0      128          *:80                       *:*                   users:(("nginx",pid=2052,fd=6),("nginx",pid=2051,fd=6),("nginx",pid=2050,fd=6))
LISTEN     0      128          *:22                       *:*                   users:(("sshd",pid=920,fd=3))
LISTEN     0      100    127.0.0.1:25                       *:*                   users:(("master",pid=1115,fd=13))
LISTEN     0      128       [::]:80                    [::]:*                   users:(("nginx",pid=2052,fd=7),("nginx",pid=2051,fd=7),("nginx",pid=2050,fd=7))
LISTEN     0      128       [::]:22                    [::]:*                   users:(("sshd",pid=920,fd=4))
LISTEN     0      100      [::1]:25                    [::]:*                   users:(("master",pid=1115,fd=14))

进去浏览器查看

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/170781.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

农场养殖管理系统软件开发方案

一、项目概述 农场养殖管理系统是一款针对农场养殖管理的软件&#xff0c;旨在提高农场养殖效率和管理水平。本方案将详细介绍该系统的开发流程&#xff0c;包括需求分析、系统设计、数据库设计、界面设计、系统测试和上线运营等方面。 二、需求分析 在开发农场养殖管理系统…

全志R128平台SPI与DBI点屏性能大对比

SPI 与 DBI 性能对比 R128 平台的 SPI 接口参数如下 全双工同步串行接口Master/Slave模式可配置支持最大96MHz时钟频率支持SPI Mode0/1/2/3片选和时钟的极性和相位可配置5个时钟源支持中断或DMA传输支持多片选支持Standard Single/Dual/Quad SPI&#xff0c;FIFO深度64B支持B…

冯·诺伊曼体系结构--操作系统

文章目录 1.认识冯诺依曼系统1.1约翰冯诺依曼1.2冯诺依曼结构1.3存储器的读写速度1.4对冯诺依曼结构的认识1.5冯诺依曼结构在生活中的演示 2.操作系统--“搞管理”的软件2.1概念2.2OS存在的意义2.3管理的方式2.4系统调用和库函数概念 1.认识冯诺依曼系统 1.1约翰冯诺依曼 1.2冯…

Hololens开发笔记

1、关闭阴影 2、将相机渲染改为后向。因为默认是Forward&#xff0c;当在场景里面想使用点光源时&#xff0c;运行起来三角面会翻倍&#xff0c;影响软件运行流畅度。 3、第三人称同步相关。开启Host/Sever/Client前&#xff0c;需要将所有挂有NetworkObject/NetworkTransfor…

vue3 使用element plus 打包时 报错

vue3vitetselementPlus中运行正常打包出错 能正常运行&#xff0c;但是打包出错 解决打包时出现导入element plus相关的爆红&#xff0c;导致无法打包的问题 如若出现类似于&#xff1a;Module ‘“element-plus”’ has no exported member ‘ElMessage’. Did you mean to…

Matplotlib的使用方法

Matplotlib是Python最著名的绘图库&#xff0c;它提供了一整套和Matlab相似的命令API&#xff0c;十分适合交互式地进行制图。而且也可以方便地将它作为绘图控件&#xff0c;嵌入到GUI应用程序中。Matplotlib能够创建多数类型的图表&#xff0c;如条形图、散点图、条形图、饼图…

Django中简单的增删改查

用户列表展示 建立列表 views.py def userlist(request):return render(request,userlist.html) urls.py urlpatterns [path(admin/, admin.site.urls),path(userlist/, views.userlist), ]templates----userlist.html <!DOCTYPE html> <html lang"en">…

ubuntu上安装edge浏览器

1下载edge浏览器 官网下载 edge浏览器的linux版本可在上面的官网中寻找。 我选择的是Linux(.deb)。 2 安装 可在终端的edge安装包所在的路径下输入下面命令安装。 sudo dpkg -i edge安装包的名称.deb3 安装可能存在的问题 1dpkg:依赖关系问题使得edge-stable的配置工作不…

带有密码的Excel只读模式,如何取消?

Excel文件打开之后发现是只读模式&#xff0c;想要退出只读模式&#xff0c;但是只读模式是带有密码的&#xff0c;该如何取消带有密码的excel只读文件呢&#xff1f; 带有密码的只读模式&#xff0c;是设置了excel文件的修改权限&#xff0c;取消修改权限&#xff0c;我们需要…

11.读取文件长度-fseek和ftell函数的使用

文章目录 简介1. 写入测试文件2. 读取文件长度 简介 主要讲使用fopen读取文件&#xff0c;配合使用fseek和ftell来读取文件长度。1. 写入测试文件 执行下方程序&#xff0c;使用fwrite函数写入40字节的数据&#xff0c;使其形成文件存入本地目录。#define _CRT_SECURE_NO_WARNI…

直播间自动发言机器人的运行分享,与开发需要到的技术分析

先来看实操成果&#xff0c;↑↑需要的同学可看我名字↖↖↖↖↖&#xff0c;或评论888无偿分享 一、引言 随着人工智能技术的不断发展&#xff0c;自动发言机器人已经成为了当今社交媒体领域的重要组成部分。它们能够自动化地发布内容、回复用户评论和消息&#xff0c;大大提高…

追寻Moonbeam身影,泰国区块链周正在火热进行中!

继Moonbeam参与HK Web3月之后&#xff0c;下一站便是由Cryptomind Group举办的泰国2023年区块链周。本次位于泰国的区块链周以“熊市中建设&#xff0c;牛市中崛起”为理念&#xff0c;旨在为对区块链技术感兴趣的个人和投资者提供机会接触行业中的团队和专家&#xff0c;并邀请…