stdout_lines中的Ansible搜索字符串,以后将用户作为变量

时间:2019-07-15 23:24:52

标签: ansible jinja2

我在stdout_lines中有一个不正常的输出

    "test.stdout_lines": [
        "Get VM Power State Script",
        "Loading vmware.vimautomation.core",
        "",
        "Attempting to connect to server1. Please wait.",
        "",
        "Connected to server1",
        "\tGathering VM Info...",
        "",
        "Attempting to connect to serevr2. Please wait.",
        "",
        "Connected to server2.",
        "\tGathering VM Info...",
        "File Exported to D:\\Scripts\\Exports\\VM_State_201907151824.csv . Please verify the content",
        "",
        "Complete."

我想将文件位置存储在输出的最后一行作为变量,即位置:-D:\ Scripts \ Exports \ VM_State_201907151824.csv。怎么办?

2 个答案:

答案 0 :(得分:0)

这对您有用吗?

{{ test.stdout_lines | join(' ') | regex_replace( '^.*File Exported to (.*) \. Please .*$', '\\1') }}

答案 1 :(得分:0)

尝试以下

- name: Get the file_path
  set_fact:
    file_path: "{{ test.stdout_lines | select('match', 'File Exported to.+') | list | regex_replace( '^.*File Exported to (.*) \\. Please .*$', '\\1') }}"
- name: debug
  debug:
    msg: "{{ file_path }}"