git module often marked as changed even when nothing changed

Using ansible 2.6

In our playbook, we are using the git module to checkout a specific repo. Most of the time, the playbook reports this as changed even though the repo has not changed. Sometimes it does properly report as not changed.

Here is the git module parameters:

  • name: checkout our repo
    git:
    repo: “git@{{ serverurl }}:repo.git”
    dest: /opt/repofolder
    key_file: /path/.ssh/sshkey
    ssh_opts: “-o StrictHostKeyChecking=no”

accept_hostkey: True
update: yes

become_user: localuser

When it reports as changed, it returns:

“after”: “f35210a1d1d008e5a7abc328733cab87c06589a6”,
“before”: “f35210a1d1d008e5a7abc328733cab87c06589a6”,
“changed”: true,

“remote_url_changed”: true

I’ve duplicated the directory and compared the entire directory. Some folders have a newer modified date. But no files were changed, with one exception, the file /opt/repofolder/.git/logs/HEAD has this added line:

f35210a1d1d008e5a7abc328733cab87c06589a6 f35210a1d1d008e5a7abc328733cab87c06589a6 localuser localuser@computername.com 1572275580 +0000 checkout: moving from master to master

That line gets added even when ansible reports no change. And also, we use these same settings in other playbooks for other repos and those correctly only report as changed when changed.

Any idea why this is happening?

Thanks.

According to the return, it states:

“remote_url_changed”: true

This means that the URL for the remote is different as compared between the repo argument for the git module, and the value specified in .git/config within that repo.

You can compare what is in .git/config via the command ansible uses: git ls-remote --get-url origin from within /opt/repofolder

Past that, you would likely need additional debugging, following the code of the git module to determine why it’s not the same value.

Thanks for the reply Matt.

You were right that the git url was different. I changed the url in .git/config and now the output of ‘git ls-remote --get-url origin’ matches what is in the ansible repo parameter. However, it is still reporting as Changed most of the time that I run the playbook. And remote_url_changed is still returning true on those runs.

Any idea how I could troubleshoot this further?

Actually, it looks like you were right Matt. We had some other external process that was changing that url back.