Git module in Ansible gets permission denied on tmp directory

Viewed 2113

I'm trying to clone a remote repository by Ansible using the git module. Here's the task configuration:

- name: Clone repo
  git:
    repo: "{{ repository }}"
    dest: "/home/{{ username }}/abc"
    key_file: "{{ git_key_file }}"
  register: code_update

But unfortunately it fails with below error:

fatal: [xyz]: FAILED! => {"changed": false, "cmd": "/usr/bin/git clone --origin origin '' /home/xyz/abc", "msg": "Cloning into '/home/xyz/abc'...\nfatal: cannot exec '/tmp/tmpm9mfdkci': Permission denied\nfatal: cannot exec '/tmp/tmpm9mfdkci': Permission denied\nfatal: unable to fork", "rc": 128, "stderr": "Cloning into '/home/xyz/abc'...\nfatal: cannot exec '/tmp/tmpm9mfdkci': Permission denied\nfatal: cannot exec '/tmp/tmpm9mfdkci': Permission denied\nfatal: unable to fork\n", "stderr_lines": ["Cloning into '/home/xyz/abc'...", "fatal: cannot exec '/tmp/tmpm9mfdkci': Permission denied", "fatal: cannot exec '/tmp/tmpm9mfdkci': Permission denied", "fatal: unable to fork"], "stdout": "", "stdout_lines": []}

It is worth mentioning that I can clone repository directly on remote server. Also I tried to change the TMP and TMPDIR using environment setting but nothing changed.

Any response would be appreciated...

1 Answers

/tmp at the server is mounted with option noexec so ansible cannot execute its own temporary scripts. The recommended fix is to set environment variable TMPDIR:

 - name: Clone the git repo in a temporary directory
      environment:
        TMPDIR: "/home/{{ username }}/tmp"
      git:
        repo: "{{ repository }}"
        dest: "/home/{{ username }}/abc"
        key_file: "{{ git_key_file }}"

Make sure the directory exists.

See https://github.com/ansible/ansible/issues/30064, esp. https://github.com/ansible/ansible/issues/30064#issuecomment-487149251

Related