Ansible解析循环的结果

时间:2017-03-22 13:27:25

标签: loops parsing ansible

相当新的ansible - 这就是我想要做的。我创建了一个监视某些服务器的剧本。该剧本中的任务之一是检查服务器是否成功备份。

这是任务:

 when(classUnderTest.getAllBytesWrapper(any(File.class))).thenReturn("test".getBytes());

我在这里访问输出:

-name: Grabbing all of the files that were created today on Server1
 shell: find /back/up/location/ -maxdepth 1 -daystart -ctime 0 -print
 with_items:
   - '{{ inventory_hostname }}'
 when: item == "Server1"
 register: Server1

输出显示为:

-debug: var=Server1.results

然而,我真的只关心这个:

ok: [Server1] => {
"changed": false, 
"Server1.results": [
    {
        "_ansible_item_result": true, 
        "_ansible_no_log": false, 
        "_ansible_parsed": true, 
        "changed": true, 
        "cmd": "find /back/up/location/ -maxdepth 1 -daystart -ctime 0 -print", 
        "delta": "0:00:00.007869", 
        "end": "2017-03-21 16:39:27.110185", 
        "invocation": {
            "module_args": {
                "_raw_params": "find /back/up/location/ -maxdepth 1 -daystart -ctime 0 -print",
                "_uses_shell": true, 
                "chdir": null, 
                "creates": null, 
                "executable": null, 
                "removes": null, 
                "warn": true
            }
        }, 
        "item": "Server1", 
        "rc": 0, 
        "start": "2017-03-21 16:39:27.102316", 
        "stderr": "", 
        "stderr_lines": [], 
        "stdout": "/back/up/location/\n/back/up/location/FULL-2017-03-22_14-22",
        "stdout_lines": [
            "/back/up/location/", 
            "/back/up/location/FULL-2017-03-22_14-22"
        ]
    }
]

为了进行以下比较:

          ` "stdout_lines": [
            "/back/up/location/", 
            "/back/up/location/FULL-2017-03-22_14-22"
        ]`

在ansible中是否有办法解析-debug: msg="Backup was successful" when: Server1.results|length != 0 只包含Server1.results字段?欢迎任何建议,非常感谢任何帮助。

1 个答案:

答案 0 :(得分:-1)

你应该可以使用: -debug:var = Server1.results [0] .stdout_lines