可能使用ansible的shell模块的stdin参数引用问题

时间:2018-05-01 00:19:27

标签: ansible jq aws-cli ansible-2.x

我有一本有以下任务的剧本:

- 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

现在我想在stale_instance_filter上使用asgs_result.stdout。以下作品:

- 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(我相信它来自ASN的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}"

很抱歉这一切都在一条线上,但我不想让任何人认为那里有换行符,因为没有。

1 个答案:

答案 0 :(得分:1)

JSON内容似乎在发送到stdin之前被解释,因此看起来像是发送了简单的引号(在详细模式中看到-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 }}"