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