I'm trying to write deployments rules with Ansible to clone a repository

Viewed 237

The steps i followed are:

  • Ansible login as root user
  • Update Server pacakges
  • Create a user called deploy
  • Clone a Git Repository from bitbucket.org

I want to clone the repository as deploy user in his home directory using ssh forwarding method.

But the issue is that, I am not able to get permissions even through ssh forwarding and the error returns as :Doesn't have rights to access the repository.

My inventory file:

[production]
rails ansible_host=(my host ip) ansible_user=ubuntu

My ansible.cfg file looks like this:

[ssh_connection]
pipelining=True
ssh_args = -o ForwardAgent=true

My playbook looks like this:

---
- hosts: production
  remote_user: root
  become: yes
  tasks:
    - name: Update all packages to latest version
      apt:
        upgrade: dist
    - add deploy user tasks here
      (deploy user add task)
    - name: APP | Clone repo
        git:
          repo: git@github.com:e911/Nepali-POS-Tagger.git
          dest: home/deploy/myproject
          accept_hostkey: true
          force: true
        become: yes
        become_user: deploy
        tags: app

My deploy user is created but for some reason I cannot clone the user as deploy user. It doesnot have access right. I have researched and think this seems to be because of ssh keys not being attached. When I login in as ubuntu and switch user as deploy the attached keys are not forwarded to deploy. But I cannot have a solution for this. How do you solve this ? Or what am I doing wrong here?

Here is the error snippet:

fatal: [rails]: FAILED! => {
    "changed": false,
    "cmd": "/usr/bin/git clone --origin origin '' /home/deploy/myproject",
    "invocation": {
        "module_args": {
            "accept_hostkey": true,
            "archive": null,
            "bare": false,
            "clone": true,
            "depth": null,
            "dest": "/home/deploy/myproject",
            "executable": null,
            "force": true,
            "gpg_whitelist": [],
            "key_file": null,
            "recursive": true,
            "reference": null,
            "refspec": null,
            "remote": "origin",
            "repo": "git@github.com:e911/Nepali-POS-Tagger.git",
            "separate_git_dir": null,
            "ssh_opts": null,
            "track_submodules": false,
            "umask": null,
            "update": true,
            "verify_commit": false,
            "version": "HEAD"
        }
    },
    "msg": "",
    "rc": 128,
    "stderr": "Cloning into '/home/deploy/myproject'...\ngit@github.com: Permission denied (publickey).\r\nfatal: Could not read from remote repository.\n\nPlease make sure you have the correct access rights\nand the repository exists.\n",
    "stderr_lines": [
        "Cloning into '/home/deploy/myproject'...",
        "git@github.com: Permission denied (publickey).",
        "fatal: Could not read from remote repository.",
        "",
        "Please make sure you have the correct access rights",
        "and the repository exists."
    ],
    "stdout": "",
    "stdout_lines": []
}

I have tried the solutions here: Ansible and Git Permission denied (publickey) at Git Clone but it was of not help.

1 Answers

We have alternative solution, using HTTP instead of SSH:

For GitHub:

  • Generate a Token from link: https://github.com/settings/tokens
  • Give permission with scope: repo (full control of private repositories)
  • Use that token git+https://<TOKEN>:x-oauth-basic@github.com/<ORGANIZATION>/<REPO>.git#<BRANCH>

For BitBucket:

  • Generate a random Password for your repo from link: https://bitbucket.org/account/settings/app-passwords
  • Give permission with scope Repositories: Read
  • Use that password to clone your repo as: git clone https://<USERNAME>:<GENERATED_PASSWORD>@bitbucket.org/<ORGANIZATION>/<REPO>.git

Hope this could be an alternative for the solution.

Related