Unwanted ansible-tmp in command

Unwanted ansible-tmp in command

I’m currently working on getting ansible to work with gcp in the scenario that direct ssh is not possible (which it is in my case). I’m using code provided in How to tell Ansible to use GCP IAP tunneling - Xebia

During the execution of a ansible.builtin.file call I’m faced with an error:

gcloud compute ssh. ... -- -C /bin/sh -c 'rm -f -r ansible-tmp-1736873865.9504788-1510864-250879130520453=/tmp/ansible/ansible-tmp-1736873865.9504788-1510864-250879130520453/ > /dev/null 2>&1 && sleep 0'

Somehow the ansible-tmp-.....= gets shoved in front of the remote path. In the gcloud command this breaks functionality.

If anybody know a way to omit this without me hacking the wrapper scripts it would be much appreciated.


I expect the my problem to lies somewhere in ansible/plugins/action/__init__.py the _make_tmp_path function that in turn calls mkdtemp in ansible/plugins/shell/__init__.py

in the function the command is constructed in ansible/lib/ansible/plugins/shell/__init__.py at v2.14.18 · ansible/ansible · GitHub . This makes a command like this

( umask 77 && mkdir -p "` echo /tmp/ansible `"&& mkdir "` echo /tmp/ansible/ansible-tmp-1736873865.9504788-1510864-250879130520453 `" && echo ansible-tmp-1736873865.9504788-1510864-250879130520453="` echo /tmp/ansible/ansible-tmp-1736873865.9504788-1510864-250879130520453 `" )

The reason I think this is because the cleanup function in action refers to the tmpdir property in shell which seems to be set by _make_tmp_path where it is a result of the shell._generate_temp_dir_name method. This seems to be a simple string passed from one function to another and the mangled string in the output looks much like the one I currently have trouble with.

Version info:

# molecule --version
molecule 6.0.3 using python 3.9 
    ansible:2.14.18
    default:6.0.3 from molecule

The format of the data returned by mkdtemp is <basefile>=<tmp_path>/<basefile> so that there is a string we can programatically look for to ensure we are splitting on the correct line of stdout.

This is handled in _make_tmp_path

The fact that the result of _make_tmp_path hasn’t split the return, to only give the path back, makes it sound as though the gcp-ssh-wrapper.sh file is screwing with the data somehow. If you run with -vvvv you should see lines that start something like the following:

<hostname> SSH: EXEC ssh ... umask 77 && mkdir -p ...
<hostname> (0, b'ansible-tmp-1736882451.581056-82192-176146295572129=/home/sivel/.ansible/tmp/ansible-tmp-1736882451.581056-82192-176146295572129\n', b"...")

The first line being the command that attempts to get the remote tmpdir, and the 2nd the response that is then parsed.

From there, I guess you have to determine what is not being returned correctly, and why _make_tmp_path isn’t parsing it right.

Make sense, I’ll dig into this and see if I can alter extend the output. Thanks.

Cleaned up the wrapper. Works now. Thanks!

can you post the revised wrapper, if the one on that post was insufficient?

This is my reference atm (before I started messing with it) GitHub - jvzantvoort/ansible-collection-jdc-services I’ll update it with the remainder as soon as I can.