3 playbook使用实例

一 playbook安装nginx

1.1 前期准备

/home/yx/nginx_install/files/nginx-1.12.2.tar.gz  #nginx安装包
/home/yx/nginx_install/templates/install_nginx.sh # nginx安装脚本
/home/yx/nginx_install/templates/nginx.conf # nginx配置文件
/home/yx/nginx_install/templates/index.html # 首页展示

/home/yx/nginx_install/templates/index.php # php首页展示


ansible 192.168.6.220 -m copy -a "src=/home/yx/nginx_install dest=/home/yx/ mode=0755"  # 然后批量拷贝文件到指定主机

1.2 编译安装nginx

- hosts: 192.168.6.220
  remote_user: root
  vars:
  - nginx_version: 1.12.2
  - nginx_web_dir: /home/yx/server/nginx
  - nginx_user: yx
  tasks:
  - name: 拷贝nginx软件包
    copy: src=/home/yx/nginx_install/files/nginx-1.12.2.tar.gz  dest=/tmp/nginx-1.12.2.tar.gz owner=yx group=yx
  - name: 解压
    shell: tar -zxf /tmp/nginx-{{ nginx_version }}.tar.gz -C /tmp
  - name: 开始编译并安装
    shell: cd /tmp/nginx-1.12.2/ && bash /home/yx/nginx_install/templates/install_nginx.sh
  - name: 创建logs和vhost目录
    file: path={{ nginx_web_dir }}/{{item}}  state=directory
    with_items:
      - vhost
      - logs
  - name: 更改新创建目录的属主为yx
    shell: sudo chown -R yx:yx {{ nginx_web_dir }}
  - name: 删除解压完的nginx目录
    shell: rm -rf /tmp/nginx-{{ nginx_version }}

  - name: 拷贝指定nginx配置文件 
    template: src=/home/yx/nginx_install/templates/nginx.conf dest=/home/yx/server/nginx/config/ owner=yx group=yx mode=0644
  - name: 拷贝测试主机文件
    template: src=/home/yx/nginx_install/templates/vhost.conf dest=/home/yx/server/nginx/config/vhost/ owner=yx group=yx mode=0644
  - name: 设置指定的首页展示html
    template: src=/home/yx/nginx_install/templates/index.html dest={{ nginx_web_dir }}/vhost/index.html owner={{ nginx_user }} group={{ nginx_user }} mode=0644
  - name: 设置指定的首页展示php
    template: src=/home/yx/nginx_install/templates/index.php dest={{ nginx_web_dir }}/vhost/ owner={{ nginx_user }} group={{ nginx_user }} mode=0644
  #handlers:
  - name: 启动nginx服务
    shell: sudo /home/yx/server/nginx/sbin/nginx


# vars指定参数   with_items:是循环创建目录

然后执行 ansible-playbook install_nginx.yaml -s -K
解释
-s 使用sudo
-K 提示密码使用sudo,sudo表示提权操作

二 playbook安装mysql

2.1 编译安装mysql

前期说明文件存放位置

# 存放mysql安装包
ls files/
mysql-boost-5.7.19.tar.gz
#####配置文件和脚本
ls templates/
install_mysql.sh  my.cnf  mysqld

ploybook文件

- hosts: 192.168.10.227
  remote_user: root
  vars:
  - mysql_version: 5.7.19
  - mysql_dir: /home/yx/server/mysql
  - mysql_user: yx
  - ip: 192.168.6.220
  tasks:
  - name: 安装依赖环境
    shell: sudo yum install -y ncurses-devel cmake
  - name: 创建mysql各自目录
    shell: mkdir -p {/home/yx/server/mysql/tmp,/home/yx/server/mysql/mysqldata,/home/yx/server/mysql/etc,/home/yx/server/mysql/mysqllog/binlog}
  - name: 拷贝文件
    copy: src=/home/yx/mysql_install/files/mysql-boost-5.7.19.tar.gz  dest=/tmp/ owner=yx group=yx
  - name: 解压
    shell: tar -zxf /tmp/mysql-boost-{{ mysql_version }}.tar.gz -C /tmp
  - name: 开始编译并安装
    shell: cd /tmp/mysql-{{ mysql_version }}/ && bash /home/yx/mysql_install/templates/install_mysql.sh
  - name: 更改新创建目录的属主为yx
    shell: sudo chown -R yx:yx {{ mysql_dir }}
  - name: 删除解压完的mysql目录
    shell: rm -rf /tmp/mysql-{{ mysql_version }}
  - name: 初始化数据库
    shell: /home/yx/server/mysql/bin/mysqld --initialize-insecure --user=yx --basedir=/home/yx/server/mysql --datadir=/home/yx/server/mysql/mysqldata

  - name: 拷贝指定mysql配置文件 
    template: src=/home/yx/mysql_install/templates/my.cnf dest=/home/yx/server/mysql/etc/ owner=yx group=yx mode=0644
  - name: 拷贝mysql启动脚本
    template: src=/home/yx/mysql_install/templates/mysqld dest=/etc/init.d/
  - name: 添加权限
    shell: sudo chmod +x /etc/init.d/mysqld
  - name: 启动mysql
    shell: /etc/init.d/mysqld start

执行 ansible-playbook install_mysql.yaml

安装mysql脚本

#!/bin/bash

cmake -DCMAKE_INSTALL_PREFIX=/home/yx/server/mysql \
-DMYSQL_DATADIR=/home/yx/server/mysql/mysqldata \
-DSYSCONFDIR=/home/yx/server/mysql/etc \
-DWITH_BOOST=boost \
-DMYSQL_USER=yx \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
-DMYSQL_TCP_PORT=3357 \
-DENABLED_LOCAL_INFILE=1 \
-DENABLE_DOWNLOADS=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_DEBUG=0 \
-DMYSQL_MAINTAINER_MODE=0 \
-DWITH_SSL:STRING=bundled \
-DWITH_ZLIB:STRING=bundled

sleep 2
make -j 2 && make install

mysql配置文件

[mysqld]
user=yx
port=3357
server-id=10
relay-log = mysql-relay-bin
log-bin=/home/yx/server/mysql/mysqllog/binlog/binlog
binlog-format = ROW
max_binlog_size = 200M
expire_logs_days = 15

relay-log = /home/yx/server/mysql/mysqllog/relay/relay-bin
relay-log-index = /home/yx/server/mysql/mysqllog/relay/relay-bin.index


gtid_mode=ON
log-slave-updates=ON
enforce-gtid-consistency=ON

slave-parallel-type = LOGICAL_CLOCK
slave_parallel_workers = 16
master_info_repository = TABLE
relay_log_info_repository = TABLE
relay_log_recovery = ON

slow-query-log=1
long_query_time=1
innodb_buffer_pool_dump_now = on
innodb_buffer_pool_load_now = on
innodb_buffer_pool_dump_at_shutdown = on
innodb_buffer_pool_load_at_startup = on


pid-file=/home/yx/server/mysql/mysql.pid
socket = /var/lib/mysql/mysql.sock
wait_timeout = 180
interactive_timeout = 28800
max_connections = 10000
max_allowed_packet = 1024M
max_user_connections = 13000
innodb_thread_concurrency = 24
innodb_buffer_pool_size = 48G
#innodb_additional_mem_pool_size = 50M 此参数在5.7版本已经被废弃。
innodb_log_buffer_size = 20M
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_log_files_in_group = 3
sync_binlog = 0
innodb_flush_log_at_trx_commit = 2
innodb_flush_method = O_DIRECT
query_cache_type = 1
query_cache_size = 128M
#query_cache_size = 1G
query_cache_limit = 2M
query_cache_min_res_unit = 1336
key_buffer_size = 2G
read_buffer_size = 64M
read_rnd_buffer_size = 32M
sort_buffer_size = 16M
max_tmp_tables = 1G
tmp_table_size = 1G
table_open_cache = 10000
thread_cache_size = 30
max_heap_table_size = 128M
query_cache_min_res_unit = 1336

general_log = OFF
general_log_file = /home/yx/server/mysql/mysqllog/access.log
log-error = /home/yx/server/mysql/mysqllog/error.log
slow-query-log-file = /home/yx/server/mysql/mysqllog/slow.log


sql_mode = ''


mysql启动脚本

见附件 https://files.cnblogs.com/files/huningfei/mysqld.zip

posted @ 2020-04-20 18:28  huningfei  阅读(271)  评论(0编辑  收藏  举报
levels of contents