dockeransibledocker-machineansible-2.xdockerpy

How to filter docker containers in exited status using docker_host_info?


Is there any other ansible-docker module to capture all the containers on the VM even the ones in exited status.

 - name: Get container info
    docker_host_info:
      containers: yes
    register: result

docker_host_info captures the containers only in UP status. Is there a way to capture or filter the ones in exited status. Kindly help.


Solution

  • You need to make use of containers_filter option with a filter status=exited.

    Check this ansible playbook:

    ---
    - name: First playbook
      hosts: 127.0.0.1
      gather_facts: true
      become: true
      tasks:
      - name: Get container info
        docker_host_info:
          containers: yes
          containers_filters:
            status: "exited"
        register: result
      - debug:
          msg: "Exited container list : {{ item['Names'] }}"
        loop: "{{ result.containers }}"
    
    

    I have few exited container on my VM.

    [centos@jumphost tmp]$ docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
    b2dacc3e5d04        httpd               "httpd-foreground"       6 minutes ago       Exited (137) 6 minutes ago                       goofy_noyce
    f54e2aa1c510        alpine              "/bin/sh"                44 minutes ago      Exited (0) 44 minutes ago                        dazzling_noyce
    db7789d302db        <no image>          "/bin/sh -c 'touch /…"   2 days ago          Created                                          fervent_morse
    3e948852f5cb        <no image>          "/bin/sh -c 'touch /…"   2 days ago          Created                                          goofy_wiles
    [centos@jumphost tmp]$
    

    Run the ansible playbook:

    [centos@jumphost tmp]$ sudo ansible-playbook -c local -i 127.0.0.1 a.yaml 
    [WARNING]: Unable to parse /var/tmp/127.0.0.1 as an inventory source
    [WARNING]: No inventory was parsed, only implicit localhost is available
    [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
    
    PLAY [First playbook] **************************************************************************************************************************************************************
    
    TASK [Gathering Facts] *************************************************************************************************************************************************************
    ok: [127.0.0.1]
    
    TASK [Get container info] **********************************************************************************************************************************************************
    ok: [127.0.0.1]
    
    TASK [debug] ***********************************************************************************************************************************************************************
    ok: [127.0.0.1] => (item={u'Status': u'Exited (137) 10 minutes ago', u'Command': u'httpd-foreground', u'Names': [u'/goofy_noyce'], u'Created': 1596717162, u'Image': u'httpd', u'Ports': [], u'Id': u'b2dacc3e5d048f6ae4143e0b76931f104d1dc132e81d378b2456aa5839e459a6'}) => {
        "msg": "Exited container list : [u'/goofy_noyce']"
    }
    ok: [127.0.0.1] => (item={u'Status': u'Exited (0) About an hour ago', u'Command': u'/bin/sh', u'Names': [u'/dazzling_noyce'], u'Created': 1596714877, u'Image': u'alpine', u'Ports': [], u'Id': u'f54e2aa1c510ece29d8665c65ae5de29552e7e280e7591c399a57a976f80fa81'}) => {
        "msg": "Exited container list : [u'/dazzling_noyce']"
    }
    
    PLAY RECAP *************************************************************************************************************************************************************************
    127.0.0.1                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    
    [centos@jumphost tmp]$
    

    As you can see only the exited containers are getting listed.

    Update:

    To list both running and exited container use multiple ansible task.

    Task 1:
    - name: Get exited container info
        |
    containers_filters:
            status: "exited"
        |
    Task 2
    - name: Get running container info
        |
    containers_filters:
            status: "running"