如何在输出上创建item_list

时间:2018-05-18 12:06:11

标签: ansible

我有一个问题是使用register:device_output从上一个任务的sdtout获取设备ID。此变量的内容如下所示:

changed: [mac100] => {
    "changed": true,
    "cmd": [
        "xxxxxxxxxxx",
        "-c"
    ],
    "delta": "0:00:05.015598",
    "end": "2018-05-18 17:56:17.829547",
    "invocation": {
        "module_args": {
            "_raw_params": "xxxxxxxxxx -c",
            "_uses_shell": false,
            "chdir": "/Users/xxx/Desktop/xxx/",
            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "warn": true
        }
    },
    "rc": 0,
    "start": "2018-05-18 17:56:12.813949",
    "stderr": "",
    "stderr_lines": [],
    "stdout": "[....] Waiting up to 5 seconds for iOS device to be connected\n[....] Found 3e194 (J82, iPad Air 2 (GSM), iphoneos, arm64) a.k.a. 'iPad' connected through USB.\n[....] Found 6cc84 (J81, iPad Air 2, iphoneos, arm64) a.k.a. 'iPad' connected through USB.",
    "stdout_lines": [
        "[....] Waiting up to 5 seconds for iOS device to be connected",
        "[....] Found 3e194 (J82, iPad Air 2 (GSM), iphoneos, arm64) a.k.a. 'iPad' connected through USB.",
        "[....] Found 6cc84 (J81, iPad Air 2, iphoneos, arm64) a.k.a. 'iPad' connected through USB."
    ]
}

我的计划是创建一个项目列表。上面输出中所有设备ID(3e194和6cc84)的id_list,以便我可以将它用于下一个循环任务

- name: do my task
  command: "do some thing {{ item }}"
  with_items: "{{ id_list }}"

非常感谢你!

1 个答案:

答案 0 :(得分:1)

我想我们需要从您的输出中获取的设备位于stdout"[....] Found 3e194 <not important string following>。这是使用正则表达式过滤的快速方法。

步骤1:将列表stdout_lines重新格式化为新列表,如果您在任何元素上找到该字符串的格式:

3e194

将其替换为final_list

第2步:逐项处理这个新的列表变量,如果项目的内容少于6个字符(也许你想匹配正好5个长度的字符串,由你决定),它是一个有效的设备ID,将其添加到- hosts: localhost gather_facts: false vars: stdout_lines: - "[....] Waiting up to 5 seconds for iOS device to be connected" - "[....] Found 3e194 (J82, iPad Air 2 (GSM), iphoneos, arm64) a.k.a. 'iPad' connected through USB." - "[....] Found 6cc84 (J81, iPad Air 2, iphoneos, arm64) a.k.a. 'iPad' connected through USB." tasks: - name: step 1 set_fact: reformat_list: "{{ stdout_lines | map('regex_replace', '^(\\[....\\] Found )([a-zA-Z0-9]*)(\\s)(.*)$', '\\2') | list }}" - name: step 2 set_fact: final_list: "{{ final_list | default([]) + [ item ]}}" when: item | length < 6 with_items: - "{{ reformat_list }}" - name: print debug: var: final_list

剧本:

PLAY [localhost] ****************************************************************************************************************************************************************************************************

TASK [step 1] *******************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [step 2] *******************************************************************************************************************************************************************************************************
skipping: [localhost] => (item=[....] Waiting up to 5 seconds for iOS device to be connected) 
ok: [localhost] => (item=3e194)
ok: [localhost] => (item=6cc84)

TASK [print] ********************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "final_list": [
        "3e194", 
        "6cc84"
    ]
}

PLAY RECAP *

输出:

PYTHONPATH

您可以使用正则表达式过滤器,或者根据需要调整时间条件。