简体   繁体   English

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

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

I am using Ansible 2.5.我正在使用 Ansible 2.5。 I need to do deploy code from local(controller) git repository to remote server.我需要将代码从本地(控制器)git 存储库部署到远程服务器。

I was trying with a Ansible-playbook with git module that can only deploy code from local repository to another local path or remote repository to another remote path.我正在尝试使用带有 git 模块的 Ansible-playbook,它只能将代码从本地存储库部署到另一个本地路径或将远程存储库部署到另一个远程路径。 Its based on hosts configuration.它基于主机配置。

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

Here repo will be local(controller-machine) git repository path and dest will be remote machine location.这里repo将是本地(控制器-机器)git 存储库路径,而dest将是远程机器位置。

This is exactly the workflow I wanted as well - to extract files from a local git repo I know I can depend on.这也正是我想要的工作流程 - 从我知道我可以依赖的本地 git 存储库中提取文件。 In my case, I use a specific commit ID (a version that has been well-tested) rather than a branch name.就我而言,我使用特定的提交 ID(经过充分测试的版本)而不是分支名称。 If you want this, just replace 'master' below with the commit 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

It may be possible to use the Ansible 'git' and 'unarchive' modules in place of the 'shell' module above, but I prefer doing it in one step.可以使用 Ansible 'git' 和 'unarchive' 模块代替上面的 'shell' 模块,但我更喜欢一步完成。

You have wrongly interpreted the use of git module of ansible.您错误地解释了 ansible 的 git 模块的使用。 It is used to clone the remote repo at the dest path ie either in the controller machine or in the remote hosts.它用于在 dest 路径(即在控制器机器或远程主机中)克隆远程仓库。 You have specified local path which doesn't exists for git module as git would try to send a http/ssh request and such path doesn't exists.您已经指定了 git 模块不存在的本地路径,因为 git 会尝试发送 http/ssh 请求,而这样的路径不存在。

The quote of the repo value from ansible is ansible 的 repo 值的报价是

repo: git, SSH, or HTTP(S) protocol address of the git repository. repo:git 存储库的 git、SSH 或 HTTP(S) 协议地址。

In case you are looking to clone on the controller machine reason being ssh keys then you can use the git module delegate to localhost then use the copy module to copy from controller to remote machine如果您希望在控制器机器上克隆原因是 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.

相关问题 如何使用git将本地存储库复制到远程服务器? - How to copy a local repository to remote server using git? 如何从本地 windows 存储库推送到 windows git 远程服务器 - How to push to windows git remote server from a local windows repository 如何使用Git将更改从本地计算机提交到服务器上的远程存储库 - How to Commits changes from the local computer to a remote repository on the server using Git 如何删除从Git中的源代码派生而来的二进制文件,但仍部署到远程存储库? - How can I remove binary files derived from source code in Git and yet still deploy to a remote repository? 如何从本地存储库中恢复意外删除的远程git存储库 - how to recover an accidently deleted remote git repository from local repository 如何从远程服务器中提取git存储库? - How to pull a git repository from remote server? Git入门-从远程TFS服务器进入本地存储库? - Getting started with Git - pull into local repository from a remote TFS server? 将站点从本地Git存储库发布到远程服务器 - Publish a site to a remote server from a local Git repository 在 Ansible Tower 中使用本地 git 存储库 - Using local git repository with Ansible Tower 如何移动本地Git存储库(带有历史记录)以由Gitosis在远程服务器上进行管理? - How to move local Git repository (with history) to be managed by Gitosis on remote server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM