与with_item一起使用的ansible表达式

时间:2017-08-08 14:43:01

标签: ansible

我有一个变量,更具体地说,它是使用with_item的set_fact任务的结果(使用寄存器:)注册的变量,让我们将其命名为myvar,简单来说,我只显示相关部分。

{
    "msg": "All items completed",
    "results": [
        {
            "item": "item1",
            "stdout_lines": [
                [
                    "result line 1",
                    "result line 2"
                ]
            ]
        },
        {
            "item": "item2",
            "stdout_lines": [
                [
                    "result line 1"
                ]
            ]
        }
    ]
}

我需要为每个"结果行X"运行一个任务。这将在命令中包含" item"的值。和"结果行X"值 我不知道该怎么做。

如果我这样做

- debug:
    msg: "{{ item.item }} , {{ item | json_query('stdout_lines[0][*]') | list }}"
  with_items: "{{ myvar | json_query('results[*]') }}"

以下将输出

item1 ; [u'result line 1',u'result line 2']
item2 ; [u'result line 1']

我在同一行都有两个stdout_lines条目。所以stdout_lines的拆分应该在with_item表达式

中进行

但在这里,我不知道如何能够"取得"所有结果的所有stdout_lines,同时保留"项目"值。

1 个答案:

答案 0 :(得分:1)

如果您有stdout_lineswith_subelements可以--- - hosts: localhost gather_facts: no vars: myvar: "results": [ { "item": "item1", "stdout_lines": [ [ "result line 1", "result line 2" ] ] }, { "item": "item2", "stdout_lines": [ [ "result line 1" ] ] } ] tasks: - command: echo {{ item.0.name }} {{ item.1 }} with_subelements: - "{{ myvar.results | json_query('[].{name:item,lines:stdout_lines[]}') }}" - lines

在你的情况下,你需要用JMESPath加点它:

   {
     path: '',
     redirectTo: '/home',
     pathMatch: 'full'
   },