Getting Ansible to run commands inside a running Docker container

Hello guys, I am using Ansible + Vagrant and having problem to run Django migrations inside a container that’s running my django app.

Basically I have a postgres container and a django container

If I vagrant ssh and then run

sudo docker exec -ti django /bin/sh -c ‘cd mumopus;python manage.py migrate’

Everything is good and life is beautiful

However if I try to run as an ansible task having

  • name: Run migrate on Django.
    become: yes
    command: docker exec -ti django /bin/sh -c ‘cd mumopus;python manage.py migrate’

I get

“django.db.utils.OperationalError: could not connect to server: Connection refused”, “\tIs the server running on host "172.17.0.2" and accepting”, “\tTCP/IP connections on port 5432?”]

I’ve run docker ps just prior to that task and all the containers are there and running, I’ve also tried to use ansible shell instead of command but no joy. I’ve made a thread in reddit with the codes, if anyone can help I would be very thankful

https://www.reddit.com/r/ansible/comments/44ao3t/ansible_command_module_in_playbook_in_role_not/

That's manage.py complaining that postgres isn't running where it
expects it to be, my guess would
be you're missing an environment variable to point to the correct
ip/port combo (if you're sure a straight
ssh session works) or you haven't started postgres in the container.