9

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

Shai Shvili
  • 99
  • 1
  • 1
  • 6

7 Answers7

24

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.

David Pärsson
  • 6,038
  • 2
  • 37
  • 52
  • 2
    During development of Ansible 2.8 `docker_container_facts` has been renamed to `docker_container_info`. Documentation is now available at https://docs.ansible.com/ansible/devel/modules/docker_container_info_module.html – Rafał Krypa Apr 26 '19 at 10:51
  • it should be `result.container`. – GeoCom May 31 '20 at 14:00
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

KVS
  • 141
  • 3
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"
sudheerchamarthi
  • 1,081
  • 8
  • 13
  • 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`. – TBirkulosis Aug 11 '22 at 16:12
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 :)

Jasmine Hegman
  • 547
  • 7
  • 22
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.

  • As of Ansible 2.8 `docker_container_facts` actually exists: https://docs.ansible.com/ansible/devel/modules/docker_container_facts_module.html – David Pärsson Jan 25 '19 at 14:14
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

itiic
  • 3,284
  • 4
  • 20
  • 31
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.

Nabeel Ahmed
  • 18,328
  • 4
  • 58
  • 63