簡體   English   中英

如何使用 Ansible 將本地 git 存儲庫中的代碼部署到遠程服務器

[英]How to deploy code from local git repository to remote server using Ansible

我正在使用 Ansible 2.5。 我需要將代碼從本地(控制器)git 存儲庫部署到遠程服務器。

我正在嘗試使用帶有 git 模塊的 Ansible-playbook,它只能將代碼從本地存儲庫部署到另一個本地路徑或將遠程存儲庫部署到另一個遠程路徑。 它基於主機配置。

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

這里repo將是本地(控制器-機器)git 存儲庫路徑,而dest將是遠程機器位置。

這也正是我想要的工作流程 - 從我知道我可以依賴的本地 git 存儲庫中提取文件。 就我而言,我使用特定的提交 ID(經過充分測試的版本)而不是分支名稱。 如果你想要這個,只需用提交 ID 替換下面的“master”。

- 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' 模塊,但我更喜歡一步完成。

您錯誤地解釋了 ansible 的 git 模塊的使用。 它用於在 dest 路徑(即在控制器機器或遠程主機中)克隆遠程倉庫。 您已經指定了 git 模塊不存在的本地路徑,因為 git 會嘗試發送 http/ssh 請求,而這樣的路徑不存在。

ansible 的 repo 值的報價是

repo:git 存儲庫的 git、SSH 或 HTTP(S) 協議地址。

如果您希望在控制器機器上克隆原因是 ssh 密鑰,那么您可以使用 git 模塊委托到本地主機,然后使用復制模塊從控制器復制到遠程機器

---
- 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: ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM