我有一个在2个或更多url上运行的Ansible作业。每个url返回具有不同值的相同变量(但具有相似的模式)。下面是作业的返回数据:
"message": [
{
"url": "http://0.0.0.1:xxx1",
"content": [
{
"message": "This is message number 1",
"message2": "This is message2 number 1"
},
{
"message": "This is message number 2",
"message2": "This is message2 number 2"
},
{
"message": "This is message number 3",
"message2": "This is message2 number 3"
}
]
},
{
"url": "http://0.0.0.2:xxx2",
"content": [
{
"message": "This is message number 1",
"message2": "This is message2 number 1"
},
{
"message": "This is message number 2",
"message2": "This is message2 number 2"
},
{
"message": "This is message number 3",
"message2": "This is message2 number 3"
}
]
}
我想解析变量'message'而不改变返回数据的结构。我期望的结果是这样的:
"message": [
{
"url": "http://0.0.0.1:xxx1",
"content": [
{
"message": "message number 1",
"message2": "This is message2 number 1"
},
{
"message": "message number 2",
"message2": "This is message2 number 2"
},
{
"message": "message number 3",
"message2": "This is message2 number 3"
}
]
},
{
"url": "http://0.0.0.2:xxx2",
"content": [
{
"message": "message number 1",
"message2": "This is message2 number 1"
},
{
"message": "message number 2",
"message2": "This is message2 number 2"
},
{
"message": "message number 3",
"message2": "This is message2 number 3"
}
]
}
我知道如何解析数据。我努力保持结构不变。我怎样才能实现我想要的?
转换Jinja中的结构
updates: |
{% for i in message %}
- content:
{% for m in i.content %}
{% set arr=m.message.split() %}
{% set message={'message': arr[2:]|join(' ')} %}
- {{ m|combine(message) }}
{% endfor %}
{% for k in i %}
{% if k != 'content' %}
{{ k }}: {{ i[k] }}
{% endif %}
{% endfor %}
{% endfor %}
为
updates: |-
- content:
- {'message': 'message number 1', 'message2': 'This is message2 number 1'}
- {'message': 'message number 2', 'message2': 'This is message2 number 2'}
- {'message': 'message number 3', 'message2': 'This is message2 number 3'}
url: http://0.0.0.1:xxx1
- content:
- {'message': 'message number 1', 'message2': 'This is message2 number 1'}
- {'message': 'message number 2', 'message2': 'This is message2 number 2'}
- {'message': 'message number 3', 'message2': 'This is message2 number 3'}
url: http://0.0.0.2:xxx2
将块转换为YAML
messag2: "{{ updates|from_yaml }}"
为
messag2:
- content:
- message: message number 1
message2: This is message2 number 1
- message: message number 2
message2: This is message2 number 2
- message: message number 3
message2: This is message2 number 3
url: http://0.0.0.1:xxx1
- content:
- message: message number 1
message2: This is message2 number 1
- message: message number 2
message2: This is message2 number 2
- message: message number 3
message2: This is message2 number 3
url: http://0.0.0.2:xxx2
用于测试的完整剧本示例
- hosts: localhost
vars:
message:
- content:
- message: This is message number 1
message2: This is message2 number 1
- message: This is message number 2
message2: This is message2 number 2
- message: This is message number 3
message2: This is message2 number 3
url: http://0.0.0.1:xxx1
- content:
- message: This is message number 1
message2: This is message2 number 1
- message: This is message number 2
message2: This is message2 number 2
- message: This is message number 3
message2: This is message2 number 3
url: http://0.0.0.2:xxx2
updates: |
{% for i in message %}
- content:
{% for m in i.content %}
{% set arr=m.message.split() %}
{% set message={'message': arr[2:]|join(' ')} %}
- {{ m|combine(message) }}
{% endfor %}
{% for k in i %}
{% if k != 'content' %}
{{ k }}: {{ i[k] }}
{% endif %}
{% endfor %}
{% endfor %}
messag2: "{{ updates|from_yaml }}"
tasks:
- debug:
var: updates
- debug:
var: messag2
Q:"此解决方案适用于变量。在任务中使用它,因为我的数据来自json输出,set_fact在结果中给了我一个违规行。">
A:没有理由将updates和messag2变量移动到set_fact任务中。a)当消息来自任务
时,下面的播放也可以正常工作- hosts: localhost
vars:
updates: |
{% for i in message %}
- content:
{% for m in i.content %}
{% set arr=m.message.split() %}
{% set message={'message': arr[2:]|join(' ')} %}
- {{ m|combine(message) }}
{% endfor %}
{% for k in i %}
{% if k != 'content' %}
{{ k }}: {{ i[k] }}
{% endif %}
{% endfor %}
{% endfor %}
messag2: "{{ updates|from_yaml }}"
tasks:
- include_vars:
file: message.yml
- debug:
var: updates
- debug:
var: messag2
b)您可以将updates和messag2的声明放入set_fact任务中。下面的戏演得很好
- hosts: localhost
tasks:
- include_vars:
file: message.yml
- set_fact:
updates: |
{% for i in message %}
- content:
{% for m in i.content %}
{% set arr=m.message.split() %}
{% set message={'message': arr[2:]|join(' ')} %}
- {{ m|combine(message) }}
{% endfor %}
{% for k in i %}
{% if k != 'content' %}
{{ k }}: {{ i[k] }}
{% endif %}
{% endfor %}
{% endfor %}
- set_fact:
messag2: "{{ updates|from_yaml }}"
- debug:
var: updates
- debug:
var: messag2
c)你不能同时声明updates和messag2放到单个set_fact任务中,因为messag2不知道updates。下面的戏
- hosts: localhost
tasks:
- include_vars:
file: message.yml
- set_fact:
updates: |
{% for i in message %}
- content:
{% for m in i.content %}
{% set arr=m.message.split() %}
{% set message={'message': arr[2:]|join(' ')} %}
- {{ m|combine(message) }}
{% endfor %}
{% for k in i %}
{% if k != 'content' %}
{{ k }}: {{ i[k] }}
{% endif %}
{% endfor %}
{% endfor %}
messag2: "{{ updates|from_yaml }}"
- debug:
var: updates
- debug:
var: messag2
失败
…错误是:'updates' is undefined .