【问题标题】:Ansible and liquibase integrationAnsible 和 liquibase 集成
【发布时间】:2015-09-24 16:17:05
【问题描述】:

我有一个用例,我必须使用ansible 运行liquibase 迁移,但不知道如何解决远程连接。一种想法是创建 ssh 隧道,但我不确定。

用例: (在远程服务器上)

  • 创建N个数据库
  • 为每个数据库创建数据库用户
  • 为每个数据库运行 liquibase 迁移

远程服务器只能通过 ssh 访问,而 liquibase 不支持 ssh 隧道(据我所知),也没有适用于 liquibase 的 ansible 模块。

在运行上述任务之前,似乎是一种 hackish 方式:

  • 为 mysql 创建正向/反向隧道,
  • 创建N个数据库
  • 为每个数据库创建数据库用户
  • 为每个数据库运行 liquibase 迁移
  • 杀死隧道。

但我不确定这是解决此问题的最佳方法

【问题讨论】:

  • 我对ansible一无所知。但是从 liquibase(在 java vm 中运行)到数据库的连接是 JDBC。因此,如果您通过 ssh 隧道建立 jdbc 连接,您应该能够使用具有 ssh 访问权限的 liquibase。
  • @Jens,这就是留在桌面上的想法,谢谢。我正在考虑避免隧道问题
  • 我遇到了类似的问题。您可以通过在主机上安装 liquibase 并使用 Ansible commandshell 模块运行 liquibase 命令来避免使用隧道。然而对我来说,这变得很复杂,我最终使用 liquibase 创建 SQL 语句并使用 Ansible mysql_db 模块部署这些语句。
  • @knowhy 检查我的答案
  • 你所说的“转机远程连接”是什么意思?

标签: mysql ansible liquibase ssh-tunnel


【解决方案1】:

使用 java 在服务器端安装 liquibase 似乎有额外的开销,因此,ssh 隧道似乎是一个可行的解决方案,我解决了这个问题,如下所示:

变量:

mysql:
    ssh_user: "xyz@{{ inventory_hostname }}"
    forward_port: 3306
    listen_port: 3307
    ssh_key: "{{ ansible_ssh_private_key_file }} "
    definer: "'root'@'localhost'"
    backup_dir: "/var/somebackup/location"

任务:

 - name: Kill any active ssh tunnel to current host
    local_action: command bash -c "ps ax | grep ssh |grep -v grep | grep  '{{ mysql.ssh_user }}' |head -n 1| xargs -n 1|head -n 1|xargs kill -KILL"
    sudo: no
    register: ssh_tunnel_end

  - name: Create ssh tunnel to mysql
    local_action:  command bash -c "ssh -o ExitOnForwardFailure=yes  -o ConnectTimeout=2 -i {{ mysql.ssh_key }} -f -N -L  {{ mysql.listen_port }}:127.0.0.1:{{ mysql.forward_port }} {{ mysql.ssh_user }}" 
    sudo: no      
    register: ssh_tunnel_start

  - name: Run liquibase migrations for each db
    shell: liquibase --contexts='{{item.1.contexts}}' --username='{{customer.db.root_user}}' --password='{{customer.db.root_password}}' --changeLogFile='update.xml' --url='jdbc:mysql://127.0.0.1:{{mysql.listen_port}}/{{ customer.prefix }}_{{ item.1.db_suffix }}'   
    args:
        chdir: "{{special_modules.migrations.dest}}/{{item.1.name}}"
    delegate_to: 127.0.0.1
    sudo: no
    with_subelements:
        - "{{ customer_modules }}"
        - migrations

  - name: Kill reverse ssh tunnel to mysql
    local_action: command bash -c "ps ax | grep ssh |grep -v grep | grep  '{{ mysql.ssh_user }}' |head -n 1|xargs -n 1|head -n 1|xargs kill -KILL"
    sudo: no
    register: ssh_tunnel_end

【讨论】:

    【解决方案2】:

    使用Ansible 2.0 在远程主机上安装Liquibase 并不是很困难:

    ---
    - name: Create Liquibase directory
      file: name=/opt/liquibase state=directory mode=0755 owner=root group=root
    
    - name: Install Liquibase
      unarchive:
        src: https://github.com/liquibase/liquibase/releases/download/liquibase-parent-{{ liquibase_version }}/liquibase-{{ liquibase_version }}-bin.tar.gz
        dest: /opt/liquibase
        copy: no
        owner: root
        group: root
        mode: 0755
        creates: /opt/liquibase/liquibase
    

    【讨论】:

    • 可能你把我的问题搞错了。我没有在任何地方安装 liquibase。我正在使用本地迁移和 liquibase 运行到远程服务器的迁移,因此我必须避免在每台服务器上安装 java + liquibase。请在下面查看我的答案
    • 为什么安装 JVM 和 Liquibase 的开销如此之大?这就是使用 Liquibase 的典型方式。
    • 我在 php 项目中使用 Liquibase
    猜你喜欢
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 2017-11-11
    • 2018-12-28
    相关资源
    最近更新 更多