项目:ansible实现LNMT环境部署(arm环境)
MacBook pro apple m1pro虚拟机环境:Ubuntu22 (ARM)操作ansible部署LNMT环境的部署
目录
前言
Ansible 是一个开源的自动化平台,用于配置管理、应用部署和自动化任务执行
将手动部署的操作转换成yaml的形式,使用一条ansible的命令,就可以一键部署操作完成
提供诸多便利,提高工作运维效率
以下,我将通过role角色来实现LNMT环境的部署
电脑环境:MacBook pro apple m1pro
虚拟机环境:Ubuntu22 (ARM)
一、使用role角色实现Nginx部署
(一)手动部署Nginx操作流程
## 编译安装nginx
## 官网下载安装包:
root@nginx-40:/usr/local/src#wget http://nginx.org/download/nginx-1.25.5.tar.gz
## 下载安装依赖包
root@nginx-40:/usr/local/src# apt-update
root@nginx-40:/usr/local/src# apt-get install make -y
root@nginx-40:/usr/local/src# apt-get install make-guile -y
root@nginx-40:/usr/local/src# apt-get install gcc -y
root@nginx-40:/usr/local/src# apt-get install libpcre3 -y
root@nginx-40:/usr/local/src# apt-get install libpcre3-dev -y
root@nginx-40:/usr/local/src# apt-get install libssl-dev -y
root@nginx-40:/usr/local/src# apt-get install zlib1g-dev -y
## 添加nginx用户和用户组
root@nginx-40:/usr/local/src# groupadd -g 88 nginx
root@nginx-40:/usr/local/src# useradd -g nginx -M -s /sbin/nologin -u 88 nginx
## 解压并进入目录中
root@nginx-40:/usr/local/src# tar -xf nginx-1.25.5.tar.gz
root@nginx-40:/usr/local/src# cd nginx-1.25.5/
## 编译
root@nginx-40:/usr/local/src/nginx-1.25.5# ./configure --user=nginx --group=nginx \
--prefix=/usr/local/nginx --with-http_stub_status_module \
--with-http_sub_module --with-http_ssl_module \
--with-pcre --with-stream
## 安装
root@nginx-40:/usr/local/src/nginx-1.25.5# make && make install
## 编译后查看
root@nginx-40:/usr/local/src/nginx-1.25.5# cd /usr/local/nginx/
root@nginx-40:/usr/local/nginx# ls
conf html logs sbin
## 启动、停止、重载服务
root@nginx-40:/usr/local/nginx# /usr/local/nginx/sbin/nginx
root@nginx-40:/usr/local/nginx# /usr/local/nginx/sbin/nginx -s stop
root@nginx-40:/usr/local/nginx# /usr/local/nginx/sbin/nginx -s reload
(二)手动部署转换ansible的部署
# 创建角色
root@admin-111:/data/playbook/roles# ansible-galaxy init nginx-install
- Role mysql-intall was created successfully
# 默认没有 templates 和file 目录 -- 需要自行创建
root@admin-111:/data/playbook/roles/nginx-install# mkdir templates files
# 目录架构
root@admin-111:/data/playbook/roles# tree
.
├── nginx-install # role角色
│ ├── README.md
│ ├── defaults
│ │ └── main.yml
│ ├── files
│ │ └── nginx-1.25.5.tar.gz
│ ├── handlers
│ │ └── main.yml
│ ├── meta
│ │ └── main.yml
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ │ ├── index.html.j2
│ │ └── nginx.conf.j2
│ ├── tests
│ │ ├── inventory
│ │ └── test.yml
│ └── vars
│ └── main.yml
└── nginx_install.yaml # 主文件
# 主文件:
指定执行role的角色 - ansible-galaxy创建的角色名称
指定执行的权限,root
root@admin-111:/data/playbook/roles# cat nginx_install.yaml
---
- hosts: nginx1
remote_user: root
roles:
- nginx-install # 指定role角色
# 全局变量定义
root@admin-111:/data/playbook/roles/nginx-install# cat vars/main.yml
# vars file for nginx-install
# file_dir: /data/playbook/nginx-1.25.5.tar.gz
nginx_ops: "/usr/local/src/configure --user=nginx \
--group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module \
--with-http_sub_module --with-http_ssl_module --with-pcre --with-stream"
# 主任务,主要执行的操作步骤
root@admin-111:/data/playbook/roles# cat nginx-install/tasks/main.yml
---
# tasks file for nginx-install
- name: update package
shell: apt-get update- name: install package
apt:
name: ["make","make-guile", "gcc","libpcre3","libpcre3-dev","zlib1g-dev","libssl-dev","zlib1g-dev"]
state: present
# disable_gpg_check: yes- name: create group
group:
name: nginx
state: present
gid: 80- name: create user
user:
name: nginx
uid: 80
group: nginx
system: yes
shell: /sbin/nologin- name: 解压并推送到目录中
unarchive:
src: nginx-1.25.5.tar.gz
dest: /usr/local/src- name: 切换进入目录进行编译
# shell: cd /usr/local/src/nginx-1.25.5 && "{{ nginx_ops }}" && make && make install
shell: ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx \
--with-http_stub_status_module --with-http_sub_module \
--with-http_ssl_module --with-pcre --with-stream &&
make && make install
args:
chdir: /usr/local/src/nginx-1.25.5
creates: /usr/local/nginx/sbin/nginx # 确定不存在,避免重复编译
become: yes # 确保root权限编译- name: 提供配置文件模版
template:
src: nginx.conf.j2
dest: /usr/local/nginx/conf/nginx.conf
owner: nginx
group: nginx
mode: 644- name: 创建访问目录
file:
path: "{{ item }}"
state: directory
loop:
- /data/web-data/hello.com
- /data/web-data/logs- name: 提供访问界面
template:
src: index.html.j2
dest: /data/web-data/hello.com/index.htnl
owner: root
group: root
mode: 644
notify: start_nginx # 如果task没有发生变化,不会执行,只有第一次执行- name: check port
shell: netstat -nutlp
register: seeport- name: 输出查看端口
debug:
msg={{ seeport }}
# 提供app,安装文件
root@admin-111:/data/playbook/roles/nginx-install# ls files/
nginx-1.25.5.tar.gz
# 编写调度器,task任务中如有调用才会执行
root@admin-111:/data/playbook/roles/nginx-install# cat handlers/main.yml---
# handlers file for nginx-install
- name: start_nginx
shell:
free_form: /usr/local/nginx/sbin/nginx
removes: /usr/local/nginx/sbin/nginx # 文件不存在则不执行- name: check port
shell: netstat -nutlp
## 提供配置文件,一般配置文件模版都是放在templates目录中,以.j2形式存放
root@admin-111:/data/playbook/roles/nginx-install# ls templates/
index.html.j2 nginx.conf.j2
# index.html.j2文件
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="http://baidu.com/">百度</a>.<br/>
Commercial support is available at
<a href="https://www.cnblogs.com/liuqingzheng">博客园李老师笔记</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>
# nginx.conf.j2文件
user nginx nginx;
worker_processes 1;
error_log logs/error.log;
pid logs/nginx.pid;events {
worker_connections 1024;
}http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
charset utf-8;server {
listen 80;
server_name www.hello.com;
access_log /data/web-data/logs/hello.com.access.log main;
location / {
root /data/web-data/hello.com;
index index.html;
}
}}
如果没有其他需求,其他文件或目录基本不用动,如果有,请自行添加
# 执行前检查语法
root@admin-111:/data/playbook/roles# ansible-playbook --syntax-check nginx_install.yaml
playbook: nginx_install.yaml
# 检查语法没有报错,在当前的role目录执行Nginx角色,即可完成一键部署Nginx
root@admin-111:/data/playbook/roles# ansible-playbook nginx_install.yaml
二、使用role角色实现Tomcat部署
(一)手动部署Tomcat操作流程
# 官网下载tomcat
root@nginx-41:/usr/local/src# wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.23/bin/apache-tomcat-10.1.23.tar.gz
# 官网下载jdk21:https://download.oracle.com/java/21/latest/jdk-21_linux-aarch64_bin.tar.gz
这个需要登录,无法使用wget下载
# 部署jdk21:解压压缩包,移动至/usr/local,配置环境变量
root@nginx-41:/usr/local/src# tar -xf jdk-21_linux-aarch64_bin.tar.gz
root@nginx-41:/usr/local/src# mv jdk-21.0.2 /usr/local/jdk
root@nginx-41:/usr/local# vim /etc/profile
........
# jdk
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib:/test
export PATH=$JAVA_HOME/bin:$PATH
# tomcat
export TOMCAT_HOME=/usr/local/tomcat
# 部署tomcat :解压压缩包,移动文件并更改名字(不推荐link),启动tomcat
root@nginx-41:/usr/local/src# tar -xf apache-tomcat-10.1.23.tar.gz
root@nginx-41:/usr/local/src# mv apache-tomcat-10.1.23 /usr/local/tomcat
root@nginx-41:/usr/local/src# /usr/local/tomcat/bin/startup.sh
# 查看tomcat端口 8080 8005
root@nginx-41:/usr/local# netstat -nutlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 45646/nginx: master
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 712/systemd-resolve
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 31846/sshd: /usr/sb
tcp6 0 0 :::22 :::* LISTEN 31846/sshd: /usr/sb
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 46433/java
tcp6 0 0 :::8080 :::* LISTEN 46433/java
udp 0 0 127.0.0.53:53 0.0.0.0:* 712/systemd-resolve访问:http://192.168.10.41:8080/
至此,tomcat部署完成!!!
# ERROR:tomcat 起不来
root@nginx-41:/usr/local/tomcat/logs# cat catalina.out
Unrecognized option: --add-opens=java.base/java.lang=ALL-UNNAMED
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
Unrecognized option: --add-opens=java.base/java.lang=ALL-UNNAMED
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
# tomcat起不来的原因:使用的是tomcat10,jdk版本必须在11以上,这个是jdk8
root@nginx-41:/usr/local/tomcat/logs# java -version
java version "1.8.0_391"
Java(TM) SE Runtime Environment (build 1.8.0_391-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.391-b13, mixed mode)
(二)手动部署转换ansible部署
# 创建角色
root@admin-111:/data/playbook/roles# ansible-galaxy init tomcat-install
- Role mysql-intall was created successfully
# 默认没有 templates 和file 目录 -- 需要自行创建
root@admin-111:/data/playbook/roles/tomcat-install# mkdir templates files
# 目录框架
root@admin-111:/data/playbook/roles# tree
.
├── tomcat-install # role角色
│ ├── README.md
│ ├── defaults
│ │ └── main.yml
│ ├── files
│ │ ├── apache-tomcat-10.1.23.tar.gz
│ │ ├── jdk-21_linux-aarch64_bin.tar.gz
│ │ └── source.sh
│ ├── handlers
│ │ └── main.yml
│ ├── meta
│ │ └── main.yml
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ ├── tests
│ │ ├── inventory
│ │ └── test.yml
│ └── vars
│ └── main.yml
└── tomcat_install.yaml ## 执行主文件
## 执行主文件
root@admin-111:/data/playbook/roles# cat tomcat_install.yaml
---
- hosts: nginx
remote_user: root
roles:
- tomcat-install
## app文件的一般存储在这,file目录下
root@admin-111:/data/playbook/roles/tomcat-install# ls files/
apache-tomcat-10.1.23.tar.gz jdk-21_linux-aarch64_bin.tar.gz
## 执行的task任务
root@admin-111:/data/playbook/roles/tomcat-install# cat tasks/main.yml
---
# tasks file for tomcat-install
- name: 解压jdk压缩包
unarchive:
src: jdk-21_linux-aarch64_bin.tar.gz
dest: /usr/local/- name: 修改压缩后的包名称为jdk
shell: mv /usr/local/jdk-21.0.2 /usr/local/jdk
args:
creates: /usr/local/jdk#- name: 判断jdk是否配置环境变量
# shell: cat /etc/profile | grep "jdk" # 放在配置环境变量前面,如果第一次运行没有,会报错无法进行下一步执行
# register: jdkEnv- name: 配置环境变量
lineinfile:
path: /etc/profile
line: "# jdk \nexport JAVA_HOME=/usr/local/jdk \nexport CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib:/test \nexport PATH=$JAVA_HOME/bin:$PATH \n# tomcat \nexport TOMCAT_HOME=/usr/local/tomcat"
state: present
#when: jdkEnv.stdout == ''- name: 读取环境变量文件,生效环境变量
script: source.sh # 因为用shell模块不能执行source命令,所以使用script模块执行脚本
- name: 查看java是否生效
shell: find /usr/local/jdk/bin -name "java"
register: javaexist- name: 输出java
debug:
msg: "{{ javaexist }}"- name: 解压tomcat
unarchive:
src: apache-tomcat-10.1.23.tar.gz
dest: /usr/local/- name: 修改压缩后的包名称为tomcat
shell: mv /usr/local/apache-tomcat-10.1.23 /usr/local/tomcat
args:
creates: /usr/local/tomcat # 文件存在则不执行shell- name: 运行tomcat
shell: /usr/local/tomcat/bin/startup.sh
#when: seePort.stdout == '' # 端口不存在,才执行任务- name: 查看端口
shell: netstat -nutlp | grep ":8080"
register: seePort- name: 输出tomcat端口, 是否启动
debug:
msg: "运行成功 {{ seePort }}"
## 创建脚本,因为shell模块不能执行source命令,换个执行方案
root@admin-111:/data/playbook/roles/tomcat-install/files# cat source.sh
#!/bin/bash
source /etc/profile
root@admin-111:/data/playbook/roles/tomcat-install/files# chmod +x source.sh
如果没有其他需求,其他文件不用动,如有需要,自行添加
## 测试语法
root@admin-111:/data/playbook/roles# ansible-playbook --syntax-check tomcat_install.yaml
playbook: tomcat_install.yaml
## 执行安装
root@admin-111:/data/playbook/roles# ansible-playbook tomcat_install.yaml
三、使用role角色实现MySQL部署
(一)手动部署MySQL操作流程
说明:因为使用的是arm架构的机器操作,MySQL官网对于Ubuntu arm的的软件包没有提供,这里使用 apt 安装MySQL
# 更新 包
root@nginx-40:/usr/local# apt-get update
# 安装 mysql服务端
root@nginx-40:/usr/local# apt-get install mysql-server
# 自动创建MySQL用户
root@nginx-40:/usr/local# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
....
mysql:x:107:112:MySQL Server,,,:/nonexistent:/bin/false
说明:
/bin/false是一个特殊的程序,当它被用作shell时,它会立即退出并返回非零状态码
/bin/false不提供任何交互功能,使用/bin/false或/usr/sbin/nologin作为shell是安全的
# 安装客户端
root@nginx-40:/usr/local# apt-get install mysql-client
# 安装MySQL开发工具
root@nginx-40:/usr/local# apt-get install libmysqlclient-dev
# 确认安装MySQL是否成功
root@nginx-40:/usr/local# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 775/sshd: /usr/sbin
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 727/systemd-resolve
tcp 0 0 127.0.0.1:33060 0.0.0.0:* LISTEN 2265/mysqld
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 2265/mysqld
tcp6 0 0 :::22 :::* LISTEN 775/sshd: /usr/sbin
# MySQL的目录
root@nginx-40:/usr/local# whereis mysql
mysql: /usr/bin/mysql /usr/lib/mysql /etc/mysql /usr/include/mysql /usr/share/mysqlroot@nginx-40:/usr/lib/mysql# ls /etc/mysql/
conf.d debian-start debian.cnf my.cnf my.cnf.fallback mysql.cnf mysql.conf.d
# 安装默认已经初始化,无需在初始化数据库
root@nginx-40:/usr/lib/mysql# ls /var/lib/mysql
'#ib_16384_0.dblwr' binlog.000001 ca.pem ibdata1 performance_schema sys
'#ib_16384_1.dblwr' binlog.000002 client-cert.pem ibtmp1 private_key.pem undo_001
'#innodb_redo' binlog.000003 client-key.pem mysql public_key.pem undo_002
'#innodb_temp' binlog.index debian-5.7.flag mysql.ibd server-cert.pem
auto.cnf ca-key.pem ib_buffer_pool nginx-40.pid server-key.pem
# 刚安装,root用户登录,无需密码
root@nginx-40:/usr/lib/mysql# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.36-0ubuntu0.22.04.1 (Ubuntu)Copyright (c) 2000, 2024, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
# 修改root密码(两个修改密码的方式)
mysql> alter user 'root'@'localhost' identified by "1234";
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
或
mysql> update mysql.user set authentication_string='123456' where user='root' and host='localhost';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
# 重启MySQL
root@nginx-40:/usr/lib/mysql# systemctl restart mysql
root@nginx-40:/usr/lib/mysql# mysql -uroot -p123456
(二)手动部署转换ansible部署
# 创建角色
root@admin-111:/data/playbook/roles# ansible-galaxy init mysql-intall
- Role mysql-intall was created successfully
# 默认没有 templates 和file 目录 -- 需要自行创建
root@admin-111:/data/playbook/roles/mysql-intall# mkdir templates files
# 目录架构
root@admin-111:/data/playbook/roles# tree
.
├── mysql-intall # role角色
│ ├── README.md
│ ├── defaults
│ │ └── main.yml
│ ├── files
│ ├── handlers
│ │ └── main.yml
│ ├── meta
│ │ └── main.yml
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ │ └── my.cnf.j2
│ ├── tests
│ │ ├── inventory
│ │ └── test.yml
│ └── vars
│ └── main.yml
└── mysql_install.yaml # 执行主文件
# 主文件内容
root@admin-111:/data/playbook/roles# cat mysql_install.yaml
---
- hosts: nginx1
remote_user: root
roles:
- mysql-intall
##执行的任务
root@admin-111:/data/playbook/roles/mysql-intall# cat tasks/main.yml
---
# tasks file for mysql-intall
- name: 更新仓库
shell: apt-get update
tags: a1- name: 安装MySQL-server、MySQL-client、开发工具
apt:
name: "{{ item }}"
state: present
loop:
- mysql-server
- mysql-client
- libmysqlclient-dev
tags: a2- name: 查看端口是否存在3306
shell: netstat -ntulp
register: mysqlport
tags: a3- name: 输出端口
debug:
msg: "端口3306是否开启 {{ mysqlport }}"
tags: a3- name: 提供my.cnf配置文件
template:
src: my.cnf.j2
dest: /etc/alternatives/my.cnf # 软连接 --》/etc/mysql/my.cnf
owner: root
group: root
mode: 777
notify: restart_mysql
tags: a4- name: 创建连接用户
shell: mysql -uroot -p123456 -e "create user 'xiaoxiao'@'192.168.10.%' identified by '123456'"
no_log: true # 为了安全起见,不要记录密码
notify: flush_privileges
tags: t1- meta: flush_handlers
- name: 授权
shell: mysql -uroot -p123456 -e "grant all privileges on *.* to 'xiaoxiao'@'192.168.10.%' WITH GRANT OPTION"
notify: flush_privileges
tags: t2- meta: flush_handlers
- name: 创建数据库
shell: mysql -uroot -p123456 -e "create database db1"
tags: t3- name: 创建表
#shell: mysql -uroot -p123456 "create table stu1(id int primary key auto_increment,name varchar(20),age smallint(3) default 18)"
shell: echo "create table db1.stu1(id int primary key auto_increment,name varchar(20),age smallint(3) default 18);" | mysql -uroot -p123456
tags: t4# 安装Python版本省略,根据自行想要的版本进行安装
- name: 安装pip3
apt:
name: python3-pip
state: present
tags: t5- name: 安装pymysql
pip:
name: pymysql
state: present
tags: t6- name: 使用mysql_query查询db1数据
mysql_query:
login_host: 192.168.10.40
login_user: xiaoxiao
login_password: 123456
login_db: db1
query: SELECT * FROM stu1;
register: result
tags: t7- name: 输出查询结果
debug:
msg: "查询结果:{{ result }}"
tags: t8
# 调度器,需要调度的任务
root@admin-111:/data/playbook/roles/mysql-intall# cat handlers/main.yml
---
# handlers file for mysql-intall
- name: restart_mysql
shell: systemctl restart mysql- name: flush_privileges
shell: mysql -uroot -p123456 -e "flush privileges"
# 提供MySQL的配置文件
root@admin-111:/data/playbook/roles/mysql-intall# cat templates/my.cnf.j2
[client]
port=3306
user=root
password=123456[][mysqld]
character-set-server=utf8mb4 #字符集相关
collation-server=utf8_general_ci
max_user_connections=0
max_connections=151
bind-address = 0.0.0.0
innodb_flush_log_at_trx_commit=2
innodb_lock_wait_timeout=30
innodb_rollback_on_timeout=ON
innodb_deadlock_detect=ON
log-error=/var/lib/mysql/mysqld.log
##慢查询日志
slow_query_log=ON
slow_query_log_file=/var/lib/mysql/slow.log
long_query_time=1.000000 #1秒
##二进制日志
log-bin=/log-/var/lib/mysql/mysql
server-id=50
binlog_format=row
binlog_rows_query_log_events=ON
max_binlog_size=1073741824
expire_logs_days=0
##写盘策略
sync_binlog=1000
##中继日志
relay-log=/var/lib/mysql/mysql/relay-log
relay-log-index=/var/lib/mysql/mysql/relay-log.index
#read_only=ON
#super_read_only=ON
relay_log_purge=OFF
如果没有其他需求,其他文件不用动
# 执行前检查语法
root@admin-111:/data/playbook/roles# ansible-playbook --syntax-check mysql_install.yaml
playbook: mysql_install.yaml
------ 根据 tags 步骤进行选择性调试指定的执行任务,调试出现的错误
# 跳过带有标签的任务,执行任务
root@admin-111:/data/playbook/roles#ansible-playbook --skip-tags tagged mysql_install.yaml
# 跳过指定任务,执行其他任务
root@admin-111:/data/playbook/roles# ansible-playbook --skip-tags="a1,a2,a3,a4" mysql_install.yaml
root@admin-111:/data/playbook/roles# ansible-playbook --skip-tags="a1,a2,a3,a4,t1,t2,t3" mysql_install.yaml
# 执行指定任务
root@admin-111:/data/playbook/roles# ansible-playbook --tags="t7,t8" mysql_install.yaml
# 最后,检查没有问题后,一键操作部署
root@admin-111:/data/playbook/roles# ansible-playbook mysql_install.yaml
(三)补充:配置主从复制
如果要配置主从复制,可以用pymysql模块的 mysql_user、mysql_replication 来操作
---
- name: "1、创建{{ user }}用户"
user:
name: "{{ user }}"
shell: /bin/bash- name: "2、创建安装目录"
file:
path: "{{ mysql_install_path }}"
state: directory
owner: "{{ user }}"
group: "{{ group }}"
recurse: yes- name: "3、解压mysql二进制包"
unarchive:
src: "{{ mysql_pkg }}"
dest: "{{ mysql_install_path }}"
owner: "{{ user }}"
group: "{{ group }}"- name: "4、创建数据目录"
file:
path: "{{ item }}"
state: directory
owner: "{{ user }}"
group: "{{ group }}"
recurse: yes
with_items:
- "{{ mysql_install_path }}/{{ mysql_version }}/data"
- "{{ mysql_install_path }}/{{ mysql_version }}/undolog"- name: "5、修改权限"
command: chown -R "{{ user }}:{{ group }}" "{{ mysql_install_path }}"- name: "6、创建链接文件"
file:
src: "{{ mysql_install_path }}/{{ mysql_version }}"
dest: "{{ mysql_install_path }}/{{ mysql_link }}"
owner: "{{ user }}"
group: "{{ group }}"
state: link- name: "7、生成配置文件"
template:
src: my.cnf.j2
dest: /etc/my.cnf- name: "8、数据库初始化"
shell: ./mysqld --initialize --user={{ user }} --basedir={{ mysql_install_path }}/{{ mysql_link }} --datadir={{ mysql_install_path }}/{{ mysql_link }}/data
args: # 相当于用shell模块的参数比如:chdir、creates、removes
chdir: "{{ mysql_install_path }}/{{ mysql_link }}/bin"- name: "9、注册初始密码"
shell: cat error.log |grep localhost|grep "temporary password"|awk '{print $NF}'
register: mysql_init_passwd
args:
chdir: "{{ mysql_install_path }}/{{ mysql_link }}/data"- name: "10、打印初始密码"
debug:
msg: "{{ mysql_init_passwd.stdout }}"- name: "11、配置systemd守护进程"
template:
src: mysqld.service.j2
dest: /usr/lib/systemd/system/mysqld.service- name: "12、启动mysqld服务"
systemd:
name: mysqld
state: started
daemon_reload: yes
enabled: yes- name: "13、修改初始密码"
shell: ./mysqladmin -u root -p"{{ mysql_init_passwd.stdout }}" password "{{ mysql_root_passwd }}"
args:
chdir: "{{ mysql_install_path }}/{{ mysql_link }}/bin"- name: "14、创建{{ repl_user }}同步用户"
mysql_user:
login_host: localhost
login_port: "{{ mysql_port }}"
login_user: root
login_unix_socket: "{{ mysql_sock }}"
login_password: "{{ mysql_root_passwd }}"
name: "{{ repl_user }}"
password: "{{ repl_passwd }}"
priv: "*.*:ALL"
state: present
host: "%"
when: master is defined- name: "15、从库配置从主库同步"
mysql_replication:
login_unix_socket: "{{ mysql_sock }}"
login_host: localhost
login_port: "{{ mysql_port }}"
login_user: root
login_password: "{{ mysql_root_passwd }}"
master_host: "{{ master_ip }}"
master_user: "{{ repl_user }}"
master_password: "{{ repl_passwd }}"
master_port: "{{ mysql_port }}"
master_auto_position: 1
mode: changemaster
when: slave is defined- name: "16、Start Slave"
mysql_replication:
login_unix_socket: "{{ mysql_sock }}"
login_user: root
login_host: localhost
login_port: "{{ mysql_port }}"
login_password: "{{ mysql_root_passwd }}"
mode: startslave
when: slave is defined- name: "17、注册复制状态"
mysql_replication:
login_host: localhost
login_user: root
login_port: "{{ mysql_port }}"
login_password: "{{ mysql_root_passwd }}"
login_unix_socket: "{{ mysql_sock }}"
mode: getslave
when: slave is defined
register: info- name: "18、打印复制状态信息"
debug:
msg: "Slave_IO_Running={{ info.Slave_IO_Running }} Slave_SQL_Running={{ info.Slave_SQL_Running }}"
when: slave is defined
四、整合一键部署LNMT
可以根据部署先后顺序,进行文件整合
执行部署:
1. 创建一个role角色,创建templates 和file 目录,将软件包和配置文件统一存放
2. 编写主文件
3. 将文件的内容整合在一块,task中可以根据自己想要软件先后安装顺序进行整合,其他的比如vars或handles可以乱序,不关紧要
如果逐一实现了单个部署Nginx、Tomcat、MySQL,整合操作还是比较容易的;
为何这里没有操作部署,因为我没有去操作实现,以上三个操作步骤我也是花了一天的时间去操作复习的ansible,如果以上有操作不当或者有什么不妥的地方,欢迎指正
至此,ansible部署LNMT的操作流程完成!!!
更多推荐
所有评论(0)