使用 Ansible 重启多个 Docker 容器没有发生 [英] Restart mutiple Docker Containers using Ansible not happening

查看:24
本文介绍了使用 Ansible 重启多个 Docker 容器没有发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Ansible 为特定图像一个一个地重新启动我的 docker 容器,但它似乎没有发生.下面是我的 yml,它正在做的是退出当前正在运行的容器.

I am trying to restart my docker containers one by one for a particular image using Ansible but it doesn't seem to be happening. Below is my yml and what it is doing is exiting the current running container.

---
- name: restart app servers
  hosts: shashank-VM
  connection: local
  become: yes
  become_method: sudo
  tasks:
    - name: Get info on the Container
      shell: docker ps | awk '/{{ item }}/{print $1}'
      register: list_of_containers
      with_items:
        - ubuntu

    - name: Restart Docker Service
      docker_container:
       name: "{{ item }}"
       # image: ubuntu
       state: started
       restart: yes
      with_items: "{{ list_of_containers.results | map(attribute='stdout_lines') | list }}"

如果您在运行 docker ps 时看到以下输出,则表示没有正在运行的容器.

If you see the below output when i run docker ps there are no running containers.

TASK [Restart Docker Service] ****************************************************************************************************************
/usr/lib/python2.7/dist-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 (1.25.9) or chardet (3.0.4) doesn't match a supported version!
  RequestsDependencyWarning)
changed: [shashank-VM] => (item=c2310b76b005)

PLAY RECAP ***********************************************************************************************************************************
shashank-VM                : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

shashank@shashank-VM:~/ansible$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

我做错了什么?有人可以帮忙吗?

What am i doing wrong? Can someone help?

推荐答案

我不认为 docker_container 模块旨在执行您想要的操作(即,重新启动现有容器).该模块旨在按名称而非 id 管理容器,并将检查正在运行的容器是否与提供给 docker_container 的选项匹配.

I don't think the docker_container module is designed to do what you want (i.e., restart an existing container). The module is designed to manage containers by name, not by id, and will check that the running container matches the options provided to docker_container.

您可能最好只使用 docker 命令重新启动容器:

You're probably better off simply using the docker command to restart your containers:

---
- name: restart app servers
  hosts: shashank-VM
  connection: local
  become: yes
  become_method: sudo
  tasks:
    - name: Get info on the Container
      shell: docker ps | awk '/{{ item }}/{print $1}'
      register: list_of_containers
      with_items:
        - ubuntu

    - name: Restart Docker Service
      command: docker restart {{ item }}
      with_items: "{{ list_of_containers.results | map(attribute='stdout_lines') | list }}"

这篇关于使用 Ansible 重启多个 Docker 容器没有发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆