一、Ansible角色定制(roles)
对于以上所有的方式有个弊端就是无法实现复用假设在同时部署Web、db、ha 时或不同服务器组合不同的应用就需要写多个yml文件。很难实现灵活的调用。 roles 用于层次性、结构化地组织playbook。roles 能够根据层次型结构自动装载变量文件、tasks以及handlers等。要使用roles只需要在playbook中使用include指令即可。简单来讲,roles就是通过分别将变量(vars)、文件(file)、任务(tasks)、模块(modules)及处理器(handlers)放置于单独的目录中,并可以便捷地include它们的一种机制。角色一般用于基于主机构建服务的场景中,但也可以是用于构建守护进程等场景中。
二、搭建实例:wordpress
环境:192.168.10.134 安装php与nginx
192.168.10.135 安装MySQL
1、创建一个roles目录来存放我们要写的规则
mkdir /root/roles #目录位置可自定义
2、用ansible-galaxy创建规则文件
#创建MySQL安装
ansible-gaalaxy init mysql
3、在tasks/main.yml里写安装规则
---
# tasks file for mysql
- name: get yum-source
get_url: url=http://10.36.192.100/yum-server.sh dest=/root/
- name: update yum
shell: sh /root/yum-server.sh
- name: install mysql
yum: name=mysql-server state=present
- name: up mysqld
service: name=mysqld state=started enabled=true
#更改数据库密码
- name: change password
shell: mysqladmin -p"`awk '/temporary password/{p=$NF}END{print p}' /var/log/mysqld.log`" password 'Yongcun8023#'
#创建wordpress库并授权
- name: create database
shell: mysql -'pYongcun8023#' -e "create database wordpress charset "uft8";grant all on wordpress.* to 'wordpress'@'%' identified by "Yongcun8023#""
4、创建nginx规则文件
ansible-galaxy init nginx
# 在vars/main.yml文件中定义变量,在下面的文件中引用变量
# vars file for nginx
user_server: nginx
user_port: 8000 #将nginx访问端口设置成8000
#同样在、tasks/main.yml下写nginx安装规则
---
# tasks file for nginx
#这是配置本地源,这个需要根据你的情况,配置yum源,如果你有,就不用
- name: get yum-source
get_url: url=http://10.36.192.100/yum-server.sh dest=/root/
- name: update yum
shell: sh /root/yum-server.sh
- name: install {{ user_server }}
yum: name={{ user_server }} state=present
- name: change file
template: src=nginx.conf dest=/etc/nginx/
- name: start {{ user_server }}
service: name={{ user_server }} state=started
#因为nginx需要连接php,所以,我们要提前改一下nginx配置文件,改好的配置文件放在nginx/templates里,更改如下
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user {{ user_server }};
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 {{ user_port | default("80") }};
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
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;
}
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
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 {
# }
# }
}
5、创建php规则文件
ansible-galaxy init php
#同样在tasks/main.yml中写安装php的规则
---
# 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
- name: souce wordpress
unarchive: src=wordpress-5.9.1-zh_CN.tar.gz dest=/usr/share/nginx/html/
6、创建一个以.yml结尾的文件
touch /root/roles/roles.yml
写入一下内容
---
- hosts: web #要安装的主机组,也可以是ip
remote_user: root #执行的用户
roles:
- php #规则文件,比如要执行MySQL的安装,那就写MySQL创建的目录名
在上面的文件中写入对应的规则文件和安装的主机组,用ansible-playbook来执行
#查看文件格式是否错误
ansible-playbook roles.yml --syntax-check
#列出要执行的主机
ansible-playbook roles.yml --list-hosts
#列出要执行的任务
ansible-playbook roles.yml --list-tasks
#前面都没问题,那就分别执行
ansible-playbook roles.yml
7、访问http://192.168.10.134:8000
#填写配置相关配置
在wordpress发布路径,创建vim /usr/share/nginx/html/wordpress/wp-config.php ,将框内内容写入,然后下一步登录即可