OStack程序员社区-中国程序员成长平台

标题: docker - 使用 Ansible 运行 'docker volume create'? [打印本页]

作者: 菜鸟教程小白    时间: 2022-8-3 10:12
标题: docker - 使用 Ansible 运行 'docker volume create'?

我有一个 Rails 应用程序,我通过 Ansible 在 Docker 容器中部署。到目前为止,我的应用程序包括三个容器:

  • 一个 Docker 卷容器(使用 docker volume create --name dbdata 创建)
  • 一个 Postgres 容器(带有 volumes_from dbdata)
  • Rails 应用程序容器(链接到 postgres 容器)

  • 我的部署剧本正在运行,但我必须运行 docker volume create通过 SSH 在服务器上执行命令。我很想通过 Ansible 做到这一点,这样我就可以将应用程序的一个新实例部署到一个空容器上。

    有没有办法运行docker volume create通过 Ansible,还是有其他方法可以做到?我检查了 Ansible Docker 模块的文档,但看起来他们不支持 volume create然而。除非我错过了什么?



    Best Answer-推荐答案


    这是一种选择,使用 command模块。

    - hosts: localhost
      tasks:
        - name: check if myvolume exists
          command: docker volume inspect myvolume
          register: myvolume_exists
          failed_when: false
    
        - name: create myvolume
          command: docker volume create --name myvolume
          when: myvolume_exists|failed
    

    我们首先使用 docker volume inspect 检查卷是否存在.我们将该任务的结果保存在变量 myvolume_exists 中。 ,然后我们只创建卷如果 inspect任务失败。

    关于docker - 使用 Ansible 运行 'docker volume create'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35047813/






    欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4