ansible组件的使用,此为未完善,只做参考

本文详细描述了如何使用Ansible脚本在Linux系统上部署Java环境,包括解压JDK、配置环境变量、安装Docker、设置MySQL数据库、以及部署FISCO应用的过程。
摘要由CSDN通过智能技术生成

下面是文件

#未完成和优化,未完成和优化,未完成和优化

---
- name: 部署Java环境,安装Docker,配置MySQL
  hosts: NOPASSWD_KEY
  become: yes  # 如果需要使用sudo,设置为yes

  vars:
    ansible_user: USER_NAME
    ansible_password: USER_PASSWD
    ansible_python_interpreter: /usr/bin/python3
    new_user_name: NEWNAME
    install_path: "/root/fisco_install/"      #按照包所在目录
    data_path: "/data/"                       #底链数据目录和部署文件目录
    local_ip: LOCALIP                         #本机ip地址,此文档数据库用
    local_port: LOCALPORT                     #数据库端口
    mysql_passwd: MYSQLPASSWD                 #数据库密码
  tasks:
    - name: 解压Java tar 包
      ansible.builtin.unarchive:
        src: "{{ install_path }}/packages/jdk-8u311-linux-x64.tar.gz"      #安装包目录
        dest: /usr/local/
        owner: root
        group: root
        mode: '0755'
    - name: 备份目标目录内容
      ansible.builtin.command: mv /usr/local/jdk1.8 /usr/local/jdk1.8_backup
      ignore_errors: true

    - name: 移动和重命名解压后的目录
      ansible.builtin.command:
        cmd: mv "/usr/local/jdk1.8.0_311" "/usr/local/jdk1.8"
      ignore_errors: true

    - name: 设置Java环境变量
      ansible.builtin.lineinfile:
        path: /etc/profile
        line: |
          export JAVA_HOME=/usr/local/jdk1.8
          export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib/*
          export PATH=$PATH:$JAVA_HOME/bin
      become_user: root
      register: env_result

    - name: 创建用户组
      ansible.builtin.group:
        name: "{{ new_user_name }}"                #部署时所使用的用户
        state: present

    - name: 创建普通用户
      ansible.builtin.user:
        name: "{{ new_user_name }}"
        state: present
        groups: "{{ new_user_name }}"
        password: n4G9603#ssx                     #此处为创建的普通用户密码,可更改
        shell: "/bin/bash"

    - name: 配置普通用户sudo免密
      ansible.builtin.lineinfile:
        dest: "/etc/sudoers"
        line: "{{ new_user_name }} ALL=(ALL) NOPASSWD: ALL"
        validate: 'visudo -cf %s'


    - name: 执行脚本,创建.ssh目录
      ansible.builtin.shell:
        cmd: mkdir "/home/{{ new_user_name }}/.ssh"
      become_user: root

    - name: 生成密钥文件,并调用/etc/profile环境变量刷新
      ansible.builtin.command:  "{{ install_path }}/key.sh"     #由于子进程和父进程问题,此处采用脚本调用
      become_user: root

    - name: 打印环境变量设置结果
      ansible.builtin.debug:
        var: env_result

    - name: 设置.ssh目录权限
      ansible.builtin.file:
        path: "/home/{{ new_user_name }}/.ssh"
        state: directory
        mode: '0700'
        owner: "{{ new_user_name }}"   # 目标主机用户
        group: "{{ new_user_name }}"   # 目标主机用户组

    - name: 免密登录配置
      ansible.builtin.copy:
        src: /root/directory/id_rsa.pub  # 本地SSH公钥路径
        dest: "/home/{{ new_user_name }}/.ssh/authorized_keys"
        owner: "{{ new_user_name }}"   # 目标主机用户
        group: "{{ new_user_name }}"   # 目标主机用户组
        mode: '0600'

    - name: 创建fisco工作目录
      ansible.builtin.file:
        path: "{{ data_path }}"
        state: directory
        owner: "{{ new_user_name }}" 
        group: "{{ new_user_name }}" 
        mode: '0777'

    - name: 创建.fisco目录
      ansible.builtin.shell:
        cmd: " mkdir /home/{{ new_user_name }}/.fisco && sudo cp {{ install_path }}/packages/tassl /home/{{ new_user_name }}/.fisco/  && sudo chown {{ new_user_name }}:{{ new_user_name }} -R /home/{{ new_user_name }} && chmod u+x /home/{{ new_user_name }}/.fisco/tassl "
      become_user: "{{ new_user_name }}"


    - name: 复制文件至工作目录
      ansible.builtin.copy:
        src: "{{ item.src }}"
        dest:  "{{ item.dest }}"
        owner: "{{ new_user_name }}"
        group: "{{ new_user_name }}"
      with_items:
#       - {src: "{{ install_path }}/packages", dest: "/{{ data_path }}/" }
        - {src: "{{ install_path }}/WeBASE-Node-Manager", dest: "/{{ data_path }}/" }
#        - {src: "{{ install_path }}/packages", dest: "/home/{{ new_user_name }}/" }
        - {src: "{{ install_path }}/WeBASE-Sign", dest: "/{{ data_path }}/" }


    - name: 复制Docker tar 包到目标主机
#      ansible.builtin.copy:
      ansible.builtin.unarchive:
        src: "{{ install_path }}/install_docker/docker-20.10.18.tgz"
        dest: /usr/bin/
        mode: '0755'

    - name: 复制docker.server文件和docker-compose,复制packages包至home目录
      ansible.builtin.copy:
        src: "{{ item.src }}"
        dest:  "{{ item.dest }}"
        mode: '0755'
      with_items:
        - {src: "{{ install_path }}/install_docker/docker-compose", dest: "/usr/local/bin/" }
        - {src: "{{ install_path }}/install_docker/docker.service", dest: "/etc/systemd/system/" }
        - {src: "{{ install_path }}/packages", dest: "/home/{{ new_user_name }}/" }

    - name: 启动Docker服务
      ansible.builtin.service:
        name: docker
        state: started

    - name: 导入数据库镜像
      ansible.builtin.shell:
        cmd: docker load -i  {{ install_path }}/install_mysql/mysql-5744.tar.gz
      become_user: root

    - name: 启动MySQL容器
      ansible.builtin.docker_container:
        name: mysql_manager
        image: mysql:5.7.44
        env:
          MYSQL_ROOT_PASSWORD: "{{ mysql_passwd }}"
        ports:
          - "{{ local_port }}:3306"
        state: started

    - name: 等待MySQL容器启动
      ansible.builtin.wait_for:
        host: 127.0.0.1
        port: "{{ local_port }}"
        delay: 10
        timeout: 60

    - name: 创建数据库1
      ansible.builtin.mysql_db:
        name: webasenodemanager
        state: present
        login_user: root
        login_password: "{{ mysql_passwd }}"
        login_port: "{{ local_port }}"
        collation: utf8mb4_general_ci
        encoding: utf8mb4

    - name: 创建数据库2
      ansible.builtin.mysql_db:
        name: webasesign
        state: present
        login_user: root
        login_password: "{{ mysql_passwd }}"
        login_port: "{{ local_port }}"
        collation: utf8mb4_general_ci
        encoding: utf8mb4


    - name: 执行底链sql文件
      ansible.builtin.command:  "{{ install_path }}/sql.sh"


    - name: 修改sign组件的yml配置文件
      ansible.builtin.replace:
        path: "{{ data_path }}/WeBASE-Sign/conf/application.yml"
        regexp: "{{ item.regexp }}"
        replace: "{{ item.replace }}"
      loop:
        - { regexp: 'url: jdbc:mysql://127.0.0.1:3306/webasesign\?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&useSSL=false', replace: 'url: jdbc:mysql://{{ local_ip }}:{{ local_port }}/webasesign?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&useSSL=false' }
        - { regexp: 'username: "用户"', replace: 'username: "root"' }
        - { regexp: 'password: "密码"', replace: 'password: "{{ mysql_passwd }}"' }


    - name: Replace Database Configuration
      ansible.builtin.replace:
        path: "{{ data_path }}/WeBASE-Node-Manager/conf/application-dev.yml"
        regexp: "{{ item.regexp }}"
        replace: "{{ item.replace }}"
      loop:
        - { regexp: 'url: jdbc:mysql://127.0.0.1:3306/webasenodemanager\?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false', replace: 'url: jdbc:mysql://{{ local_ip }}:{{ local_port }}/webasenodemanager?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false' }
        - { regexp: 'username: "用户"', replace: 'username: "root"' }
        - { regexp: 'password: "密码"', replace: 'password: "{{ mysql_passwd }}"' }
        - { regexp: ' webaseSignAddress: "127.0.0.1:5004"', replace: ' webaseSignAddress: "{{ local_ip }}:5004"' }

    - name: 添加执行权限到 WeBASE-Sign/start.sh
      ansible.builtin.command:
        cmd: "chmod u+x {{ data_path }}/WeBASE-Sign/start.sh"
      become_user: "{{ new_user_name }}"

    - name: 添加执行权限到 WeBASE-Node-Manager/start.sh
      ansible.builtin.command:
        cmd: "chmod u+x {{ data_path }}/WeBASE-Node-Manager/start.sh"
      become_user: "{{ new_user_name }}"

    - name: 执行Sign启动脚本
      ansible.builtin.command: "{{ data_path }}/WeBASE-Sign/start.sh"
      become_user: "{{ new_user_name }}"
      args:
        chdir: "{{ data_path }}/WeBASE-Sign"
      environment:
        JAVA_HOME: "/usr/local/jdk1.8"


    - name: 执行Manager启动脚本
      ansible.builtin.command: "{{ data_path }}/WeBASE-Node-Manager/start.sh"
      become_user: "{{ new_user_name }}"
      args:
        chdir: "{{ data_path }}/WeBASE-Node-Manager"
      environment:
        JAVA_HOME: "/usr/local/jdk1.8"

    - name: 等待 10 秒
      ansible.builtin.pause:
        seconds: 10

    - name: 检查 WeBASE-Sign 是否在运行
      ansible.builtin.command: "ps aux | grep 'WeBASE-Sign' | grep -v grep"
      register: sign_process
      ignore_errors: true

    - name: 检查 WeBASE-Node-Manager 是否在运行
      ansible.builtin.command: "ps aux | grep 'WeBASE-Node-Manager' | grep -v grep"
      register: manager_process
      ignore_errors: true

    - name: 打印 WeBASE-Sign 进程状态
      ansible.builtin.debug:
        var: sign_process.stdout_lines

    - name: 打印 WeBASE-Node-Manager 进程状态
      ansible.builtin.debug:
        var: manager_process.stdout_lines
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

人生不过大梦一场

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值