Ansible 播放使用 lftp 上传 ftp 文件

Ansible play to upload ftp file using lftp

我使用 ansible 设置了 ftp 服务器,并通过从我的本地主机

手动执行 lftp 来验证它是否正常工作

我使用 put 上传了一个文件,当我执行 ls 时,我能够在 ftp 服务器中找到该文件。

我写了一个将文件上传到 ftp 服务器的剧本,它总是返回成功。但是,我找不到我使用我的游戏上传的文件。然后我编辑了我的游戏以显示结果寄存器并发现了警告。见下文

---
- name: test ftp upload
  hosts: localhost
  tasks:
    - name: install lftp
      yum:
        name: lftp
    - name: upload file
      shell: >
        lftp ansible1.example.com<<EOF
        cd pub
        put /etc/hosts
        bye
        EOF
      register: result

    - name: display result
      debug:
        var: result

下面是我在 运行 播放

后得到的输出
PLAY [test ftp upload] ****************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************
ok: [localhost]

TASK [install lftp] *******************************************************************************************************************************************************************
ok: [localhost]

TASK [upload file] ********************************************************************************************************************************************************************
changed: [localhost]

TASK [display result] *****************************************************************************************************************************************************************
ok: [localhost] => {
    "result": {
        "changed": true,
        "cmd": "lftp ansible1.example.com<<EOF cd pub put /etc/hosts bye EOF\n",
        "delta": "0:00:00.010150",
        "end": "2020-04-04 09:15:26.305530",
        "failed": false,
        "rc": 0,
        "start": "2020-04-04 09:15:26.295380",
        "stderr": "/bin/sh: warning: here-document at line 0 delimited by end-of-file (wanted `EOF')",
        "stderr_lines": [
            "/bin/sh: warning: here-document at line 0 delimited by end-of-file (wanted `EOF')"
        ],
        "stdout": "",
        "stdout_lines": []
    }
}

PLAY RECAP ****************************************************************************************************************************************************************************
localhost                  : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

我无法理解警告 w.r.t EOF 部分以及是否是文件上传失败的原因。因为,我使用下面的 shell 脚本(基本上与剧中的步骤相同)上传文件并且它完美无缺。

lftp ansible1.example.com<<EOF
cd pub
put /etc/hosts
bye
EOF

您为 shell 命令使用了错误的标量块样式指示器(参见 documentation)。

您需要在 shell 代码段中保留新行。在这种情况下,您必须使用文字标量块指示器:|

您已经使用了折叠标量块指示器 (>),它基本上会将新行转换为空格(尽管它有点复杂,如 this other source 中所述)。因此,您当前的 shell 代码在您的 shell 中扩展为一行(您可以调试该值以自己查看):

lftp ansible1.example.com<<EOF cd pub put /etc/hosts bye EOF

以下应该可以解决您当前的问题:

    - name: upload file
      shell: |
        lftp ansible1.example.com<<EOF
        cd pub
        put /etc/hosts
        bye
        EOF
      register: result