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

查看:482
本文介绍了使用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天全站免登陆