目录
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