git clone --mirror with Ansible 2.4

标签 git ansible

如何通过 ansible 2.4 中的 git 模块执行此操作? 我看过 doco http://docs.ansible.com/ansible/latest/git_module.html 镜像克隆没有选项。

有没有其他方法可以做到这一点而不必直接运行 shell 命令.. 目前我有一些看起来像这样的东西..

- name: Clone git repo
  git:
    repo: ssh://git@github.com/foo/bar.git
    key_file: /home/deploy/.ssh/id_rsa
    dest: /path/to/repo
    accept_hostkey: true
    update: yes
    version: master
    bare: no
  become_user: deploy
  when: repo_created.changed

我喜欢漂亮的配置开关来接受主机 key 等。 我认为的替代方案是这样的……(尚未测试)

- name: Test if github is a known host
  shell: ssh-keygen -l -f /home/deploy/.ssh/known_hosts -F github.com
  register: github_host_is_known
  sudo_user: deploy
  ignore_errors: True
  changed_when: github_host_is_known.rc != 0
- name: Add githubs key to known hosts
  shell: ssh-keyscan -H github.com >> /home/deploy/.ssh/known_hosts
  when: github_host_is_known.rc != 0
  sudo_user: deploy
- name: "Clone repo"
  command: git clone --mirror git@github.com:foo/bar.git /path/to/repo
  sudo_user: deploy
  when: repo_created.changed

这是我唯一/最好的选择吗?

最佳答案

到目前为止,这是我能够使用 Ansible 2.4 克隆镜像仓库的最佳方式

- name: Add githubs key to known hosts
  known_hosts:
    path: /home/deploy/.ssh/known_hosts
    name: github.com
    key: "{{ lookup('pipe', 'ssh-keyscan -t rsa github.com') }}"
    state: present
  sudo_user: deploy

- name: change the owner of the known_hosts file to deploy user
# because https://github.com/ansible/ansible/issues/29331
  file:
    path: /home/deploy/.ssh/known_hosts
    owner: deploy
    group: deploy
    mode: 0644

- name: Clone repo with --mirror
  environment:
    GIT_SSH_COMMAND: ssh -i /home/deploy/.ssh/id_rsa # Needs git 2.3 + for this to work
  command: git clone --mirror git@github.com:foo/bar.git /path/to/repo
  sudo_user: deploy

这感觉还不错。有一个镜像选项仍然很好。

编辑:说得太早了,看起来 known_hosts 模块改变了文件权限。 :( 现在感觉更 hacky

关于git clone --mirror with Ansible 2.4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48143780/

相关文章:

git undo merge and add for 单个文件

ansible - 为什么 Ansible 复制模块不复制这个目录?

linux - Ansible 主机/组结构

ansible - 使用 ansible-playbook 隐藏额外输出

windows - git checkout 时出错。路径太长

c++ - 使用 git 进行 VS2010 项目 : Can't add file *. opensdf

git - 通过cron + ssh-agent进行git push(无密码)

git - 恢复 merge 恢复

variables - 角色默认变量中的 Ansible 递归循环

ansible:在角色中包含角色?