Docker NFS volume using Ansible

旧时模样 提交于 2021-01-28 05:48:33

问题


Given a simple example such as

$ docker volume create --driver local \
    --opt type=nfs \
    --opt o=addr=192.168.1.1,rw \
    --opt device=:/path/to/dir \
    foo

How can I do the same using Ansible? I tried for example

- name: NFS volume mount
  docker_volume:
    driver: "local"
    driver_options:
      type: nfs
      o: "addr=192.168.1.1,rw"
      device: /path/to/dir
    volume_name: foo

Which will create the volume without errors but it will fail when the volume is used with docker_container module.

TASK [oracle-database : docker_container] **************************************
fatal: [oracle]: FAILED! => {"changed": false, "msg": "Error starting container 1beb3254e25a8fe47cd78e803a2050f24020cf72e138472d7d14d963e02bec7f: 500 Server Error: Internal Server Error (\"{\"message\":\"error while mounting volume '/var/lib/docker/volumes/foo/_data': failed to mount local volume: mount /path/to/dir:/var/lib/docker/volumes/foo/_data, data: addr=192.168.1.1: invalid argument\"}\")"}

When I create the volume using Ansibe

[root@oracle ~]# docker volume inspect oracle-dumps
[
    {
        "CreatedAt": "2020-08-31T02:07:14-07:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/foo/_data",
        "Name": "foo",
        "Options": {
            "device": "/path/to/dir",
            "o": "192.168.1.1,rw",
            "type": "nfs"
        },
        "Scope": "local"
    }
]

When I create the volume manually using docker volume create:

[root@oracle ~]# docker volume inspect oracle-dumps
[
    {
        "CreatedAt": "2020-08-31T02:07:14-07:00",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/foo/_data",
        "Name": "foo",
        "Options": {
            "device": "/path/to/dir",
            "o": "192.168.1.1,rw",
            "type": "nfs"
        },
        "Scope": "local"
    }
]

The labels attribute aside, looks pretty much the same.

docker inspect of the container is the same with regards to the volume

        {
            "Type": "volume",
            "Name": "oracle-dumps",
            "Source": "/var/lib/docker/volumes/foo/_data",
            "Destination": "/otherpath/to/dir",
            "Driver": "local",
            "Mode": "rw",
            "RW": true,
            "Propagation": ""
        }

So the conclusion is that there is a bug but where? Ansible? docker-py? Docker?

The workaround is

- name: Create dump volume
  command:
    cmd: "docker volume create --driver local --opt type=nfs --opt o=addr=192.168.1.1,rw --opt device=:/path/to/dir foo"

回答1:


The problem is the missing :. The device path should be :/path/to/dir

- name: NFS volume mount
  docker_volume:
    driver: "local"
    driver_options:
      type: nfs
      o: "addr=192.168.1.1,rw"
      device: :/path/to/dir
    volume_name: foo


来源:https://stackoverflow.com/questions/63666513/docker-nfs-volume-using-ansible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!