[英]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.