Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Git checkout: UnboundLocalError: local variable 'remote_head' referenced before assignment #5504

Closed
hsanjuan opened this issue Nov 6, 2016 · 39 comments

Comments

@hsanjuan
Copy link

hsanjuan commented Nov 6, 2016

ISSUE TYPE
  • Bug Report
COMPONENT NAME

git

ANSIBLE VERSION
ansible 2.2.0.0
  config file = 
  configured module search path = Default w/o overrides
CONFIGURATION

pipeline = True + related ControlPath etc

OS / ENVIRONMENT

Linux

SUMMARY

Git checkout fails randomly. Seems like a race condition.

STEPS TO REPRODUCE

Have a step:

- name: checkout
  become: yes
  git:
    repo: https://github.com/hsanjuan/gallomatic.git
    dest: /opt/gallomatic
    force: yes
    version: HEAD
EXPECTED RESULTS

OK

ACTUAL RESULTS

Randomly get (about 50% of the time):

An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_T5rJsX/ansible_module_git.py", line 1040, in <module>
    main()
  File "/tmp/ansible_T5rJsX/ansible_module_git.py", line 994, in main
    result.update(changed=True, after=remote_head, msg='Local modifications exist')
UnboundLocalError: local variable 'remote_head' referenced before assignment

fatal: [myhost]: FAILED! => {
    "changed": false, 
    "failed": true, 
    "invocation": {
        "module_name": "git"
    }, 
    "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_T5rJsX/ansible_module_git.py\", line 1040, in <module>\n    main()\n  File \"/tmp/ansible_T5rJsX/ansible_module_git.py\", line 994, in main\n    result.update(changed=True, after=remote_head, msg='Local modifications exist')\nUnboundLocalError: local variable 'remote_head' referenced before assignment\n", 
    "module_stdout": "", 
    "msg": "MODULE FAILURE"
}
@ansibot
Copy link

ansibot commented Nov 6, 2016

@ansible ping, this issue is waiting for your response.
click here for bot help

@pankajrlal
Copy link

I am also facing this

@fubarhouse
Copy link

+1, everything has suddenly stopped working...
Seems to be when forced is true/yes.

@zero-code-hero
Copy link

zero-code-hero commented Nov 7, 2016

Also having this issue.

Code:

    - name: Clone/Update Repo
      git: repo=git@[removed].git dest="{{ approot }}" force=yes accept_hostkey=true

Error:

fatal: [<server>]: FAILED! => {"changed": false, "failed": true, "module_stderr": "Shared connection to [<server> closed.\r\n", "module_stdout": "Traceback (most recent call last):\r\n File \"/tmp/ansible_4cXbqC/ansible_module_git.py\", line 1040, in \r\n main()\r\n File \"/tmp/ansible_4cXbqC/ansible_module_git.py\", line 994, in main\r\n result.update(changed=True, after=remote_head, msg='Local modifications exist')\r\nUnboundLocalError: local variable 'remote_head' referenced before assignment\r\n", "msg": "MODULE FAILURE"}

This has a 100% failure rate.

@noirmist
Copy link

noirmist commented Nov 7, 2016

Also having this issue

@fubarhouse
Copy link

fubarhouse commented Nov 8, 2016

This issue also affects 2.2.0 and 2.3.0, but does not affect 2.1.2.0.

@rtrvrtg
Copy link

rtrvrtg commented Nov 8, 2016

Can reproduce this 100% of the time when force=yes is set and the destination directory already exists.

@dbourcet
Copy link

dbourcet commented Nov 8, 2016

Also having this issue.

@solomongifford
Copy link

solomongifford commented Nov 8, 2016

For those that need a quick fix, you can find the git.py in the ansible source and remove the "after=remote_head".

An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_T5rJsX/ansible_module_git.py", line 1040, in <module>
    main()
  File "/tmp/ansible_T5rJsX/ansible_module_git.py", line 994, in main
    result.update(changed=True, after=remote_head, msg='Local modifications exist')
UnboundLocalError: local variable 'remote_head' referenced before assignment

@fubarhouse
Copy link

@solomongifford Thanks for the quick fix! Here's hoping for a patch in-release soon!

@svenfinke
Copy link

@solomongifford Thanks for the quick-fix, but I honestly can't find the file... Where should it normally be located?
A patch for this bug would be quite nice, I also hope it will be released soon.

@wasimkham
Copy link

wasimkham commented Nov 10, 2016

@svenfinke The file (for me) is located here I think - /Library/Python/2.7/site-packages/ansible/modules/core/source_control/git.py

@solomongifford There are many instances in the file of "after=remote_head" - you suggest to remove them all?

@BenEinwechter
Copy link

BenEinwechter commented Nov 10, 2016

@svenfinke and @wasimkham If you run ansible-playbook with three verbose flags, Ansible 2.2 will print out the path of the Ansible module being invoked:

$ ansible-playbook playbook.yml -vvv
=>

TASK [boss_app : checkout git repo] ********************************************
task path: /vagrant/roles/boss_app/tasks/main.yml:28
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/core/source_control/git.py

I edited line 994 in /usr/lib/python2.7/dist-packages/ansible/modules/core/source_control/git.py

# from
result.update(changed=True, after=remote_head, msg='Local modifications exist')

# to
result.update(changed=True, msg='Local modifications exist')

This change gets me around the problem. Not sure if it's correct, but it seems pretty benign, and I think it's what @solomongifford is referring to.

sf-project-io pushed a commit to redhat-cip/software-factory that referenced this issue Nov 10, 2016
This change fixes the following issues encounter with the last version
of ansible:

* Unable to find '...' in expected paths. Relative role path are not
  supported anymore, use absolute path instead.
* ansible/ansible-modules-core#5504 broke
  git module when force is used. Apply dirty patch in the meantime.

Change-Id: Iac77c2b7d423a63fec75ea569b046ade06d2d2fb
@solomongifford
Copy link

@wasimkham: @BenEinwechter is correct - its just the one in the snippet that I pasted in the comment above. Thats the only one that needs to be removed.

@svenfinke: @wasimkham is correct - /path/to/your/python/site-packages/ansible/modules/core/source_control/git.py

@sunshine69
Copy link

I got affected by this as well with force=yes

@soichih
Copy link

soichih commented Nov 14, 2016

Is there a workaround for this? Maybe like - downgrading the ansible git module?

@BenEinwechter
Copy link

@soichih See my comment above for a temporary workaround: #5504 (comment)

@acidjunk
Copy link

+1 for a fix

@hsanjuan
Copy link
Author

FYI, there is a PR for this: #5505

@hvisage
Copy link

hvisage commented Nov 17, 2016

+1

In our case: first run (two current django deploys) it fails, then the 2nd run works fine.

@arlaneenalra
Copy link

Just got bit by this. Looks like local modifications to the git repo on the server are the triggering factor for us.

@senderista
Copy link

+1 just hit this after upgrading to 2.2

@arohr
Copy link

arohr commented Nov 19, 2016

+1
same as @hvisage first run fails, second works fine

@ansibot
Copy link

ansibot commented Nov 22, 2016

@ansible, ping. This issue is still waiting on your response.
click here for bot help

ScorpionResponse added a commit to ScorpionResponse/ansible-django that referenced this issue Nov 22, 2016
@bmalynovytch
Copy link

Fix provided by #5505 works 👍

@joeczucha
Copy link

Thanks for the workaround @BenEinwechter, that will keep me moving whilst we wait for an official fix.

FWIW, installed with homebrew on OSX, I had to modify:

/usr/local/Cellar/ansible/2.2.0.0_1/libexec/lib/python2.7/site-packages/ansible/modules/core/source_control/git.py

@fubarhouse
Copy link

For me this is a pretty critical fix, me and all of my team literally cannot update and have to always make sure we're using 2.1.x.x until the fix is in release.

Any chance we could get an ETA on this?

jpeak5 pushed a commit to lsulibraries/islandora_ansible_base_box that referenced this issue Nov 29, 2016
@feanorknd
Copy link

such a critical bug like this, affecting git replication platforms, may be fixed as soon as possible.
Also affected...

@sldenazis
Copy link

Same issue here, applied the @BenEinwechter 's workaround for now.

@sysarcher
Copy link

I'm also facing this issue. Worked with ansible 2.1 but failed on 2.2

@andreasschroth
Copy link

We're also affected and thus our whole development team has problems when provisioning their VMs. Please provide a fix as soon as possible, thank you! :)

@ghost
Copy link

ghost commented Dec 5, 2016

Also seeing this, #5504 (comment) seemed to fix it.

@oucil
Copy link

oucil commented Dec 7, 2016

+1 on Ansible 2.3

@ansibot
Copy link

ansibot commented Dec 7, 2016

This repository has been locked. All new issues and pull requests should be filed in https://github.com/ansible/ansible

Please read through the repomerge page in the dev guide. The guide contains links to tools which automatically move your issue or pull request to the ansible/ansible repo.

lchan752 added a commit to lchan752/python3-pip-ansible that referenced this issue Dec 8, 2016
@iJebus
Copy link

iJebus commented Dec 9, 2016

An alternative workaround that I have found is simply to retry the task, and it succeeds on the second pass.

e.g.

- name: force pull git on repo with changes
  git:
    repo: git@bitbucket.org:your-user/your-repo.git
    dest: "{{ commit_dir }}"
    version: "{{ commit }}"
    force: true
  register: git_deploy
  until: git_deploy|succeeded

@likid0
Copy link

likid0 commented Dec 13, 2016

Also seeing this, #5504 (comment) seemed to fix it.

@ghost
Copy link

ghost commented Dec 15, 2016

There are many features I can't use because of this ugly bug.. I hope this will be fixed in the next version.

@abadger
Copy link
Contributor

abadger commented Dec 15, 2016

This should be fixed by: ansible/ansible#19057

If someone would care to test this against a checkout of either devel or the stable-2.2 branch to confirm it has been taken care of that would be great! Otherwise, I'll close this as fixed in a few days.

needs_info

@jornjambers
Copy link

Possible workaround when "force: true" is required:

- name: cleanup clone dir cassandra
  become: yes
  file: 
    path: /tmp/php-driver/cassandra
    state: absent

 name: Cloning php-cassandra repo
  become: yes
  git:
    repo: https://github.com/datastax/php-driver.git
    dest: /tmp/php-driver/cassandra
    version: "v{{ php_pecl_cassandra_version }}"
    track_submodules: yes
    #force: yes

@abadger abadger closed this as completed Dec 19, 2016
jbabe pushed a commit to amaabca/ansible-newrelic-inode-plugin that referenced this issue Dec 22, 2016
- Update playbook to make ansible-lint . return a zero exit status

- Moves previous commits from a playbook into the structure for a role

- Sets some role defaults and raises errors when variables are not set

- Applies workarounds for git clone error that appeared when version was defined (version is defined so ansible-lint passes)

See Also:
- ansible/ansible-modules-core#5504
- ansible/ansible#19057
jbabe pushed a commit to amaabca/ansible-newrelic-inode-plugin that referenced this issue Dec 22, 2016
- Update playbook to make ansible-lint . return a zero exit status

- Moves previous commits from a playbook into the structure for a role

- Sets some role defaults and raises errors when variables are not set

- Applies workarounds for git clone error that appeared when version was defined (version is defined so ansible-lint passes)

See Also:
- ansible/ansible-modules-core#5504
- ansible/ansible#19057
paulRbr added a commit to swcc/ansible-certbot-role that referenced this issue Mar 3, 2017
sf-project-io pushed a commit to softwarefactory-project/sf-config that referenced this issue Apr 26, 2017
This change fixes the following issues encounter with the last version
of ansible:

* Unable to find '...' in expected paths. Relative role path are not
  supported anymore, use absolute path instead.
* ansible/ansible-modules-core#5504 broke
  git module when force is used. Apply dirty patch in the meantime.

Change-Id: Iac77c2b7d423a63fec75ea569b046ade06d2d2fb
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests