Git with Ansible

From Knowledge Center
Revision as of 16:20, 21 March 2019 by Ruan (talk | contribs)
Jump to: navigation, search

Clone a private github repository with ansible:

$ cat git-run.yml
---
- name: git-test
  hosts: laptop
  vars_files:
    - vars.yml
  roles:
    - git-test
$ cat vars.yml
git_repo: git@github.com:user/reponame.git
deploy_private_key: /Users/ruan/workspace/repo/ansible/deploy_keys
cat roles/git-test/tasks/main.yml
- name: ensure directory has correct permissions.
  file:
    path: /tmp/deploy
    state: directory
    owner: ruan
    group: staff

- name: ensure .ssh directory exists.
  file:
    path: /tmp/deploy/.ssh
    state: directory
    mode: 0700
    owner: ruan
    group: staff

- name: places keys in path
  copy:
    src: "{{ deploy_private_key }}"
    dest: /tmp/deploy/.ssh/deploy_keys
    mode: 0600
    owner: ruan
    group: staff

# See: https://stackoverflow.com/a/37096534/100134
#- name: Ensure setfacl support is present.
#  package: name=acl

- name: clone to path.
  git:
    repo: "{{ git_repo }}"
    dest: /tmp/deploy/main-repo
    accept_hostkey: yes
    key_file: /tmp/.ssh/deploy_keys
  become_user: ruan

- name: delete deploy key
  file:
    path: /tmp/deploy/.ssh/deploy_keys
    state: absent

Resources: