ansible playbook安装nacos

目录

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

2.创建rocketmq所需目录任务

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

[4. 解压nacos](#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
相关推荐
墨水\\15 小时前
Ansible部署及基础模块
服务器·网络·ansible
七七powerful15 小时前
ansible play-book玩法
linux·服务器·ansible
Karoku0662 天前
【自动化部署】Ansible循环
linux·运维·数据库·docker·容器·自动化·ansible
W u 小杰2 天前
Ansible基本用法
服务器·网络·ansible
运维小文2 天前
ansible剧本快速上手
linux·运维·python·自动化·ansible·幂等性·剧本
记得多喝水o2 天前
Ansible自动化运维 技术与最佳实践
运维·自动化·ansible
Karoku0662 天前
【自动化部署】Ansible Playbook 基础应用
运维·网络·docker·容器·自动化·ansible·consul
运维小文4 天前
ansible的流程控制
python·自动化·ansible·脚本
高hongyuan6 天前
Ansible 简介及常用命令 安装部署tomcat -单机版
运维·自动化·ansible
澜堇6 天前
深入理解 Ansible Playbook:组件与实战
linux·运维·ansible