Getting Ansible to run commands inside a running Docker container

1,544 views
Skip to first unread message

Andre Castelan Prado

unread,
Feb 5, 2016, 1:02:11 PM2/5/16
to Ansible Project
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/

Dick Davies

unread,
Feb 7, 2016, 3:42:47 PM2/7/16
to ansible list
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.

On 5 February 2016 at 17:59, Andre Castelan Prado
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/1cac337e-7560-4001-852f-71c05a8cebbb%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages