如何使用docker_host_info过滤docker个退出状态的容器?
How to filter docker containers in exited status using docker_host_info?
是否有任何其他 ansible-docker 模块来捕获 VM 上的所有容器,甚至是处于退出状态的容器。
- name: Get container info
docker_host_info:
containers: yes
register: result
docker_host_info 仅捕获处于 UP 状态的容器。有没有办法捕获或过滤处于退出状态的那些。请帮忙。
您需要使用带有过滤器 status=exited
的 containers_filter 选项。
检查这个 ansible 剧本:
---
- 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 }}"
我的 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]$
运行 ansible 剧本:
[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]$
如您所见,仅列出了退出的容器。
更新:
要同时列出 运行 和已退出的容器,请使用多个 ansible 任务。
Task 1:
- name: Get exited container info
|
containers_filters:
status: "exited"
|
Task 2
- name: Get running container info
|
containers_filters:
status: "running"
是否有任何其他 ansible-docker 模块来捕获 VM 上的所有容器,甚至是处于退出状态的容器。
- name: Get container info
docker_host_info:
containers: yes
register: result
docker_host_info 仅捕获处于 UP 状态的容器。有没有办法捕获或过滤处于退出状态的那些。请帮忙。
您需要使用带有过滤器 status=exited
的 containers_filter 选项。
检查这个 ansible 剧本:
---
- 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 }}"
我的 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]$
运行 ansible 剧本:
[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]$
如您所见,仅列出了退出的容器。
更新:
要同时列出 运行 和已退出的容器,请使用多个 ansible 任务。
Task 1:
- name: Get exited container info
|
containers_filters:
status: "exited"
|
Task 2
- name: Get running container info
|
containers_filters:
status: "running"