git clone - 使用Ansible 2.4的镜像

时间:2018-01-08 03:09:45

标签: git ansible

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

有没有其他方法可以做到这一点,而无需直接运行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 个答案:

答案 0 :(得分: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模块改变了文件权限。 :(现在感觉更hacky