ansible playbook安装nacos

server/2024/11/27 11:58:35/

目录

1.创建nacos应用相关的用户

2.创建rocketmq所需目录任务

3.安装JDK1.8、python以及虚拟环境安装PyMysql(用于执行数据库脚本)

4. 解压nacos

5.创建nacos数据库

6.上传sql文件

7.创建数据库表

8.启动nacos

9.nacos集群主机ip配置

10.变量配置

11.配置nacos.yml


更详细的ansible安装信息可以参考rocketmq用ansible安装的这篇文章

https://blog.csdn.net/qq_38313984/article/details/143470663?sharetype=blogdetail&sharerId=143470663&sharerefer=PC&sharesource=qq_38313984&spm=1011.2480.3001.8118

1.创建nacos应用相关的用户

在/home/ansible/roles/nacos/tasks目录下,创建user_and_group.yml

- name: Ensure nacos Group Existsgroup:name: nacosstate: present- name: Create nacos useruser:name: nacosuserpassword: "{{ plaintext_password }}"system: yescreatehome: yesgroup: nacosstate: present- name: Grant sudo privileges to nacosuserlineinfile:path: /etc/sudoersstate: presentregexp: '^nacosuser'line: 'nacosuser ALL=(ALL:ALL) ALL'validate: 'visudo -cf %s'

2.创建rocketmq所需目录任务

在/home/ansible/roles/nacos/tasks目录下,创建directories.yml

# 创建rocketmq所需的目录结构
- name: Create Nacos Directory Structurefile:path: "{{ item.path }}"state: directorymode: "{{ item.mode }}"owner: nacosusergroup: nacosloop:- { path: "/opt/nacos", mode: '0755' }  # nacos安装包- { path: "/online/nacos", mode: '0755' }  # nacos解压目录- { path: "{{ python_venv_path }}",mode: '0755' } #python虚拟路径- { path: "/data/logs/nacos/on",mode: '0755' } # 日志存放路径- { path: "/data/logs/nacos/off",mode: '0755' } # 日志归档路径

3.安装JDK1.8、python以及虚拟环境安装PyMysql(用于执行数据库脚本)

在/home/ansible/roles/nacos/tasks目录下,创建install.yml

# 更新APT的软件包索引
- name: Update apt package indexapt:update_cache: yes# 校验是否已经安装jdk1.8
- name: Check if JDK 1.8 is installedansible.builtin.command: java -versionregister: java_versionchanged_when: falsefailed_when: false# 安装jdk1.8
- name: Install OpenJDK 1.8apt:name: openjdk-8-jdkstate: presentwhen: java_version.stdout is not search("1.8")# java home环境配置
- name: Ensure JAVA_HOME is set in /etc/profilelineinfile:dest: /etc/profileline: 'export JAVA_HOME={{java_home}}'regexp: 'export JAVA_HOME='  # This ensures the line is unique and avoids duplicates# java bin目录配置
- name: Ensure JAVA_HOME/bin is in PATH in /etc/profilelineinfile:dest: /etc/profileline: 'export PATH=$PATH:$JAVA_HOME/bin'regexp: 'export PATH=.*\$JAVA_HOME/bin'# 上传nacos安装包
- name: Upload Nacos packagebecome: yes # 使用become获取root 权限copy:src: "{{ nacos_package_path }}"dest: "{{ nacos_package_path }}"owner: nacosusergroup: nacosmode: '0644'- name: Ensure Python and pip are installedansible.builtin.package:name:- python3- python3-pipstate: present- name: Ensure python3-venv is installedansible.builtin.package:name: "python3-venv"state: present- name: Create a virtual environmentansible.legacy.command:cmd: "{{python_path}} -m venv {{python_venv_path}}"creates: "{{python_venv_path}}/bin/pip"- name: Install PyMySQL in the virtual environmentansible.legacy.command:cmd: "{{python_venv_path}}/bin/pip install PyMySQL"creates: "{{ python_venv_path }}/lib/python3.x/site-packages/PyMySQL"# 虚拟环境安装pip3、PyMysql(需要在虚拟环境安装PyMysql)
- name: Set ansible_python_interpreteransible.builtin.set_fact:ansible_python_interpreter: "{{ python_venv_path }}/bin/python"# 这个安装之后解决账号密码相关报错
- name: Install cryptography package using pip commandansible.legacy.command:cmd: "{{python_venv_path}}/bin/pip install cryptography"creates: "{{ python_venv_path }}/lib/python3.x/site-packages/cryptography"

4. 解压nacos

在/home/ansible/roles/nacos/tasks目录下,创建extract.yml

# 解压下载的rocket安装包
- name: Extract Nacos packageunarchive:src: "{{ nacos_package_path }}"dest: "{{ nacos_extract_dir }}"remote_src: yestags: [extract]

5.创建nacos数据库

在/home/ansible/roles/nacos/tasks目录下,创建create_nacos_db_and_user.yml

- name: Create a Nacos databasecommunity.mysql.mysql_db:name: "{{new_database}}"state: presentlogin_host: "{{ mysql_host }}"login_port: "{{ mysql_port }}"login_user: "{{login_user}}"login_password: "{{login_password}}"run_once: true- name: Create a new MySQL user with full privilegescommunity.mysql.mysql_user:name: "{{ new_database_user }}"password: "{{ new_user_password }}"host: "%"  # 允许从任何主机连接priv: "{{ new_database }}.*:ALL"  login_host: "{{ mysql_host }}"login_port: "{{ mysql_port }}"      login_user: "{{login_user}}"login_password: "{{ login_password }}"column_case_sensitive: falsestate: presentrun_once: true- name: Flush MySQL privilegescommunity.mysql.mysql_query:login_host: "{{ mysql_host }}"login_port: "{{ mysql_port }}"login_user: "{{login_user}}"login_password: "{{ login_password }}"query: "FLUSH PRIVILEGES;"run_once: true

6.上传sql文件

在/home/ansible/roles/nacos/tasks目录下,创建upload_file.yml

# 上传创建nacos相关表的sql文件
- name: Upload dashboard jar filebecome: yes # 使用become获取root 权限copy:src: "{{sql_file_path}}"  # 源文件路径dest: "{{sql_file_dest_path}}"  # 目标文件路径mode: '0644'# 上传集群配置文件
- name: Create Cluster conf filebecome: yes # 使用become获取root 权限template:src: "{{cluster_conf_j2_file}}"   # 源文件路径dest: "{{cluster_conf_dest_path}}"  # 目标文件路径mode: '0644'# 上传集群配置文件
- name: Cover Application Properties filebecome: yes # 使用become获取root 权限copy:src: "{{application_properties_path}}"  # 源文件路径dest: "{{application_properties_dest_path}}"  # 目标文件路径mode: '0644'# 上传logback日志配置文件
- name: Cover Logback filebecome: yes # 使用become获取root 权限copy:src: "{{logback_path}}"  # 源文件路径dest: "{{logback_dest_path}}"  # 目标文件路径mode: '0644'

7.创建数据库表

在/home/ansible/roles/nacos/tasks目录下,创建init_database.yml

- name: Read SQL file contentsansible.builtin.slurp:src: "{{ sql_file_dest_path }}"register: sql_file_contentrun_once: true- name: Split SQL statements and execute each oneansible.builtin.set_fact:sql_statements: "{{ sql_file_content['content'] | b64decode | split(';')}}"run_once: true- name: Execute Create Table SQL statementcommunity.mysql.mysql_query:login_host: "{{ mysql_host }}"login_port: "{{ mysql_port }}"login_user: "{{ login_user }}"login_password: "{{ login_password }}"login_db: "{{ new_database }}"query: "{{ item }}"loop: "{{ sql_statements }}"when: item | trim | length > 0run_once: true

8.启动nacos

在/home/ansible/roles/nacos/tasks目录下,创建start.yml

- name: Execute Nacos startup scriptansible.builtin.shell:cmd: "cd {{nacos_extract_dir}}/nacos/bin && sh startup.sh"environment:JAVA_HOME: '{{java_home}}' #得指定java版本,测试环境有多个java版本register: nacos_startfailed_when: nacos_start.rc != 0  # 如果启动脚本返回非0状态码,则认为任务失败 - name: Check Nacos startup resultansible.builtin.debug:var: nacos_start.stdout

9.nacos集群主机ip配置

在/home/ansible/inventory/hosts中增加配置

[nacos]
nacos_node1 ansible_host=10.xx.x.1x cluster_conf_j2_file=cluster.conf.dev.j2
nacos_node2 ansible_host=10.xx.x.1x  cluster_conf_j2_file=cluster.conf.dev.j2
nacos_node3 ansible_host=10.xx.x.1x  cluster_conf_j2_file=cluster.conf.dev.j2

在/home/ansible/roles/nacos/templates目录下,创建cluster.conf.dev.j2,生产环境为cluster.conf.prod.j2

10.xx.x.1x:8848
10.xx.x.1x:8848
10.xx.x.1x:8848

10.变量配置

/home/ansible/roles/nacos/defaults的main.yml文件中配置

nacos_version: "2.4.0"
plaintext_password: xxxxxxxx
# 控制节点nacos安装包上传路径
nacos_package_path: /opt/nacos/nacos-server-{{nacos_version}}.tar.gz
# 被控节点nacos安装包解压路径
nacos_extract_dir: /online
#mysql配置
new_database: "nacos_db"
new_database_user: "nacos"
sql_file_path: /opt/nacos/mysql-schema.sql
sql_file_dest_path: /online/nacos/mysql-schema.sql# 集群文件路径配置
cluster_conf_dest_path: /online/nacos/conf/cluster.conf
# 数据库及密钥文件
application_properties_path: /opt/nacos/application.properties
application_properties_dest_path: /online/nacos/conf/application.properties#python虚拟路径
python_venv_path: /online/python/venv
#python路径
python_path: /usr/bin/python3
# java路径
java_home: /usr/lib/jvm/java-8-openjdk-amd64# logback文件
logback_path: /opt/nacos/nacos-logback.xml
logback_dest_path: /online/nacos/conf/nacos-logback.xml

11.配置nacos.yml

/home/ansible/playbooks/dev/middleware中配置nacos.yml

---
- name: Install and configure Nacos clusterhosts: nacosbecome: yesroles:- role: nacoscluster_conf_j2_file: "{{ hostvars[inventory_hostname]['cluster_conf_j2_file'] }}"  # 对应的配置文件vars:login_user: "root"login_password: "xxxxx"mysql_host: 10.xxx.10.xxmysql_port: 3306new_user_password: "xxxxxxxx" #nacos管理页面

配置完上述项,在/home/ansible/playbooks/dev/middleware中,执行

ansible-playbook nacos.yml


http://www.ppmy.cn/server/145327.html

相关文章

C# 结构体

文章目录 前言一、结构体的定义与基本使用(一)定义结构体(二)结构体的使用示例 二、C# 结构的特点(一)丰富的成员类型(二)构造函数相关限制与特性(三)继承方面…

【Android】android compat理解

1,前提 即便是在同一手机上安装的不同apk,其编译的apk不同,也会导致行为上的差异。如SDK34有限制后台启动,但如果安装的apk所依赖的sdk是33,则不会表现出此差异。这是如何实现的呢?其实,本质是…

分布式kettle调度平台v6.4.0新功能介绍

介绍 Kettle(也称为Pentaho Data Integration)是一款开源的ETL(Extract, Transform, Load)工具,由Pentaho(现为Hitachi Vantara)开发和维护。它提供了一套强大的数据集成和转换功能&#xff0c…

day01

Hm-Footer.vue <template><div class"hm-footer">我是hm-footer</div></template><script>export default {}</script><style>.hm-footer{height:100px;line-height:100px;text-align:center;font-size:30px;background-…

Java学习,重载(Overload)

Java中&#xff0c;重载&#xff08;Overload&#xff09;是指同一个类中&#xff0c;可以有多个方法有相同的名称&#xff0c;但这些方法的参数列表必须不同。参数列表的不同可以体现在参数的个数、参数的类型或者参数的顺序上。重载使得一个类可以以多种方式被调用&#xff0…

解决 node.js 执行 npm下载 报无法执行脚本的错

报错如下 这个问题其实就是本地策略组造成的 策略的查看是 Get-ExecutionPolicy -List 看一下本地策略是什么情况 正常能 执行脚本 的情况如下&#xff08;这个就是微软的安全策略&#xff09; 如果你的这些全是 Undefined那就中招了 Undefined 当前 scope中没有…

用天翼云搭建一个HivisionIDPhoto证件照处理网站

世人不必记我&#xff0c;我不记世人。 HivisionIDPhoto证件照处理网站 世人不必记我&#xff0c;我不记世人。项目地址项目搭建与修改前端后端遇到的坑 成果图 前段时间工作需要频繁处理证件照&#xff0c;当时同事推荐一个证件照小程序&#xff08;要看广告&#xff09;&…

数字排列(Java Python JS C++ C )

题目描述 小明负责公司年会,想出一个趣味游戏: 屏幕给出 1 ~ 9 中任意 4个不重复的数字,大家以最快时间给出这几个数字可拼成的数字从小到大排列位于第 N 位置的数字,其中 N 为给出数字中最大的(如果不到这么多数字则给出最后一个即可)。 注意: 2 可以当作 5 来使用,…