Ansible w/ Docker - Show current Container state
Asked Answered
G

7

10

Im working on a little Ansible project in which I'm using Docker Containers.

I'll keep my question short:

I want to get the state of a running Dockercontainer!

What I mean by that is, that i want to get the current state of the container, that Docker shows you by using the "docker ps" command.

Examples would be:

  1. Up
  2. Exited
  3. Restarting

I want to get one of those results from a specific container. But without using the Command or the Shell module!

KR

Granddaddy answered 21/7, 2017 at 12:24 Comment(0)
C
25

As of Ansible 2.8 you can use the docker_container_info, which essentially returns the input from docker inspect <container>:

- name: Get infos on container
  docker_container_info:
    name: my_container
  register: result

- name: Does container exist?
  debug:
    msg: "The container {{ 'exists' if result.exists else 'does not exist' }}"

- name: Print the status of the container
  debug:
    msg: "The container status is {{ result.container['State']['Status'] }}"
  when: result.exists

With my Docker version, State contains this:

"State": {
    "Status": "running",
    "Running": true,
    "Paused": false,
    "Restarting": false,
    "OOMKilled": false,
    "Dead": false,
    "Pid": 8235,
    "ExitCode": 0,
    "Error": "",
    "StartedAt": "2019-01-25T14:10:08.3206714Z",
    "FinishedAt": "0001-01-01T00:00:00Z"
}

See https://docs.ansible.com/ansible/2.8/modules/docker_container_info_module.html for more details.

Corpuz answered 25/1, 2019 at 14:21 Comment(2)
During development of Ansible 2.8 docker_container_facts has been renamed to docker_container_info. Documentation is now available at docs.ansible.com/ansible/devel/modules/…Endomorphic
it should be result.container.Otherworldly
R
7

Unfortunately, none of the modules around docker can currently "List containers". I did the following as work around to grab the status:

- name: get container status
  shell: docker ps -a -f name={{ container }} --format {%raw%}"table {{.Status}}"{%endraw%} | awk 'FNR == 2 {print}' | awk '{print $1}'
  register: status

Result will then be available in the status variable

Radcliff answered 10/8, 2017 at 21:26 Comment(0)
I
3

This worked for me:

    - name: Get container status
      shell: docker inspect --format={{ '{{.State.Running}}' }} {{ container_name }}
      register: status
    #Start the container if it is not running
    - name: Start the container if it is in stopeed state.
      shell: docker start heuristic_mestorf
      when: status.stdout != "true"
Ivette answered 30/10, 2018 at 10:57 Comment(1)
This worked for me. I also used ignore_errors: true for the case that the container doesn't exist at all, and then check return code status.rc.Footrope
C
2

Edit: If you are running Ansible 2.8+ you can use docker_container_info. See David Pärsson's answer for details.

Here is one way to craft it using the docker_container module (note that it will create the container if it does not exist):

- name: "Check if container is running"
  docker_container:
    name: "{{ container_name }}"
    state: present
  register: container_test_started
  ignore_errors: yes

- set_fact:
    container_exists: "{{ container_test_started.ansible_facts is defined }}"

- set_fact:
    container_is_running: "{{ container_test_started.ansible_facts is defined and container_test_started.ansible_facts.docker_container.State.Status == 'running' }}"
    container_is_paused: "{{ container_test_started.ansible_facts is defined and container_test_started.ansible_facts.docker_container.State.Status == 'paused' }}"

For me the gotchya was that if the container doesn't exist, ansible_facts is not defined. If it does though, then that contains basically the whole docker inspect <container> output so I navigate that for the status.

If you just need to short circuit, a simpler alternative would be to move the desired set_fact'd value into a failed_when on the docker_container task.

I do it through set_fact to keep my options open for forking behavior elsewhere.. e.g. stop, do task, then put back how it was.

I included pause because it is commonly forgotten as a state :)

Convolvulaceous answered 24/12, 2018 at 6:10 Comment(5)
This will in fact start the container if it exists but is stopped, which is not what the OP asks for.Trenchant
Oh dear, good catch! I think adding "state: present" to the "docker_container" action would help thatConvolvulaceous
It would, but it would still create the container if it does not exist. That might be okay, but it's a side effect a user should be aware of.Trenchant
True, I will add a note to clarify that as well and a reference to see your answer. Thanks!Convolvulaceous
This doesn't work. The docker_container module complains if state=present and image property is not specified.Endomorphic
D
1

There is an ansible module docker_image_facts which give you information about images. You are looking for something that would be docker_container_facts, which does not currently exist. Good idea though.

Doriandoric answered 21/7, 2017 at 13:11 Comment(1)
As of Ansible 2.8 docker_container_facts actually exists: docs.ansible.com/ansible/devel/modules/…Trenchant
K
0

The question is not clear, but generally speaking you can use ansible with docker in two cases:

  • by using docker module of ansible

http://docs.ansible.com/ansible/latest/docker_module.html

- name: data container
  docker:
    name: mydata
    image: busybox
    state: present
    volumes:
    - /data
  • by calling ansible inside Dockerfile

    FROM centos7 RUN ansible-playbook -i inventory playbook.yml

Knorring answered 21/7, 2017 at 12:48 Comment(1)
The docker module is DEPRECATED for a while now.Overcurious
B
0

Your question is slightly unclear.

My best try - you want to have output of 'docker ps' - the first thing comes in mind is to use the Ansible command module, but you don't want to use it.

Then there are few docker modules:

  1. docker - This is the original Ansible module for managing the Docker container life cycle.
  2. docker_container - ansible module to manage the life cycle of docker containers.

You can look into the options -> parameters to get what exactly you're looking for.


Here's the complete list of Ansible modules for managing/using Docker.

Beckner answered 21/7, 2017 at 13:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.