Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Ansible docker_container, how can I display standard out? (stdout)

I'm using Ansible and its module docker_container to launch tape unit test containers in nodejs. This is nice because I don't have to have npm mess up my host, my only dev box dependency is python and docker.

I need to be able to see stdout to see that tests have been run. However, Docker's --attach option is not exposed in docker_container and I cannot find any way to have stdout print out from the ansible launch of the container.

I can go back to bash scripts to launch docker containers but I'd rather not...

How can I display a container's standard out with Ansible's docker_container module?

like image 401
Paul S Avatar asked Nov 16 '16 06:11

Paul S


People also ask

How do you use the Docker_container Ansible module?

The URL or Unix socket path used to connect to the Docker API. To connect to a remote host, provide the TCP connection string. For example, 'tcp://192.0.2.23:2376'. If TLS is used to encrypt the connection, the module will automatically replace 'tcp' in the connection URL with 'https'.

How does Ansible module connect to Docker API?

Environment Variables Control how the modules connect to the Docker API by setting the following variables in the environment of the host running Ansible: DOCKER_HOST. The URL or Unix socket path used to connect to the Docker API. DOCKER_API_VERSION.

Is there a Docker image for Ansible?

Run your Docker image with Ansible Now, with the Docker container image with Ansible installed, we have a ready-made Docker container image that can be copied to any container host and used to run Ansible with all the components already installed and ready.


2 Answers

This actually works with Ansible v2.5, by using the detach attribute:

- name: Run the tests
  docker_container:
    detach: false
    name: "foo-container"
    command: npm test
  register: docker_container_output

- name: Show test output
  debug:
    msg: "{{ docker_container_output.ansible_facts.docker_container.Output }}"

- name: Remove the test container
  docker_container:
    state: absent
    name: "foo-container"

It's important to specify detach: false to capture the output, and then to use the ansible_facts.docker_container.Output path to access the results.

like image 111
nwinkler Avatar answered Oct 05 '22 15:10

nwinkler


Don't use Ansible for running simple unit tests. Ansible makes complicated things easy and easy things complicated.

The following bash script is 4 lines and the Ansible playbook is already at 25 lines and still not fully functional due to concurrency issues and output formatting issues (I'm sure I'd find more).

Ansible for docker is still too new, doesn't deal easily with concurrency issues, has missing docker features, etc. and probably should only be used when you are doing something complicated that bash can't easily handle (like dealing with remote servers, retries, etc)

#!/bin/bash
docker build -t server-unit-test .
docker run -it --name server-unit-test server-unit-test npm test
docker rm server-unit-test
like image 29
Paul S Avatar answered Oct 05 '22 17:10

Paul S