what I'm trying to accomplish is to run commands inside of a Docker container that has already been created on a Digital Ocean Ubuntu/Docker Droplet using Ansible.
Can't seem to find anything on this, or I'm majorly missing something. This is my Ansible task in my play book. I'm very new to Ansible so any advice or wisdom would be greatly appreciated.
- name: Test Deploy
hosts: [my-cluster-of-servers]
tasks:
- name: Go Into Docker Container And Run Multiple Commands
docker:
name: [container-name]
image: [image-ive-created-container-with-on-server]
state: present
command: docker exec -it [container-name] bash
Update: there is a way to do this without using my module, see my other answer
I wrote a simple module to run exec on a remote Docker host. I've submitted it to the ansible project, but you can easily add it to your own projects if you need to. The module is only 23 lines long, take it from my pull request and add it to your ./library directory, and then you can add a task in your playbook like so:
You should be able to execute a script (with your sequence of command in it) with
docker exec
:(See also "Why do I have to use
bash -l -c
inside my container?")/path/to/script
should be accessible by your Ansible process./path/to/log
is a path inside the container, that could be shared in a volume.Ended up doing something like that:
After discussion with some very helpful developers on the ansible github project, a better way to do this is like so:
If you have python installed in your image, you can use the command module or any other module instead of raw.
If you want to do this on a remote docker host, add:
to the add_host block.
See the Ansible documentation for a more complete example.
You can run commands within docker containers using the command module For example this code will execute
echo "Hello1"
andecho "Hello2"
in my_container: