为什么 ansible 不断重新创建 docker 状态为 "started" 的容器
Why ansible keeps recreating docker containers with state "started"
我有一个由 Ansible 管理的 docker 容器。每次我用 Ansible 启动容器时,它都会被重新创建而不是刚刚启动。
以下是我用于 stop/start 容器的 Ansible 命令:
ansible-playbook <playbook> -i <inventory> --extra-vars "state=stopped"
ansible-playbook <playbook> -i <inventory> --extra-vars "state=started"
这是我用来管理容器的 Ansible 任务。 "stop" 和 "start" 命令之间唯一不同的是 {{ state }}。
- docker:
name: "{{ postgres_container_name }}"
image: "{{ postgres_image_name }}"
state: "{{ state }}"
ports:
- "{{ postgres_host_port }}:{{ postgres_guest_port }}"
env:
POSTGRES_USER: "{{ postgres_user }}"
POSTGRES_PASSWORD: "{{ postgres_password }}"
POSTGRES_DB: "{{ postgres_db }}"
当我启动、停止和启动容器时,我从 Ansible 命令得到以下详细输出:
changed: [127.0.0.1] => {"ansible_facts": {"docker_containers": [{"Id": "ab1c0f6cc30de33aba31ce93671267783ba08a1294df40556870e66e8bf77b6d", "Warnings": null}]}, "changed": true, "containers": [{"Id": "ab1c0f6cc30de33aba31ce93671267783ba08a1294df40556870e66e8bf77b6d", "Warnings": null}], "msg": "removed 1 container, started 1 container, created 1 container.", "reload_reasons": null, "summary": {"created": 1, "killed": 0, "pulled": 0, "removed": 1, "restarted": 0, "started": 1, "stopped": 0}}
它表明容器已更改、已删除、已创建并已启动。
你能告诉我为什么 Ansible 看到我的容器已更改并重新创建它而不是启动它吗?
当您在 started
.
状态下使用时,Ansible 的 docker 模块将首先删除任何已停止的同名容器
module docs don't really make it all that clear but there is a comment explaining this in the source code in the started
function.
我有一个由 Ansible 管理的 docker 容器。每次我用 Ansible 启动容器时,它都会被重新创建而不是刚刚启动。
以下是我用于 stop/start 容器的 Ansible 命令:
ansible-playbook <playbook> -i <inventory> --extra-vars "state=stopped"
ansible-playbook <playbook> -i <inventory> --extra-vars "state=started"
这是我用来管理容器的 Ansible 任务。 "stop" 和 "start" 命令之间唯一不同的是 {{ state }}。
- docker:
name: "{{ postgres_container_name }}"
image: "{{ postgres_image_name }}"
state: "{{ state }}"
ports:
- "{{ postgres_host_port }}:{{ postgres_guest_port }}"
env:
POSTGRES_USER: "{{ postgres_user }}"
POSTGRES_PASSWORD: "{{ postgres_password }}"
POSTGRES_DB: "{{ postgres_db }}"
当我启动、停止和启动容器时,我从 Ansible 命令得到以下详细输出:
changed: [127.0.0.1] => {"ansible_facts": {"docker_containers": [{"Id": "ab1c0f6cc30de33aba31ce93671267783ba08a1294df40556870e66e8bf77b6d", "Warnings": null}]}, "changed": true, "containers": [{"Id": "ab1c0f6cc30de33aba31ce93671267783ba08a1294df40556870e66e8bf77b6d", "Warnings": null}], "msg": "removed 1 container, started 1 container, created 1 container.", "reload_reasons": null, "summary": {"created": 1, "killed": 0, "pulled": 0, "removed": 1, "restarted": 0, "started": 1, "stopped": 0}}
它表明容器已更改、已删除、已创建并已启动。
你能告诉我为什么 Ansible 看到我的容器已更改并重新创建它而不是启动它吗?
当您在 started
.
module docs don't really make it all that clear but there is a comment explaining this in the source code in the started
function.