Deploy application from local git repository

ansibleansible-playbook

This is a two-fold question.

In scenarios where the git repository is not publicly accessible, can I deploy from a local machine (that also runs Ansible) to a remove host using the local copy of the repository on my hard drive (or a copy on the master server, for example)?

If the answer to the previous question is yes, is the git module used for this purpose?

EDIT:

What I've tried thus far:

The dir structure as follows:

repo/
|-.git/
|-deploy/
| |-inventory
| |-roles/
| | \-app/
| |   \-main.yml
| \-vagrant.yml
\-src/

playbook contains:

- name: Clone local application
  git: repo={{ inventory_dir }}/../
       dest=/home/{{ application_name }}/code

Deploying this to a vagrant box via SSH results in:

fatal: [vagrant]: FAILED! => {
  "changed": false, 
  "cmd": "/usr/bin/git clone --origin origin path/to/repo", 
  "failed": true, 
  "msg": "Cloning into '/home/app/code'...\nfatal: 
  'path/to/repo' does not appear to be a git repository\nfatal: 
  Could not read from remote repository.\n\nPlease make sure you 
  have the correct access rights\nand the repository exists.", 
  ...}

Best Answer

Ansible git module uses native git executable to perform its actions, so you need to proceed like you would do with manual operations.

  • Mount the disk containing the Git repository to the target machine.

    This is easily achievable with Vagrant, if you keep the repository under the directory containing Vagrantfile (this might be different to your scenario - not sure what you mean by vagrant.yml).

    Vagrant mounts this directory by default in /vagrant in virtual machine, so to clone a repository you'd use the standard git module:

    - git:
        repo: /vagrant/path/to/source/repository
        dest: /path/to/destination
    

    It will clone the repository to /path/to/destination/repository.

  • Use Ansible synchronize module to push the repository to the destination machine. If the only reason for cloning is to "deploy application" without pushing back to the original repository, it is sufficient.

  • Finally you can share the repository using either of the protocols supported by Git, like SSH, HTTP, HTTPS, FTP, FTPS, rsync; or mount the directory with NFS (this is equivalent of the first method).