Ansible得到错误转换成json



我使用下面的代码来获得lambda标签,并试图将输出转换为json,但得到下面的错误。看起来to_json没有像预期的那样工作。有人能让我知道我做错了什么吗?我需要得到lambda函数的标签

- name: Lambda - Retrieve existing instances
block:
- shell: aws --region {{ AWSRegion }} lambda list-functions | jq -r ".Functions[].FunctionArn" | xargs -I {} aws --region {{ AWSRegion }} lambda list-tags --resource {} --query '{"{}":Tags}'
register: LambdaClustersTagR
environment:
AWS_ACCESS_KEY_ID: "{{ aK }}"
AWS_SECRET_ACCESS_KEY: "{{ SK }}"
- debug:
var: LambdaClustersTagR
- set_fact:
LambdaClustersTag: "{{ LambdaClustersTagR.stdout | from_json }}"

错误信息a如下:

TASK [deployment/Lambda : set_fact] ********************************************
fatal: [localhost]: FAILED! => {"msg": "the field 'args' has an invalid value ({u'LambdaClustersTag': u'{{ LambdaClustersTagR.stdout | from_json }}'}), and could not be converted to an dict.The error was: Extra data: line 9 column 1 - line 33 column 2 (char 397 - 1491)nnThe error appears to be in '/var/lib/deployment/Lambda/tasks/main.yml': line 14, column 5, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nn      var: LambdaClustersTagRn  - set_fact:n    ^ heren"}

请发现下面shell命令执行的结果,我需要将下面的响应转换成json

TASK [deployment/Lambda : debug] ***********************************************
ok: [localhost] => {
"LambdaClustersTagR": {
"changed": true, 
"cmd": "aws --region us-east-2 lambda list-functions | jq -r ".Functions[].FunctionArn" | xargs -I {} aws --region us-east-2 lambda list-tags --resource {} --query '{"{}":Tags}'", 
"delta": "0:00:04.791855", 
"end": "2021-08-22 18:17:18.046849", 
"failed": false, 
"rc": 0, 
"start": "2021-08-22 18:17:13.254994", 
"stderr": "", 
"stderr_lines": [], 
"stdout": "{n    "arn:aws:lambda:us-east-2:12345:function:sam-app-HelloWorldFunction-RVdw5VaehZNq": {n        "aws:cloudformation:stack-id": "arn:aws:cloudformation:us-east-2:12345:stack/sam-app/366f4e20-ba7b-11eb-8e29-06942b403bc4", n        "lambda:createdBy": "SAM", n        "aws:cloudformation:stack-name": "sam-app", n        "aws:cloudformation:logical-id": "HelloWorldFunction"n    }n}n{n    "arn:aws:lambda:us-east-2:12345:function:pouConfigFunction_te": {n        "applications": "pr2", n        "env": "te"n    }n}n{n    "arn:aws:lambda:us-east-2:12345:function:PaymentBatchTopic": {}n}n{n    "arn:aws:lambda:us-east-2:12345:function:te-pou-config2-pouConfigFunction-XoXhZK1OCL1z": {n        "aws:cloudformation:stack-id": "arn:aws:cloudformation:us-east-2:12345:stack/te-pou-config2/347ee360-f12f-11eb-b87c-0aee4f45e49a", n        "lambda:createdBy": "SAM", n        "aws:cloudformation:stack-name": "te-pou-config2", n        "aws:cloudformation:logical-id": "pouConfigFunction"n    }n}n{n    "arn:aws:lambda:us-east-2:12345:function:te-pou-config-test-pouConfigTestFunction-DM2B0dZF0JuW": {n        "aws:cloudformation:stack-id": "arn:aws:cloudformation:us-east-2:12345:stack/te-pou-config-test/b6ac3610-f127-11eb-9513-0a79740bc2e0", n        "lambda:createdBy": "SAM", n        "aws:cloudformation:stack-name": "te-pou-config-test", n        "aws:cloudformation:logical-id": "pouConfigTestFunction"n    }n}", 
"stdout_lines": [
"{", 
"    "arn:aws:lambda:us-east-2:12345:function:sam-app-HelloWorldFunction-RVdw5VaehZNq": {", 
"        "aws:cloudformation:stack-id": "arn:aws:cloudformation:us-east-2:12345:stack/sam-app/366f4e20-ba7b-11eb-8e29-06942b403bc4", ", 
"        "lambda:createdBy": "SAM", ", 
"        "aws:cloudformation:stack-name": "sam-app", ", 
"        "aws:cloudformation:logical-id": "HelloWorldFunction"", 
"    }", 
"}", 
"{", 
"    "arn:aws:lambda:us-east-2:12345:function:pouConfigFunction_te": {", 
"        "applications": "pr2", ", 
"        "env": "te"", 
"    }", 
"}", 
"{", 
"    "arn:aws:lambda:us-east-2:12345:function:PaymentBatchTopic": {}", 
"}", 
"{", 
"    "arn:aws:lambda:us-east-2:12345:function:te-pou-config2-pouConfigFunction-XoXhZK1OCL1z": {", 
"        "aws:cloudformation:stack-id": "arn:aws:cloudformation:us-east-2:12345:stack/te-pou-config2/347ee360-f12f-11eb-b87c-0aee4f45e49a", ", 
"        "lambda:createdBy": "SAM", ", 
"        "aws:cloudformation:stack-name": "te-pou-config2", ", 
"        "aws:cloudformation:logical-id": "pouConfigFunction"", 
"    }", 
"}", 
"{", 
"    "arn:aws:lambda:us-east-2:12345:function:te-pou-config-test-pouConfigTestFunction-DM2B0dZF0JuW": {", 
"        "aws:cloudformation:stack-id": "arn:aws:cloudformation:us-east-2:12345:stack/te-pou-config-test/b6ac3610-f127-11eb-9513-0a79740bc2e0", ", 
"        "lambda:createdBy": "SAM", ", 
"        "aws:cloudformation:stack-name": "te-pou-config-test", ", 
"        "aws:cloudformation:logical-id": "pouConfigTestFunction"", 
"    }", 
"}"
]
}
}

您的输出包含几个顶级json映射一个接一个,例如

{
"a_var": "value",
"other_var": [1,3,4]
}
{
"some_other_var": "toto"
}
{
"yet_an_other_var": "titi"
}

这使得from_json过滤器有点疯狂,我不得不承认错误信息真的很不清楚(我自己花了相当多的时间找到这个问题)。

你必须要么自己加载每个顶层json映射,要么生成一个包含所有变量的顶层映射,你可以一次加载,即

{
"a_var": "value",
"other_var": [1,3,4],
"some_other_var": "toto",
"yet_an_other_var": "titi"
}

相关内容

  • 没有找到相关文章

最新更新