【问题标题】:git clone --mirror with Ansible 2.4git clone --mirror 与 Ansible 2.4
【发布时间】:2018-01-08 03:09:45
【问题描述】:

你如何通过 ansible 2.4 中的 git 模块做到这一点? 我查看了 doco 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

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

【问题讨论】:

    标签: git ansible


    【解决方案1】:

    到目前为止,这是我能够使用 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了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-03
      • 2018-03-21
      • 1970-01-01
      • 2020-04-29
      • 2017-05-30
      • 1970-01-01
      • 2015-10-17
      • 1970-01-01
      相关资源
      最近更新 更多