使用 ansible 在 RHEL VM 上安装 Docker

Install Docker on RHEL VM using ansible

我想使用 Ansible 从远程 RHEL VM 上的二进制文件安装 Docker CE。我已按照 guide 中提到的步骤制作了剧本。我跳过了可选的第三步,因为我使用的是可执行文件的完整路径。但是,当我的脚本进入第 4 步时 - 启动 Docker 守护程序,它不会启动。

我看到的调试输出是:

TASK [Debug] *********************************************************************************************************************************************************************************************************************************
ok: [10.230.166.49] => {
"tmp": {
    "changed": true, 
    "cmd": "sudo /root/docker/dockerd &", 
    "delta": "0:00:00.025462", 
    "end": "2018-02-16 22:26:58.213010", 
    "failed": false, 
    "rc": 0, 
    "start": "2018-02-16 22:26:58.187548", 
    "stderr": "time=\"2018-02-16T22:26:58.211441819-05:00\" level=warning msg=\"could not change group /var/run/docker.sock to docker: group docker not found\"\nFailed to connect to containerd: exec: \"docker-containerd\": executable file not found in $PATH", 
    "stderr_lines": [
        "time=\"2018-02-16T22:26:58.211441819-05:00\" level=warning msg=\"could not change group /var/run/docker.sock to docker: group docker not found\"", 
        "Failed to connect to containerd: exec: \"docker-containerd\": executable file not found in $PATH"
    ], 
    "stdout": "", 
    "stdout_lines": [], 
    "warnings": [
        "Consider using 'become', 'become_method', and 'become_user' rather than running sudo"
    ]
} }

这是我的 Ansible 剧本:

---
 - hosts: remotes
   become: yes
   tasks:
    - name: Fetch docker version
      shell: docker version 2>&1 | grep Version | awk '{print }'
      register: docker_version 

    - name: Check if docker is installed, download binary otherwise
      command: curl -O https://download.docker.com/linux/static/stable/x86_64/docker-17.12.0-ce.tgz
      when: docker_version.stdout == ''

    - name: Extract binary
      unarchive: 
        src: /root/docker-17.12.0-ce.tgz
        dest: /root/
        remote_src: yes
      when: docker_version.stdout == ''

    - name: start docker
      shell: sudo /root/docker/dockerd &
      register: tmp

    - name: Debug
      debug: 
        var: tmp

附带说明一下,我登录到远程虚拟机并 运行

ls -al docker

并发现我在提取二进制文件后得到的目录 docker 属于一个组 vradeploy。这是输出:

total 107112
drwxr-xr-x  2 root vradeploy     4096 Dec 27 15:13 .
dr-xr-x---. 7 root root          4096 Feb 16 20:53 ..    
-rwxr-xr-x  1 root vradeploy 19938610 Dec 27 15:13 docker
-rwxr-xr-x  1 root vradeploy 15433832 Dec 27 15:13 docker-containerd
-rwxr-xr-x  1 root vradeploy 12773768 Dec 27 15:13 docker-containerd-ctr
-rwxr-xr-x  1 root vradeploy  4320064 Dec 27 15:13 docker-containerd-shim
-rwxr-xr-x  1 root vradeploy 46366152 Dec 27 15:13 dockerd
-rwxr-xr-x  1 root vradeploy   760040 Dec 27 15:13 docker-init
-rwxr-xr-x  1 root vradeploy  2517244 Dec 27 15:13 docker-proxy
-rwxr-xr-x  1 root vradeploy  7550928 Dec 27 15:13 docker-runc

刚刚花了一点时间让你看看我是否能弄明白。下面的剧本是我为您整理的,确实有效。虽然它与您的原始代码有点不同。但只是想和你分享一下,看看对你有没有帮助。

- hosts: test_nodes
  vars:
    _docker_version: 17.12.0
  tasks:
    - group:
        name: docker
        state: present
      become: true

    - name: Setting HOME Directory Path
      set_fact:
        _home: "{{ ansible_env['HOME'] }}"

    - name: Fetch docker version
      shell: docker version 2>&1 | grep Version | awk '{print }'
      register: docker_version

    - name: Check if docker is installed, download binary otherwise
      get_url:
        url: "https://download.docker.com/linux/static/stable/x86_64/docker-{{ _docker_version }}-ce.tgz"
        dest: "{{ _home }}"
      when: docker_version.stdout == ''

    - name: Extract binary
      unarchive:
        src: "{{ _home }}/docker-{{ _docker_version }}-ce.tgz"
        dest: "{{ _home }}/"
        remote_src: yes
        creates: "{{ _home }}/docker/dockerd"
      when: docker_version.stdout == ''

    - name: Copy Docker Binaries to /usr/bin
      shell: cp {{ _home }}/docker/docker* /usr/bin/
      args:
        creates: /usr/bin/docker
      become: true

    - name: start docker
      shell: dockerd &
      become: true
      args:
        creates: /var/run/docker.pid
      register: tmp

    - name: Debug
      debug:
        var: tmp

结果如下:

TASK [start docker] ************************************************************************************************************************************************************
Sunday 18 February 2018  01:25:17 -0500 (0:00:00.492)       0:00:09.419 *******
changed: [node0]

TASK [Debug] *******************************************************************************************************************************************************************
Sunday 18 February 2018  01:25:19 -0500 (0:00:01.357)       0:00:10.776 *******
ok: [node0] => {
    "tmp": {
        "changed": true,
        "cmd": "dockerd &",
        "delta": "0:00:01.135519",
        "end": "2018-02-18 01:25:19.879137",
        "failed": false,
        "rc": 0,
        "start": "2018-02-18 01:25:18.743618",
        "stderr": "time=\"2018-02-18T01:25:18.758962434-05:00\" level=info msg=\"libcontainerd: started new docker-containerd process\" pid=20346\ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=\"starting containerd\" module=containerd revision=89623f28b87a6004d4b785663257362d1658a729 version=v1.0.0 \ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=\"setting subreaper...\" module=containerd \ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=\"changing OOM score to -500\" module=containerd \ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.content.v1.content\"...\" module=containerd type=io.containerd.content.v1 \ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.snapshotter.v1.btrfs\"...\" module=containerd type=io.containerd.snapshotter.v1 \ntime=\"2018-02-18T01:25:18-05:00\" level=warning msg=\"failed to load plugin io.containerd.snapshotter.v1.btrfs\" error=\"path /var/lib/docker/containerd/daemon/io.containerd.snapshotter.v1.btrfs must be a btrfs filesystem to be used with the btrfs snapshotter\" module=containerd \ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.snapshotter.v1.overlayfs\"...\" module=containerd type=io.containerd.snapshotter.v1 \ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.metadata.v1.bolt\"...\" module=containerd type=io.containerd.metadata.v1 \ntime=\"2018-02-18T01:25:18-05:00\" level=warning msg=\"could not use snapshotter btrfs in metadata plugin\" error=\"path /var/lib/docker/containerd/daemon/io.containerd.snapshotter.v1.btrfs must be a btrfs filesystem to be used with the btrfs snapshotter\" module=\"containerd/io.containerd.metadata.v1.bolt\" \ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.differ.v1.walking\"...\" module=containerd type=io.containerd.differ.v1 \ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.gc.v1.scheduler\"...\" module=containerd type=io.containerd.gc.v1 \ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.grpc.v1.containers\"...\" module=containerd type=io.containerd.grpc.v1 \ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.grpc.v1.content\"...\" module=containerd type=io.containerd.grpc.v1 \ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.grpc.v1.diff\"...\" module=containerd type=io.containerd.grpc.v1 \ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.grpc.v1.events\"...\" module=containerd type=io.containerd.grpc.v1 \ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.grpc.v1.healthcheck\"...\" module=containerd type=io.containerd.grpc.v1 \ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.grpc.v1.images\"...\" module=containerd type=io.containerd.grpc.v1 \ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.grpc.v1.leases\"...\" module=containerd type=io.containerd.grpc.v1 \ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.grpc.v1.namespaces\"...\" module=containerd type=io.containerd.grpc.v1 \ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.grpc.v1.snapshots\"...\" module=containerd type=io.containerd.grpc.v1 \ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.monitor.v1.cgroups\"...\" module=containerd type=io.containerd.monitor.v1 \ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.runtime.v1.linux\"...\" module=containerd type=io.containerd.runtime.v1 \ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.grpc.v1.tasks\"...\" module=containerd type=io.containerd.grpc.v1 \ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.grpc.v1.version\"...\" module=containerd type=io.containerd.grpc.v1 \ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.grpc.v1.introspection\"...\" module=containerd type=io.containerd.grpc.v1 \ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=serving... address=\"/var/run/docker/containerd/docker-containerd-debug.sock\" module=\"containerd/debug\" \ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=serving... address=\"/var/run/docker/containerd/docker-containerd.sock\" module=\"containerd/grpc\" \ntime=\"2018-02-18T01:25:18-05:00\" level=info msg=\"containerd successfully booted in 0.002771s\" module=containerd \ntime=\"2018-02-18T01:25:18.775686433-05:00\" level=info msg=\"[graphdriver] using prior storage driver: overlay2\"\ntime=\"2018-02-18T01:25:18.780002053-05:00\" level=info msg=\"Graph migration to content-addressability took 0.00 seconds\"\ntime=\"2018-02-18T01:25:18.780501640-05:00\" level=info msg=\"Loading containers: start.\"\ntime=\"2018-02-18T01:25:18.840608687-05:00\" level=info msg=\"Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip can be used to set a preferred IP address\"\ntime=\"2018-02-18T01:25:18.859447845-05:00\" level=info msg=\"Loading containers: done.\"\ntime=\"2018-02-18T01:25:18.863941249-05:00\" level=info msg=\"Docker daemon\" commit=c97c6d6 graphdriver(s)=overlay2 version=17.12.0-ce\ntime=\"2018-02-18T01:25:18.863990869-05:00\" level=info msg=\"Daemon has completed initialization\"\ntime=\"2018-02-18T01:25:18.877109649-05:00\" level=info msg=\"API listen on /var/run/docker.sock\"",
        "stderr_lines": [
            "time=\"2018-02-18T01:25:18.758962434-05:00\" level=info msg=\"libcontainerd: started new docker-containerd process\" pid=20346",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=\"starting containerd\" module=containerd revision=89623f28b87a6004d4b785663257362d1658a729 version=v1.0.0 ",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=\"setting subreaper...\" module=containerd ",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=\"changing OOM score to -500\" module=containerd ",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.content.v1.content\"...\" module=containerd type=io.containerd.content.v1 ",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.snapshotter.v1.btrfs\"...\" module=containerd type=io.containerd.snapshotter.v1 ",
            "time=\"2018-02-18T01:25:18-05:00\" level=warning msg=\"failed to load plugin io.containerd.snapshotter.v1.btrfs\" error=\"path /var/lib/docker/containerd/daemon/io.containerd.snapshotter.v1.btrfs must be a btrfs filesystem to be used with the btrfs snapshotter\" module=containerd ",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.snapshotter.v1.overlayfs\"...\" module=containerd type=io.containerd.snapshotter.v1 ",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.metadata.v1.bolt\"...\" module=containerd type=io.containerd.metadata.v1 ",
            "time=\"2018-02-18T01:25:18-05:00\" level=warning msg=\"could not use snapshotter btrfs in metadata plugin\" error=\"path /var/lib/docker/containerd/daemon/io.containerd.snapshotter.v1.btrfs must be a btrfs filesystem to be used with the btrfs snapshotter\" module=\"containerd/io.containerd.metadata.v1.bolt\" ",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.differ.v1.walking\"...\" module=containerd type=io.containerd.differ.v1 ",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.gc.v1.scheduler\"...\" module=containerd type=io.containerd.gc.v1 ",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.grpc.v1.containers\"...\" module=containerd type=io.containerd.grpc.v1 ",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.grpc.v1.content\"...\" module=containerd type=io.containerd.grpc.v1 ",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.grpc.v1.diff\"...\" module=containerd type=io.containerd.grpc.v1 ",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.grpc.v1.events\"...\" module=containerd type=io.containerd.grpc.v1 ",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.grpc.v1.healthcheck\"...\" module=containerd type=io.containerd.grpc.v1 ",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.grpc.v1.images\"...\" module=containerd type=io.containerd.grpc.v1 ",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.grpc.v1.leases\"...\" module=containerd type=io.containerd.grpc.v1 ",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.grpc.v1.namespaces\"...\" module=containerd type=io.containerd.grpc.v1 ",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.grpc.v1.snapshots\"...\" module=containerd type=io.containerd.grpc.v1 ",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.monitor.v1.cgroups\"...\" module=containerd type=io.containerd.monitor.v1 ",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.runtime.v1.linux\"...\" module=containerd type=io.containerd.runtime.v1 ",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.grpc.v1.tasks\"...\" module=containerd type=io.containerd.grpc.v1 ",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.grpc.v1.version\"...\" module=containerd type=io.containerd.grpc.v1 ",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=\"loading plugin \"io.containerd.grpc.v1.introspection\"...\" module=containerd type=io.containerd.grpc.v1 ",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=serving... address=\"/var/run/docker/containerd/docker-containerd-debug.sock\" module=\"containerd/debug\" ",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=serving... address=\"/var/run/docker/containerd/docker-containerd.sock\" module=\"containerd/grpc\" ",
            "time=\"2018-02-18T01:25:18-05:00\" level=info msg=\"containerd successfully booted in 0.002771s\" module=containerd ",
            "time=\"2018-02-18T01:25:18.775686433-05:00\" level=info msg=\"[graphdriver] using prior storage driver: overlay2\"",
            "time=\"2018-02-18T01:25:18.780002053-05:00\" level=info msg=\"Graph migration to content-addressability took 0.00 seconds\"",
            "time=\"2018-02-18T01:25:18.780501640-05:00\" level=info msg=\"Loading containers: start.\"",
            "time=\"2018-02-18T01:25:18.840608687-05:00\" level=info msg=\"Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip can be used to set a preferred IP address\"",
            "time=\"2018-02-18T01:25:18.859447845-05:00\" level=info msg=\"Loading containers: done.\"",
            "time=\"2018-02-18T01:25:18.863941249-05:00\" level=info msg=\"Docker daemon\" commit=c97c6d6 graphdriver(s)=overlay2 version=17.12.0-ce",
            "time=\"2018-02-18T01:25:18.863990869-05:00\" level=info msg=\"Daemon has completed initialization\"",
            "time=\"2018-02-18T01:25:18.877109649-05:00\" level=info msg=\"API listen on /var/run/docker.sock\""
        ],
        "stdout": "",
        "stdout_lines": []
    }
}

PLAY RECAP *********************************************************************************************************************************************************************
node0                      : ok=10   changed=5    unreachable=0    failed=0