git 在ansible中部署时如何拉
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33625581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pull while deployment in ansible
提问by Ajeet Khan
I am using Ansiblefor configuration management and the following task to clone a Git repo:
我正在使用Ansible进行配置管理和以下任务来克隆 Git 存储库:
# Example git checkout from Ansible Playbooks
- git: repo=git://foosball.example.org/path/to/repo.git
dest=/srv/checkout
version=release-0.22
This clones the repo with the particular version.
这会克隆具有特定版本的 repo。
Does it do a git pull
when run again if the repo already exists? Or does it simply clone the repo all the time? How to do a git pull
in Ansible if the repo already exists and how can we run a specific command if the repo exists and same if the repo is cloned for the first time?
git pull
如果 repo 已经存在,它是否会再次运行?或者它只是一直克隆回购?git pull
如果 repo 已经存在,如何在 Ansible 中执行 a ,如果 repo 存在,我们如何运行特定命令,如果 repo 是第一次克隆,我们如何运行?
回答by ydaetskcoR
Ansible is a declarative tool in which you describe how you want your server/environment to look and Ansible attempts to make that happen. It is also designed to be idempotentwhich means that re-running your plays should reproduce the same end result each time as long as nothing underneath has changed.
Ansible 是一个声明性工具,您可以在其中描述您希望服务器/环境的外观以及 Ansible 试图实现这一点。它也被设计为幂等的,这意味着只要底层没有任何改变,重新运行你的剧本应该每次都重现相同的最终结果。
The git modulealso ascribes to this and simply tries to make sure that the remote host has the repo on it and to the version (or branch/tag) that you optionally asked for.
该git的模块也归咎于此,只是试图确保远程主机上有和版本(或分支/标签)的回购,你可选择要求。
So when you run the git task in your question on a fresh environment it will clone the repo to the destination folder. On future runs, the repo is already there so it simply does a git pull.
因此,当您在新环境中运行问题中的 git 任务时,它会将存储库克隆到目标文件夹。在以后的运行中,repo 已经在那里,所以它只是做一个 git pull。
If you specify a tag/branch/commit ref to the update
property then it will simply check that version out and pull that.
如果您为该update
属性指定了一个标签/分支/提交引用,那么它只会检查该版本并将其拉出。
回答by Alex Jaimes
This works for me using a shell command:
这适用于我使用 shell 命令:
- hosts: myserver
become: yes
tasks:
- name: pull changes
shell: chdir=/path/whereis/myrepo git pull https://mygituser:[email protected]/company/myrepo.git master
i had to specify the remote url using user and password because it hangs with simple "git pull".
我必须使用用户名和密码指定远程 url,因为它挂起简单的“git pull”。