如何在执行ansible部署时获取git的提交id

如何在执行ansible部署时获取git的提交id,git,deployment,ansible,Git,Deployment,Ansible,我正在使用ansible部署项目,而部署时我会使用git的最后一个提交Id,以便在出现任何问题时,我应该能够恢复到原来的状态。 获取提交id的过程非常繁琐,需要登录到远程服务器并获取提交id 是否有任何方法可以从远程服务器获取最后一个提交Id,并将其登录到本地服务器。在ansible将最新代码部署到remote之前 你可以 - name: get git version shell: git rev-parse HEAD register: git_version - name: st

我正在使用ansible部署项目,而部署时我会使用git的最后一个提交Id,以便在出现任何问题时,我应该能够恢复到原来的状态。
获取提交id的过程非常繁琐,需要登录到远程服务器并获取提交id

是否有任何方法可以从远程服务器获取最后一个提交Id,并将其登录到本地服务器。在ansible将最新代码部署到remote之前

你可以

- name: get git version
  shell: git rev-parse HEAD
  register: git_version

- name: store it
  shell: echo "last_git_version: {{ git_version.stdout }}" > host_vars/{{ ansible_host }}/git_info.yml
  delegate_to: localhost
然后,当您部署代码时,您将拥有一个变量,其中包含您部署的代码的最新版本,您可以恢复到该版本。

- name: get git version
  shell: git rev-parse HEAD
  register: git_version

- name: store it
  shell: echo "last_git_version: {{ git_version.stdout }}" > host_vars/{{ ansible_host }}/git_info.yml
  delegate_to: localhost

然后,当您部署代码时,您将拥有一个变量,其中包含您部署的代码的最新版本,您可以恢复到该版本。

我想更好的选择是:

  • 注册的输出
  • 并使用注册变量的
    .before
一个例子如下所示:

- name: Clone git project
  git:
    repo: https://github.com/nextcloud/docker
    dest: /tmp/
  register: result_git_clone
- name: Print last commit ID
  debug:
    msg:
      - "{{ result_git_clone.before }}"
预期输出应类似于:

TASK [nextcloud : Print last commit ID] ***********************************************************************
ok: [puigreig] => {}

MSG:

['476e60305b5f51aba863f0552cbaf7c2d4e0dbf9']

我想更好的选择是:

  • 注册的输出
  • 并使用注册变量的
    .before
一个例子如下所示:

- name: Clone git project
  git:
    repo: https://github.com/nextcloud/docker
    dest: /tmp/
  register: result_git_clone
- name: Print last commit ID
  debug:
    msg:
      - "{{ result_git_clone.before }}"
预期输出应类似于:

TASK [nextcloud : Print last commit ID] ***********************************************************************
ok: [puigreig] => {}

MSG:

['476e60305b5f51aba863f0552cbaf7c2d4e0dbf9']

尝试此“stderr”时出错:“/bin/sh:1:git:not found”系统中未安装可执行文件或路径未正确设置。第一个问题可以通过解决,第二个问题可以通过尝试此“stderr”时出错来解决:“/bin/sh:1:git:not found”系统中未安装git可执行文件或未正确设置路径
env var。第一个可以用,第二个可以用