Ansible-如何根据特定输出使任务失败

时间:2018-10-21 01:53:27

标签: ansible ansible-template

嗨,我有一个任务,要求将文件从远程文件箱ftp到ansible受控机器。我使用的是基于期望的FTP,所以如果远程文件箱中不存在文件,则希望ansible失败。以下是我的代码。我正在使用寄存器变量。但是我看不到调试显示寄存器内容。

     - name: copy files remote
       shell: |
         set timeout 1000
         spawn ftp {{ buildIP }}

         expect ":"
         send "{{ build_user }}\n"

    expect "ssword:"
    send "{{ build_password }}\n"

    expect "ftp>"
    send "get {{ build_path }} /root/Desktop/Sanity/{{  TID }}/{{ Image_dir }}/{{ build_filename }}\n"

    expect "ftp>"
    send "quit\n"

    set multiPrompt {[#$]}
    expect -re $multiPrompt
    exit 0
  args:
    executable: /usr/bin/expect
  register: shell_output
  tags: copy_source_code
- debug:
    var: shell_output.stdout_lines

1 个答案:

答案 0 :(得分:0)

我建议您使用get_url从FTP中获取文件,而不是期望的,它会在重演中显示任务是成功还是失败

播放示例:

---
- hosts: localhost
  tasks:
    - name: FTP Download
      get_url: url=ftp://username:password@localhost/file.zip dest=/tmp
      register: get_url_result

如果FTP服务器中不存在该文件,则输出将类似于以下内容:

  

PLAY RECAP ********************************************* ***********

     

localhost:ok = 1已更改= 0 =无法访问= 0
  失败= 1

在任务错误详细信息中,您也可以找到“未找到”错误

相关问题