無法在遠端主機上執行ansible play

無法在遠端主機上執行ansible play

我試圖在遠端主機上的 ansible 中執行多個 shell 命令,但失敗得很慘:

   - name: Mkdir on server and copy packages locally
     shell: |
      mkdir /root/vdbench
      cd /root
      cp vdbench50403.zip /root/vdbench
      cd /root/vdbench
      unzip vdbench50403.zip

錯誤:

TASK [Mkdir on server and copy packages locally] *******************************
fatal: [153.254.108.166]: FAILED! => {"changed": true, "cmd": "mkdir /root/vdbench\n cd /root\n cp vdbench50403.zip /root/vdbench\n cd /root/vdbench\n unzip vdbench50403.zip", "delta": "0:00:00.006011", "end": "2017-05-26 07:24:30.518445", "failed": true, "rc": 127, "start": "2017-05-26 07:24:30.512434", "stderr": "mkdir: cannot create directory ‘/root/vdbench’: File exists\n/bin/sh: line 4: unzip: command not found", "stdout": "", "stdout_lines": [], "warnings": ["Consider using file module with state=directory rather than running mkdir"]}
fatal: [153.254.108.165]: FAILED! => {"changed": true, "cmd": "mkdir /root/vdbench\n cd /root\n cp vdbench50403.zip /root/vdbench\n cd /root/vdbench\n unzip vdbench50403.zip", "delta": "0:00:00.005799", "end": "2017-05-26 07:24:30.740551", "failed": true, "rc": 127, "start": "2017-05-26 07:24:30.734752", "stderr": "mkdir: cannot create directory ‘/root/vdbench’: File exists\n/bin/sh: line 4: unzip: command not found", "stdout": "", "stdout_lines": [], "warnings": ["Consider using file module with state=directory rather than running mkdir"]}
fatal: [153.254.108.164]: FAILED! => {"changed": true, "cmd": "mkdir /root/vdbench\n cd /root\n cp vdbench50403.zip /root/vdbench\n cd /root/vdbench\n unzip vdbench50403.zip", "delta": "0:00:00.006032", "end": "2017-05-26 07:24:30.745565", "failed": true, "rc": 127, "start": "2017-05-26 07:24:30.739533", "stderr": "mkdir: cannot create directory ‘/root/vdbench’: File exists\n/bin/sh: line 4: unzip: command not found", "stdout": "", "stdout_lines": [], "warnings": ["Consider using file module with state=directory rather than running mkdir"]}
fatal: [153.254.108.163]: FAILED! => {"changed": true, "cmd": "mkdir /root/vdbench\n cd /root\n cp vdbench50403.zip /root/vdbench\n cd /root/vdbench\n unzip vdbench50403.zip", "delta": "0:00:00.006703", "end": "2017-05-26 07:24:30.832733", "failed": true, "rc": 127, "start": "2017-05-26 07:24:30.826030", "stderr": "mkdir: cannot create directory ‘/root/vdbench’: File exists\n/bin/sh: line 4: unzip: command not found", "stdout": "", "stdout_lines": [], "warnings": ["Consider using file module with state=directory rather than running mkdir"]}

答案1

閱讀輸出告訴您的內容:

  • mkdir 失敗,因為目錄已存在
  • 目標系統缺少 unzip 指令。

若要修復錯誤,請執行以下操作:

  • 將 -p 標誌傳遞給 mkdir
  • 在目標伺服器上安裝 unzip

或者,更好的是,更改您的劇本以使用 ansible 模組執行操作,而不是僅在目標主機上執行 shell 命令。

- name: Extract vdbench
  unarchive:
    src: vdbench50403.zip
    dest: /root/vdbench

請注意,src 是您執行 ansible 的電腦上的一個檔案。

相關內容