使用Ansible Shell模块的stdin参数的可能的报价问题 [英] Probable quoting issue using the stdin argument of ansible's shell module

查看:42
本文介绍了使用Ansible Shell模块的stdin参数的可能的报价问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一部剧本,其中包含以下任务:

I've got a playbook with the following tasks:

- 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.以下作品:

Now I want to use stale_instance_filter on asgs_result.stdout. The following works:

- 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上完成所有操作,就可以使用.

I get the following error message: parse error: Invalid numeric literal at line 1, column 23 (which I believe is from the account number in the ARN for the ASG.) I think it's a quoting issue (maybe something about the double quotes in the JSON), but I've also tried asgs_result.stdout | quote to no avail. I also tried the command module; it didn't help either. Of course this all works if I do it directly on the CLI.

我意识到我可以组合使用两个jq过滤器,但是我想将asgs_result重用于其他用途,并且不想多次进行查询.如何解决此问题,以便可以使用stdin参数?

I realize I could combine the two jq filters but I want to reuse asgs_result for other things and don't want to have to make the query multiple times. How can I fix this so I can use the stdin argument?

我被要求提供一个asgs_result值的示例,好了,您去了,这里是其中的stdout属性(因为我没有使用其他任何东西):

I was asked to provide an example of the value of asgs_result, well here you go, here's the stdout attribute in it (since I don't use anything else):

"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}"

很抱歉,所有内容都在一行上,但我不想让任何人认为其中有换行符,因为没有换行符.

Sorry that it is all on one line but I don't want to make anyone think there is a newline in there, because there isn't.

推荐答案

JSON内容在发送到stdin之前似乎已经得到了解释,因此看起来像是发送了简单的引号(在-vvv的详细模式下看到):

The JSON content seems to be interpreted before sent to the stdin, so looks like simple quotes are sent (seen in verbose mode with -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过滤器逃脱了很多:

So, you need to "escape" it. Unfortunately, the to_json filter, escape to much:

"stdin": "\"{\\\"AutoScalingGroupARN\\\":\\\"arn:aws:autosca...

但是string过滤器非常适合:

"stdin": "{\"AutoScalingGroupARN\":\"arn:aws:autosca...

因此,stdin的正确方法是这样:

So, the correct way with stdin is this:

- shell: jq -r '{{ stale_instance_filter }}'
  args:
    stdin: "{{ asgs_result.stdout | string }}"

这篇关于使用Ansible Shell模块的stdin参数的可能的报价问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆