当前位置: 首页 > article >正文

ansible playbook安装nacos

目录

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 Exists
  group:
    name: nacos
    state: present

- name: Create nacos user
  user:
    name: nacosuser
    password: "{{ plaintext_password }}"
    system: yes
    createhome: yes
    group: nacos
    state: present

- name: Grant sudo privileges to nacosuser
  lineinfile:
      path: /etc/sudoers
      state: present
      regexp: '^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 Structure
  file:
    path: "{{ item.path }}"
    state: directory
    mode: "{{ item.mode }}"
    owner: nacosuser
    group: nacos

  loop:
    - { 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 index
  apt:
    update_cache: yes

# 校验是否已经安装jdk1.8
- name: Check if JDK 1.8 is installed
  ansible.builtin.command: java -version
  register: java_version
  changed_when: false
  failed_when: false

# 安装jdk1.8
- name: Install OpenJDK 1.8
  apt:
    name: openjdk-8-jdk
    state: present
  when: java_version.stdout is not search("1.8")

# java home环境配置
- name: Ensure JAVA_HOME is set in /etc/profile
  lineinfile:
    dest: /etc/profile
    line: '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/profile
  lineinfile:
    dest: /etc/profile
    line: 'export PATH=$PATH:$JAVA_HOME/bin'
    regexp: 'export PATH=.*\$JAVA_HOME/bin'

# 上传nacos安装包
- name: Upload Nacos package
  become: yes # 使用become获取root 权限
  copy:
    src: "{{ nacos_package_path }}"
    dest: "{{ nacos_package_path }}"
    owner: nacosuser
    group: nacos
    mode: '0644'

- name: Ensure Python and pip are installed
  ansible.builtin.package:
    name:
      - python3
      - python3-pip
    state: present

- name: Ensure python3-venv is installed
  ansible.builtin.package:
    name: "python3-venv"
    state: present
      
- name: Create a virtual environment
  ansible.legacy.command:
    cmd: "{{python_path}} -m venv {{python_venv_path}}"
    creates: "{{python_venv_path}}/bin/pip"

- name: Install PyMySQL in the virtual environment
  ansible.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_interpreter
  ansible.builtin.set_fact:
    ansible_python_interpreter: "{{ python_venv_path }}/bin/python"


# 这个安装之后解决账号密码相关报错
- name: Install cryptography package using pip command
  ansible.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 package
  unarchive:
    src: "{{ nacos_package_path }}"
    dest: "{{ nacos_extract_dir }}"
    remote_src: yes
  tags: [extract]

5.创建nacos数据库

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

- name: Create a Nacos database
  community.mysql.mysql_db:
    name: "{{new_database}}"
    state: present
    login_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 privileges
  community.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: false
    state: present
  run_once: true

- name: Flush MySQL privileges
  community.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 file
  become: yes # 使用become获取root 权限
  copy:
    src: "{{sql_file_path}}"  # 源文件路径
    dest: "{{sql_file_dest_path}}"  # 目标文件路径
    mode: '0644'

# 上传集群配置文件
- name: Create Cluster conf file
  become: yes # 使用become获取root 权限
  template:
    src: "{{cluster_conf_j2_file}}"   # 源文件路径
    dest: "{{cluster_conf_dest_path}}"  # 目标文件路径
    mode: '0644'

# 上传集群配置文件
- name: Cover Application Properties file
  become: yes # 使用become获取root 权限
  copy:
    src: "{{application_properties_path}}"  # 源文件路径
    dest: "{{application_properties_dest_path}}"  # 目标文件路径
    mode: '0644'

# 上传logback日志配置文件
- name: Cover Logback file
  become: 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 contents
  ansible.builtin.slurp:
    src: "{{ sql_file_dest_path }}"
  register: sql_file_content
  run_once: true

- name: Split SQL statements and execute each one
  ansible.builtin.set_fact:
    sql_statements: "{{ sql_file_content['content'] | b64decode | split(';')}}"
  run_once: true

- name: Execute Create Table SQL statement
  community.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 > 0
  run_once: true

8.启动nacos

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

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

- name: Check Nacos startup result
  ansible.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 cluster
  hosts: nacos
  become: yes
  roles:
    - role: nacos
      cluster_conf_j2_file: "{{ hostvars[inventory_hostname]['cluster_conf_j2_file'] }}"  # 对应的配置文件
  vars:
    login_user: "root"
    login_password: "xxxxx"
    mysql_host: 10.xxx.10.xx
    mysql_port: 3306
    new_user_password: "xxxxxxxx" #nacos管理页面

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

ansible-playbook nacos.yml


http://www.kler.cn/a/406908.html

相关文章:

  • 电子电气架构 ---漫谈车载网关
  • Parker派克防爆电机在实际应用中的安全性能如何保证?
  • 【如何用更少的数据作出更好的决策】-gpt生成
  • 【题解】—— LeetCode一周小结46
  • centos安装jenkins
  • 【Vue】Vue指令
  • 华为HCCDA云技术认证--分布式云架构
  • 【论文笔记】LLaVA-o1: Let Vision Language Models Reason Step-by-Step
  • FastApi教程
  • 力扣 76. 最小覆盖子串
  • Java项目部署的三个阶段:java -jar、Docker和Kubernetes
  • 【H2O2|全栈】JS进阶知识(六)ES6(2)
  • HAL库的简单介绍以及环境搭建
  • 《生成式 AI》课程 作业6 大语言模型(LLM)的训练微调 Fine Tuning -- part2
  • 【环境配置】ubuntu下的保持程序一直运行
  • 【工具变量】上市公司企业信贷可得性数据(2000-2022年)
  • Unity图形学之CubeMap立方体贴图
  • 装饰器模式 (Decorator Pattern)
  • 设计模式-创建型-单例模式
  • ssm面向品牌会员的在线商城小程序
  • 【SQL Server】华中农业大学空间数据库实验报告 实验四 完整性约束
  • IDEA2024 maven构建跳过测试
  • 【跳线帽】是什么?怎么用?
  • AIVA 技术浅析(五):使用的自然语言处理(NLP)技术浅析
  • mac homebrew国内镜像源安装
  • SpringBoot社团管理:数据驱动的解决方案