如何使用Ansible将本地git代码库部署到远程服务器?

6

我正在使用Ansible 2.5版本。我需要从本地(控制节点)git仓库将代码部署到远程服务器。

我尝试使用带有git模块的Ansible-playbook,该模块只能将代码部署从本地仓库到另一个本地路径或从远程仓库到另一个远程路径。它基于主机配置。

- git:
    repo: /home/pi/Desktop/kk/Vue-Example/
    dest: /home/pi/Desktop/bb

在这里,repo将是本地(控制器机器)git库的路径,而dest将是远程机器的位置。

1
你遇到了什么问题?当你尝试上述操作时,得到了什么结果? - chenchuk
当添加 hosts: localhost 时,它可以工作,因为我已经存在 git 存储库和目标。它可以部署本地->本地。但是当添加 hosts: remote-host 时,它不起作用。它说远程服务器在该位置没有 git 存储库。我需要部署本地->远程。配置应该是什么? - Anjan Biswas
3个回答

6

这正是我所期望的工作流程——从我知道可以依赖的本地git仓库中提取文件。在我的情况下,我使用特定的提交ID(已经经过充分测试的版本)而不是分支名称。如果您也想这样做,请将下面的“master”替换为提交ID。

- tasks:
    - name: Make temp directory
      tempfile:
        state: directory
      register: temp_git_archive
      delegate_to: localhost
      become: no
      changed_when: False
    - name: Extract latest git commit on branch master
      shell: git archive master |tar --extract --directory={{ temp_git_archive.path }}
      args:
        chdir: /home/pi/Desktop/kk/Vue-Example/  # YOUR LOCAL GIT REPO
      delegate_to: localhost
      become: no
      changed_when: False
    - name: Copy to remote
      copy:
        src: "{{ temp_git_archive.path }}"
        dest: /home/pi/Desktop/bb  # YOUR DESTINATION DIRECTORY
    - name: Delete temp directory
      file:
        path: "{{ temp_git_archive.path }}"
        state: absent
      when: temp_git_archive.path is defined
      delegate_to: localhost
      become: no
      changed_when: False

或许可以使用 Ansible 的 'git' 和 'unarchive' 模块代替上文的 'shell' 模块,但我更喜欢一步到位。


1

这是我的看法:

---
- name: Create repo directory
  file:
    path: "{{project_src_destination}}"
    state: directory
- name: Init repo
  command: git init {{project_src_destination}}
  args:
    creates: "{{project_src_destination}}/.git"
- name: Test repo state
  command: git show {{project_branch}}
  args:
    chdir: "{{project_src_destination}}"
  register: result
  ignore_errors: true
  changed_when: result.rc != 0
- name: Upload the repo
  local_action:
    module: command git push ssh://{{inventory_hostname}}/{{project_src_destination}} {{project_branch}}
    chdir: "{{project_src_source}}"
  when: 'result.rc != 0'
- name: Checkout the newly created branch
  command: git checkout {{project_branch}}
  args:
    chdir: "{{project_src_destination}}"
  register: result
  changed_when: "'Switched' in result.stdout"

与之前的答案不同,无需创建中间存档。如果需要进行增量更新,则可以具有优势。

完全不同的方法是只使用rsync:

- name: Copy sources
  ansible.posix.synchronize:
    src: "{{project_src_host}}"
    dest: "{{project_src_destination}}"
    delete: true
    rsync_opts:
      - "--exclude=.git"
      - "--exclude=*.pb.go"
      - "--exclude=*.o"
      - "--exclude=*.d"
      - "--filter=:- .gitignore"

这里的想法是主要依赖于 .gitignore 来避免复制中间编译结果。


1
您错误地解释了ansible的git模块的用法。它用于在目标路径中克隆远程存储库,即在控制器机器或远程主机上。您指定了不存在于git模块中的本地路径,因为git会尝试发送http / ssh请求,而这样的路径不存在。
来自ansible的repo值的引用是:
repo:git,SSH或HTTP(S)协议地址的git存储库。
如果您想在控制器机器上克隆,原因是ssh密钥,那么可以使用git模块委派到localhost,然后使用复制模块从控制器复制到远程机器。
---
- name: play to checkout
  hosts: remote-hosts
  tasks:
    - name: git checkout
      repo: "{{ repo_url }}"
      dest: /tmp
      delegate_to: localhost
    - name: copy module
      synchronize:
        src: ...
        dest: ...

repo: local_or_remote_git_repository 对我来说是有效的。但它仅限于本地到本地或远程-> 远程,基于 hosts: 配置。无论如何,您能否告诉我部署本地-> 远程仅 git 更改文件的好解决方案是什么。 - Anjan Biswas
我之前所说的是,由于仓库存在且Git将其视为本地仓库并已存在于本地机器上,因此它可以在本地到本地运行。您想要在控制器机器上检出并复制到远程机器,对吗?那么以上方法就可以实现。 - error404
好的,但是它是重新部署整个应用程序还是只有Git更改? - Anjan Biswas
1
在这种情况下,您可以使用Ansible的同步模块。因此,您希望将修改后的文件从本地控制器机器复制到远程机器-只会复制已修改的内容,但请注意,每次运行仓库以满足本地需求都需要从远程进行拉取。 - error404

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接