I have a playbook that I’d like to use to populate a particular directory on the managed host by rsyncing the contents of that same directory on a different host, but only in the case where the directory in question does not yet exist on the managed host. I tried doing that first with a task like this:
- name: rsync data directory $item from prodhost if it doesn’t exist already
action: command /usr/bin/rsync -avz $prodhost:/home/jbcsys/data/$item /home/jbcsys/data/
only_if: “not os.path.exists(‘/home/jbcsys/data/$item’)”
with_items: - foo
- bar
…and at first I thought it was working the way I intended. But then I realized that that was an artifact of how I was testing the playbook: by running ansible-playbook on the same host that I was actually trying to manage. I thought the python expression in the only_if was being executed on the managed host. But it’s apparently being executed on the host where I’m running ansible-playbook.
So far I’ve worked around this by writing a script that exists on the managed host that does the same thing (that is, that has internal logic to rsync the files in question if the directory does not yet exist on localhost), then I run that script on the managed host by invoking “command” in my ansible playbook. But this seems like an obvious thing to want to be able to do within ansible (to have execution of a command on the managed host be conditional on some fact about the filesystem on that managed host), so I’m wondering if there’s an obvious way to do this in ansible that I’ve overlooked. Is there?
Thanks.