Git with Ansible

From Knowledge Center
Revision as of 16:19, 21 March 2019 by Ruan (talk | contribs) (Created page with "<pre> cat git-run.yml --- - name: git-test hosts: laptop vars_files: - vars.yml roles: - git-test </pre> <pre> $ cat vars.yml git_repo: git@github.com:user/repo...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
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:

- https://docs.ansible.com/ansible/latest/modules/file_module.html - https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html - https://www.jeffgeerling.com/blog/2018/cloning-private-github-repositories-ansible-on-remote-server-through-ssh