使用Ansible&;的外壳模块的stdin参数可能会出现引用问题 [英] Probable quoting issue using the stdin argument of ansible's shell module
本文介绍了使用Ansible&;的外壳模块的stdin参数可能会出现引用问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一份行动手册,其中包含以下任务:
- set_fact:
asg_filter: >
.AutoScalingGroups[] |
select(.Tags[] | select(.Key == "Role").Value == "myrole")
- shell: aws autoscaling --region us-west-2 describe-auto-scaling-groups | jq --compact-output "{{ asg_filter }}"
register: asgs_result
- set_fact:
stale_instance_filter: >
.LaunchConfigurationName as $lc |
.Instances[] |
select(.LaunchConfigurationName != $lc) |
.InstanceId
现在我想在asgs_result.stdout
上使用stale_instance_filter
。以下功能有效:
- shell: echo '{{ asgs_result.stdout }}' | jq -r '{{ stale_instance_filter }}'
但这不是:
- shell: jq -r '{{ stale_instance_filter }}'
args:
stdin: "{{ asgs_result.stdout }}"
我收到以下错误消息:parse error: Invalid numeric literal at line 1, column 23
(我认为它来自ASG的ARN中的帐号。)我认为这是一个引号问题(可能是关于JSON中的双引号),但我也尝试了asgs_result.stdout | quote
,但都没有用。我还尝试了command
模块;它也没有帮助。当然,如果我直接在CLI上执行此操作,则所有这些操作都有效。
我意识到我可以组合这两个jq
筛选器,但我想重复使用asgs_result
来做其他事情,并且不想多次查询。如何修复此问题才能使用stdin
参数?
编辑:我被要求提供ASGS_RESULT的值的一个示例,好吧,这就是它中的stdout
属性(因为我没有使用任何其他属性):
"stdout": "{"AutoScalingGroupARN":"arn:aws:autoscaling:us-east-2:123456:autoScalingGroup:e75a213b-75fe-467c-8cf5-d7c51f76c471:autoScalingGroupName/myrole-dev","TargetGroupARNs":[],"SuspendedProcesses":[],"DesiredCapacity":4,"Tags":[{"ResourceType":"auto-scaling-group","ResourceId":"myrole-dev","PropagateAtLaunch":true,"Value":"dev","Key":"Dimension"},{"ResourceType":"auto-scaling-group","ResouJceId":"myrole-dev","PropagateAtLaunch":true,"Value":"true","Key":"Monitored"},{"ResourceType":"auto-scaling-group","ResourceId":"myrole-dev","PropagateAtLaunch":true,"Value":"myrole","Key":"Name"},{"ResourceType":"auto-scaling-group","ResourceId":"myrole-dev","PropagateAtLaunch":true,"Value":"myrole","Key":"Role"},{"ResourceType":"auto-scaling-group","ResourceId":"myrole-dev","PropagateAtLaunch":true,"Value":"2035-09-30 18:55:31 +0000","Key":"cleaner-destroy-after"},{"ResourceType":"auto-scaling-group","ResourceId":"myrole-dev","PropagateAtLaunch":true,"Value":"vpce-2c23ca45","Key":"force_s3_endpoint_dependency"},{"ResourceType":"auto-scaling-group","ResourceId":"myrole-dev","PropagateAtLaunch":true,"Value":"owned","Key":"kubernetes.io/cluster/dev"}],"EnabledMetrics":[],"LoadBalancerNames":[],"AutoScalingGroupName":"myrole-dev","DefaultCooldown":300,"MinSize":4,"Instances":[{"ProtectedFromScaleIn":false,"AvailabilityZone":"us-east-2b","InstanceId":"i-0141fd35e3cf3ad0a","HealthStatus":"Healthy","LifecycleState":"InService","LaunchConfigurationName":"dev_myrole_20180511171410107500000002"},{"ProtectedFromScaleIn":false,"AvailabilityZone":"us-east-2c","InstanceId":"i-01aec2b3546d75190","HealthStatus":"Healthy","LifecycleState":"InService","LaunchConfigurationName":"dev_myrole_20180511171410107500000002"},{"ProtectedFromScaleIn":false,"AvailabilityZone":"us-east-2a","InstanceId":"i-0830b227f034d2859","HealthStatus":"Healthy","LifecycleState":"InService","LaunchConfigurationName":"dev_myrole_20180511171410107500000002"},{"ProtectedFromScaleIn":false,"AvailabilityZone":"us-east-2b","InstanceId":"i-0f7d847e8c168040b","HealthStatus":"Healthy","LifecycleState":"InService","LaunchConfigurationName":"dev_myrole_20180511171410107500000002"}],"MaxSize":4,"VPCZoneIdentifier":"subnet-c348988e,subnet-79743210,subnet-156ee36e","HealthCheckGracePeriod":300,"TerminationPolicies":["Default"],"LaunchConfigurationName":"dev_myrole_20180511171410107500000002","CreatedTime":"2018-02-20T22:35:32.183Z","AvailabilityZones":["us-east-2a","us-east-2b","us-east-2c"],"HealthCheckType":"EC2","NewInstancesProtectedFromScaleIn":false}"
很抱歉,所有内容都在一行上,但我不想让任何人认为其中有换行符,因为实际上没有。
推荐答案
json内容似乎是在发送到标准输入之前被解释的,所以看起来像是发送了简单的引号(在详细模式下,-vvv
):
"stdin": "{'AutoScalingGroupARN': 'arn:aws:autoscaling:us-east-2:123456:autoScalin
gGroup:e75a213b-75fe-467c-8cf5-d7c51f76c471:autoScalingGroupName/myrole-dev', ...,
'AvailabilityZones': ['us-east-2a', 'us-east-2b', 'us-east-2c']}"
哪个JSON无效:
$ echo "{'AutoScalingGroupARN': 'arn:aws:autoscaling:us-east-2:123456:autoScalingGroup:e75a213b-75fe-467c-8cf5-d7c51f76c471:autoScalingGroupName/myrole-dev', 'HealthCheckGracePeriod': 300}" | jq
parse error: Invalid numeric literal at line 1, column 23
$ echo '{"AutoScalingGroupARN": "arn:aws:autoscaling:us-east-2:123456:autoScalingGroup:e75a213b-75fe-467c-8cf5-d7c51f76c471:autoScalingGroupName/myrole-dev", "HealthCheckGracePeriod": 300}' | jq
{
"AutoScalingGroupARN": "arn:aws:autoscaling:us-east-2:123456:autoScalingGroup:e75a213b-75fe-467c-8cf5-d7c51f76c471:autoScalingGroupName/myrole-dev",
"HealthCheckGracePeriod": 300
}
所以,您需要"逃离"它。
不幸的是,to_json
筛选器有很多转义:
"stdin": ""{\"AutoScalingGroupARN\":\"arn:aws:autosca...
但string
筛选器非常适合:
"stdin": "{"AutoScalingGroupARN":"arn:aws:autosca...
所以,stdin
的正确方式是:
- shell: jq -r '{{ stale_instance_filter }}'
args:
stdin: "{{ asgs_result.stdout | string }}"
这篇关于使用Ansible&;的外壳模块的stdin参数可能会出现引用问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文