Ansible角色定制实例

目录

角色定制:roles

角色定制实例:利用角色部署wordpress

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

2.定义配置文件

①nginx

②php

 ③mysql

④定义剧本文件

⑤启动服务


角色定制:roles

      对于普通的剧本(playbook)有个弊端就是无法实现复用假设在同时部署Web、db、ha 时或不同服务器组合不同的应用就需要写多个yml文件。很难实现灵活的调用。

  roles 用于层次性、结构化地组织playbook。roles 能够根据层次型结构自动装载变量文件、tasks以及handlers等。要使用roles只需要在playbook中使用include指令即可。简单来讲,roles就是通过分别将变量(vars)、文件(file)、任务(tasks)、模块(modules)及处理器(handlers)放置于单独的目录中,并可以便捷地include它们的一种机制。

角色定制实例:利用角色部署wordpress

192.168.134.165 server01

192.168.134.166 server02

192.168.134.163 server03

server01管理其他两个服务器资产。

1.在roles目录下生成对应的目录结构
[root@server01 ~]# mkdir roles
#生成相应的目录
[root@server01 roles]# ansible-galaxy init nginx
[root@server01 roles]# ansible-galaxy init mysql
[root@server01 roles]# ansible-galaxy init php
[root@server01 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
│   │   └── nginx.conf
│   ├── 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.yml
2.定义配置文件

我们需要修改每个文件对应的配置。

①nginx
  • [root@server01 roles]# vim nginx/tasks/main.yml
[root@server01 roles]# vim nginx/tasks/main.yml 
---
# tasks file for nginx- name: install {{ server }}yum: name={{ server }},epel-release  state=present- name: start {{ server }}service: name={{ server }} state=started- name: 拷贝wordpress源代码unarchive: src=/root/wordpress-6.4.1-zh_CN.tar.gz dest=/usr/share/nginx/html- name: copy wordpresscopy: src=/root/roles/wp-config.php  dest=/usr/share/nginx/html/wordpress/- name: copytemplate: src=/root/roles/nginx/templates/nginx.conf dest=/etc/nginx/  #将nginx的配置文件模板发送到资产上notify: restartnginxtags: rs#上传wordpress的包
[root@server01 ~]# rz 
[root@server01 ~]# ll
-rw-r--r--  1 root root 25302043 11月 13 09:58 wordpress-6.4.1-zh_CN.tar.gz
  • 创建 /root/roles/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', 'Aren@123' );/** Database hostname */
define( 'DB_HOST', '192.168.134.166' );/** 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',         'ug.ct&MB!sL1_o/6t.}Xt4|f?;%|@.Zw6>wUwM3uSXcgB_nnOaZF-m^oUAuV.i-n' );
define( 'SECURE_AUTH_KEY',  'EP#~FNQDJRfL4C8Lboja}KVrc:fhdq_L~gn#wN>^qrn.DHZQwkjl=]-:QFcv:aY-' );
define( 'LOGGED_IN_KEY',    '3NQgD!)H{lR=,s(EQm7!jYrlR*T|3:h:Ag>_|#6qAO k6U#vvF)gXqb EKdcu[]v' );
define( 'NONCE_KEY',        ',LMJd60b=Qj1]B)ut!JAYCuJ)xN?`Dlnc>P8Tl_hCTk3Sl+qyAvpi_[VbCTAR?:/' );
define( 'AUTH_SALT',        'uj<)Ftb_ZA8+;ms%1AqGik.P@35[]r?.d~jc4J?]und3^vEV*=noZ}z^Gbj?u,oQ' );
define( 'SECURE_AUTH_SALT', '-CoV$a0F^9AF ?Zk()y<{}*WB`QP;$++T`F2NC6OUb]2=i9GW`*/1RjLb&sTO>}/' );
define( 'LOGGED_IN_SALT',   '2)?iJr4fi!gqk5~76^f1}Apdwynt:;$JoNdw]ty:kL]tEfy[%$H(oLNdCX/bFhJy' );
define( 'NONCE_SALT',       'zU(c-T%ayYW17wFle,oVj0@VG&m,e#Ujs~M|@>q[^|RCp*q)GbGTbRh*zh_#V5h6' );/**#@-*//*** 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_';
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@server01 roles]# vim nginx/vars/main.yml
[root@server01 roles]# vim nginx/vars/main.yml
---
# vars file for nginxserver: nginxport: 80
  • [root@server01 roles]# vim nginx/templates/nginx.conf
user nginx;
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       {{ port }};server_name  localhost;root         /usr/share/nginx/html/wordpress;index index.php;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;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; #站点根目录,取决于root配置项include        fastcgi_params;  #包含nginx常量定义}error_page 404 /404.html;location = /404.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}}}
  • 定义handlers文件 [root@server01 roles]# vim nginx/handlers/main.yml 
---
# handlers file for nginx- name: restartnginxservice: name={{ server }} state=restarted
②php
  • root@server01 roles]# vim php/tasks/main.yml 
---
# tasks file for php- name: install 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: start phpservice: name=php80-php-fpm state=started
 ③mysql
  • [root@server01 roles]# vim mysql/tasks/main.yml 
---
# tasks file for mysql- name: install mariadbyum: name=mariadb-server,mariadb  state=present disablerepo=mysql-5.7-community- name: start dbservice: name=mariadb  state=started  enabled=true- name: change passwdshell: mysql  -e "create database {{ db_name }}; grant all on wordpress.* to 'wordpress'@'%' identified by '{{ db_pass }}'; flush privileges"tags: db           
  • [root@server01 roles]# vim mysql/vars/main.yml
---
# vars file for mysqldb_name: 'wordpress'db_pass: Aren@123
④定义剧本文件

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

[root@server01 roles]# vim roles.yaml 
---- hosts: allremote_user: rootroles:- nginx- php- mysql
⑤启动服务
  • [root@server01 roles]# ansible-playbook /root/roles/roles.yaml

 进入web页面

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

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

相关文章

Jira Software Enterprise Crack

Jira Software Enterprise Crack Jira软件是为您的应用程序组中的每一个成员设计、监控和启动优秀软件的。 策略&#xff1a;生成用户故事和问题&#xff0c;策略冲刺&#xff0c;并在应用程序团队中分配任务。 跟踪&#xff1a;在具有绝对可见性的完整背景下&#xff0c;确定团…

Python---字典---dict

1、为什么需要字典 如果想要存储一个人的信息&#xff0c;姓名&#xff1a;Tom&#xff0c;年龄&#xff1a;20周岁&#xff0c;性别&#xff1a;男&#xff0c;如何快速存储。 person [Tom, 20, 男] 在日常生活中&#xff0c;姓名、年龄以及性别同属于一个人的基本特征。 但…

玩转硬件之Micro:bit的玩法(五)——垃圾分类

垃圾分类&#xff0c;为了美好的明天 垃圾是我们生活中不可避免的产物&#xff0c;每天都有大量的垃圾被丢弃&#xff0c;如果不加以处理&#xff0c;就会给环境和人类带来严重的危害。 垃圾分类是一种有效的垃圾管理方式&#xff0c;它是指按照一定的标准或规则&#xff0c;将…

从单服务设计看SLA保证

文章首发公众号&#xff1a;海天二路搬砖工 0. 引言 在微服务架构中&#xff0c;谈到SLA保证&#xff0c;我们更多是从宏观的角度来需求解决方案。比如&#xff0c;通过合理服务拆分来增加系统整体的可维护性&#xff1b;通过多实例部署来保证系统的灾备。但是单个服务是可靠…

AYIT-ACM实验室发展历程

AYIT-ACM简介 ACM协会为你的梦想插上翅膀。 本院ACM协会成立于2012年 2008年开始小规模参加河南省竞赛 2014年成功实现金牌零突破 指导老师&#xff1a;孙高飞老师 安阳工学院计算机科学与信息工程学院ACM队是一支优秀的队伍&#xff0c;一支充满活力与激情的队伍&am…

网络编程 初探windows编程

目录 一、什么是Winodws编程 二、开发环境搭建以及如何学习 三、VA助手安装 四、第一个Win32程序 五、窗口类句柄/窗口类对象 六、Winodws消息循环机制 七、Windows数据类型 一、什么是Winodws编程 Windows 编程指的是在 Microsoft Windows 操作系统上进行软件开发的过…

加速mvn下载seatunnel相关jar包

seatunnel安装的时候&#xff0c;居然要使用mvnw来下载jar包&#xff0c;而且是从https://repo.maven.apache.org 下载&#xff0c;速度及其缓慢&#xff0c;改用自己本地的mvn下载。 修改其安装插件相关脚本&#xff0c;复制install-plugin.sh重命名为install-plugin-mvn.sh …

Qt执行带参sql

//准备执行的sql语句&#xff0c;此为带参的sql语句query.prepare("update employee set Name:Name, Gender:Gender,Height:Height,"" Birthday:Birthday, Mobile:Mobile, Province:Province,"" City:City, Department:Department, Education:Educati…

Jmeter 性能 —— 负载阶梯场景!

1、安装阶梯测试的第三方插件->搜jpgc 选项-JMeter Plugins Manager -搜jpgc 空格&#xff0c;然后安装 2、脚本-线程组选jpgc Stepping Thread Group 最终并发数为100&#xff0c;并发数从0开始&#xff0c;5秒内增加10个并发数&#xff0c;增加10个后持续30s&#xff0c;…

数据结构与算法之美学习笔记:18 | 散列表(上):Word文档中的单词拼写检查功能是如何实现的?

目录 前言散列思想散列函数散列冲突解答开篇 前言 本节课程思维导图&#xff1a; Word 的单词拼写检查功能&#xff0c;虽然很小但却非常实用。你有没有想过&#xff0c;这个功能是如何实现的呢&#xff1f;其实啊&#xff0c;一点儿都不难。只要你学完今天的内容&#xff0c;…

linuxC语言缓冲区及小程序的实现

文章目录 1.文件缓冲区1.1介绍1.2缓冲文件系统1.3冲刷函数fflush1.4认识linux下的缓冲区 2.linux小程序的实现2.1 回车\r和换行\n2.2倒计时程序2.3进度条小程序sleep/usleep代码运行结果 1.文件缓冲区 1.1介绍 为缓和 CPU 与 I/O 设备之间速度不匹配&#xff0c;文件缓冲区用以…

从C语言到C++_40(多线程相关)C++线程接口+线程安全问题加锁(shared_ptr+STL+单例)

目录 1. C多线程 1.1 thread库 1.2 mutex库 1.3 RAII锁 1.4 atomicCAS 1.5 condition_variable 1.6 分别打印奇数和偶数 2. shared_ptr线程安全 2.1 库里面的shared_ptr使用 2.2 shared_ptr加锁代码 3. 单例模式线程安全 3.1 懒汉模式线程安全问题 3.2 懒汉模式最…