目录
角色定制:roles
角色定制实例:利用角色部署wordpress
1.在roles目录下生成对应的目录结构
2.定义配置文件
①nginx
②php
③mysql
④定义剧本文件
⑤启动服务
对于普通的剧本(playbook)有个弊端就是无法实现复用假设在同时部署Web、db、ha 时或不同服务器组合不同的应用就需要写多个yml文件。很难实现灵活的调用。
roles 用于层次性、结构化地组织playbook。roles 能够根据层次型结构自动装载变量文件、tasks以及handlers等。要使用roles只需要在playbook中使用include指令即可。简单来讲,roles就是通过分别将变量(vars)、文件(file)、任务(tasks)、模块(modules)及处理器(handlers)放置于单独的目录中,并可以便捷地include它们的一种机制。
192.168.134.165 server01
192.168.134.166 server02
192.168.134.163 server03
server01管理其他两个服务器资产。
[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
我们需要修改每个文件对应的配置。
[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 wordpress
copy: src=/root/roles/wp-config.php dest=/usr/share/nginx/html/wordpress/
- name: copy
template: src=/root/roles/nginx/templates/nginx.conf dest=/etc/nginx/ #将nginx的配置文件模板发送到资产上
notify: restartnginx
tags: 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
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
---
# vars file for nginx
server: nginx
port: 80
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 file for nginx
- name: restartnginx
service: name={{ server }} state=restarted
---
# tasks file for php
- name: install php
yum: 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 php
service: name=php80-php-fpm state=started
---
# tasks file for mysql
- name: install mariadb
yum: name=mariadb-server,mariadb state=present disablerepo=mysql-5.7-community
- name: start db
service: name=mariadb state=started enabled=true
- name: change passwd
shell: mysql -e "create database {{ db_name }}; grant all on wordpress.* to 'wordpress'@'%' identified by '{{ db_pass }}'; flush privileges"
tags: db
---
# vars file for mysql
db_name: 'wordpress'
db_pass: Aren@123
接下来,我们就来定义剧本文件,由于大部分设置我们都单独配置在了roles里面,所以,接下来剧本就只需要写一点点内容即可:
[root@server01 roles]# vim roles.yaml
---
- hosts: all
remote_user: root
roles:
- nginx
- php
- mysql
进入web页面