Ansible is one of the best tool when it comes to deployment and any related things as well. I’m using ansible to deploy my website, and a few of my projects.

Using git modules on Ansible is quite easy, for examples:

- name: pull the latest code from git
  git:
    repo: https://github.com/username/repo.git
    dest: /path/to/remote/dir

With a simple configuration, the git module will clone or update the from repo to the destination directory. Thats if your repository is Public.

But, if you’re trying to use the very same method for Private Repository, you’ll got an error that you have no rights to access the repository.

Of course, we should add deployment key or ssh keys into our git account. If you are new to this, you can read Adding a new SSH key to your GitHub account

Let say, we use our ssh key from ~/.ssh/deployment_key. Ansible has already provide us with the configuration parameters, so lets modify our ansible task.

- name: pull the latest code from git
  git:
    repo: https://github.com/username/repo.git
    dest: /path/to/remote/dir
    key_file: ~/.ssh/deployment_key

The key_file parameters tell ansible to use that key when pulling the repository