The syntax error you get could be caused by a python 3 interpreter being
used at the target machine instead of python 2. This is a common error
when running ansible on target machines running Archlinux because the
default python interpreter (at /usr/bin/python) on Arch is actually the
python3 interpreter. In these cases you should set the
"ansible_python_interpreter" variable for the affected hosts to the
correct path ("/usr/bin/python2" for archlinux).
That did fix the problem. I was assuming that isn't required when you
install using virtualenv. Everything else has been working fine so
far, this was the first thing I ran into an issue with while using
ansible from virtualenv. Should this be considered a bug? If so, I'll
open a new ticket on github.
It is not a bug. It is just that some hosts need to be configured so
that ansible uses the python2 interpreter when running on them. Also,
running from inside a virtualenv has nothing to do with this. Running
from outside (not in a virtualenv) would cause the same error. It is an
error caused on remote host (controlled), not on local host (controller).
Probably I'm failing to understand the internal working and should
spend more time understanding ansible. I was assuming that when
running inside virtualenv, it would use python installation inside
virtualenv only but that doesn't seem to be the case with synchronize
module. I should probably understand how it works internally to
understand why this happens.
Ansible runs first on the local machine (controller), then opens an ssh
connection to the remote machine (target), copies itself and needed
modules to the remote machine and invokes itself on the remote machine.
So, eventually you will have _two_ processes of ansible: one runs on
local machine and the other runs on the remote machine. If you happen to
have installed ansible on the local machine inside a virtual environment
and you have enabled that virtualenv, then the process running on the
local machine will be running in the context of that virtualenv. On the
other hand, the process that is running on the remote machine will have
been invoked with the python interpreter configured with
ansible_python_interpreter (which defaults to '/usr/bin/python').