重写 yaml 变量(ansible)
Rewriting the yaml variable (ansible)
我有一个变量:
"result":[
{
"item": "192.168.0.1",
"stdout_lines": [
"a",
"b",
"c",
"d",
"e",
]
},
{
"item": "192.168.0.2",
"stdout_lines": [
"aa",
"bb",
"cc",
]
}
]
我想获取以下变量:
"result":[
{
"item": [
"192.168.0.1",
"192.168.0.1",
"192.168.0.1",
"192.168.0.1",
"192.168.0.1",
]
"stdout_lines": [
"a",
"b",
"c",
"d",
"e",
]
},
{
"item": [
"192.168.0.2",
"192.168.0.2",
"192.168.0.2",
]
"stdout_lines": [
"aa",
"bb",
"cc",
]
}
]
也就是说,我需要“item”的值与“stdout_lines”中的值重复多次。 “stdout_lines”的值总是变化的,“stdout_lines”可以有不同数量的值。
我该怎么做?
例如
- set_fact:
_tmp: "{{ _tmp|d([]) + [item|combine({'item': _items.split()})] }}"
loop: "{{ result }}"
vars:
_count: "{{ item.stdout_lines|length }}"
_item: "{{ item.item }} "
_items: "{{ _item * _count|int }}"
- set_fact:
result: "{{ _tmp }}"
给出了预期的结果
result:
- item:
- 192.168.0.1
- 192.168.0.1
- 192.168.0.1
- 192.168.0.1
- 192.168.0.1
stdout_lines:
- a
- b
- c
- d
- e
- item:
- 192.168.0.2
- 192.168.0.2
- 192.168.0.2
stdout_lines:
- aa
- bb
- cc
我有一个变量:
"result":[
{
"item": "192.168.0.1",
"stdout_lines": [
"a",
"b",
"c",
"d",
"e",
]
},
{
"item": "192.168.0.2",
"stdout_lines": [
"aa",
"bb",
"cc",
]
}
]
我想获取以下变量:
"result":[
{
"item": [
"192.168.0.1",
"192.168.0.1",
"192.168.0.1",
"192.168.0.1",
"192.168.0.1",
]
"stdout_lines": [
"a",
"b",
"c",
"d",
"e",
]
},
{
"item": [
"192.168.0.2",
"192.168.0.2",
"192.168.0.2",
]
"stdout_lines": [
"aa",
"bb",
"cc",
]
}
]
也就是说,我需要“item”的值与“stdout_lines”中的值重复多次。 “stdout_lines”的值总是变化的,“stdout_lines”可以有不同数量的值。
我该怎么做?
例如
- set_fact:
_tmp: "{{ _tmp|d([]) + [item|combine({'item': _items.split()})] }}"
loop: "{{ result }}"
vars:
_count: "{{ item.stdout_lines|length }}"
_item: "{{ item.item }} "
_items: "{{ _item * _count|int }}"
- set_fact:
result: "{{ _tmp }}"
给出了预期的结果
result:
- item:
- 192.168.0.1
- 192.168.0.1
- 192.168.0.1
- 192.168.0.1
- 192.168.0.1
stdout_lines:
- a
- b
- c
- d
- e
- item:
- 192.168.0.2
- 192.168.0.2
- 192.168.0.2
stdout_lines:
- aa
- bb
- cc