使用Ansible 2.4进行git clone --mirror

4
如何在Ansible 2.4中通过git模块实现此操作? 我已经查看了文档http://docs.ansible.com/ansible/latest/git_module.html,但没有mirror克隆选项。 有没有其他方法可以实现它而不必直接运行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

我喜欢这些好用的配置开关,可以接受主机密钥等等。我认为,替代方案可能是这样的...(还没有测试过)

- 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

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

1个回答

3

目前,这是我用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模块改变了文件权限。 :( 现在感觉更像是黑客了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接