Are you relying on .bashrc to load the environment for the user you are using to SSH with? There seems to be an environment difference from when Ansible does SSH to execute the command versus what you are doing locally. Try doing a debug by registering the command which g++
- name: Check location of program
command: which g++
register: result
- debug: var=result
Is this doing a sudo? sudo command will not source the .bashrc automatically.
I worked around it in my system with
- name: run script
shell: cd; bash -lc "script.sh > script.log 2>&1"
This forces the .bashrc to be loaded. I had LD_LIBRARY_PATH defined in my .bashrc that I needed to be loaded when executing the script. I know there are a dozen other ways probably could have been done, but due to legacy install reasons with my product, I was stuck with these ENV settings being in .bashrc, and I was not interested in trying to pull those ENV values over into my playbooks to prepend to the shell command.
May not be your issue, but I suspect a similar problem as it looks like make is looking for g++ / c++ and not finding it.
Lastly, I have ran into issues with TTY due to nested SSH and redirections, that is another area to check out when trying to execute applications remotely, that they behave when the TTY is / is not present. Doubt this is the case with make, but thought I would throw it out there in case it sprung a lightbulb.
Thanks!
Michael