首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从不可否认的事实中删除双引号

问从不可否认的事实中删除双引号
EN

Stack Overflow用户
提问于 2021-01-31 05:33:04
回答 1查看 717关注 0票数 1

有一个json输出,我正试图解析它。我将输出注册到名为instance_ip的变量中。

以下是json输出:

代码语言:javascript
复制
{
    "msg": {
        "instances": [
            {
                "root_device_type": "ebs",
                "private_dns_name": "",
                "cpu_options": {
                    "core_count": 2,
                    "threads_per_core": 1
                },
                "security_groups": [],
                "state_reason": {
                    "message": "Client.UserInitiatedShutdown: User initiated shutdown",
                    "code": "Client.UserInitiatedShutdown"
                },
                "monitoring": {
                    "state": "disabled"
                },
                "ebs_optimized": false,
                "state": {
                    "code": 48,
                    "name": "terminated"
                },
                "client_token": "test-Logst-14O6L4IETB05E",
                "virtualization_type": "hvm",
                "architecture": "x86_64",
                "tags": {
                    "sg:environment": "TST",
                    "Name": "logstash1",
                    "aws:cloudformation:logical-id": "Logstash1A1594E87",
                    "sg:owner": "Platforms@paparapa.com",              
                    "aws:cloudformation:stack-name": "test-three-ec2-instances-elk-demo",
                    "elastic_role": "logstash",
                    "sg:function": "Storage"
                },
                "key_name": "AWS_key",
                "image_id": "ami-09f765d333a8ebb4b",
                "state_transition_reason": "User initiated (2021-01-31 09:46:23 GMT)",
                "hibernation_options": {
                    "configured": false
                },
                "capacity_reservation_specification": {
                    "capacity_reservation_preference": "open"
                },
                "public_dns_name": "",
                "block_device_mappings": [],
                "metadata_options": {
                    "http_endpoint": "enabled",
                    "state": "pending",
                    "http_tokens": "optional",
                    "http_put_response_hop_limit": 1
                },
                "placement": {
                    "group_name": "",
                    "tenancy": "default",
                    "availability_zone": "ap-southeast-2a"
                },
                "enclave_options": {
                    "enabled": false
                },
                "ami_launch_index": 0,
                "ena_support": true,
                "network_interfaces": [],
                "launch_time": "2021-01-31T09:44:51+00:00",
                "instance_id": "i-0fa5dbb869833d7c6",
                "instance_type": "t2.medium",
                "root_device_name": "/dev/xvda",
                "hypervisor": "xen",
                "product_codes": []
            },
            {
                "root_device_type": "ebs",
                "private_dns_name": "ip-10-x-x-x.ap-southeast-2.compute.internal",
                "cpu_options": {
                    "core_count": 2,
                    "threads_per_core": 1
                },
                "source_dest_check": true,
                "monitoring": {
                    "state": "disabled"
                },
                "subnet_id": "subnet-0d5f856afab8f0eec",
                "ebs_optimized": false,
                "iam_instance_profile": {
                    "id": "AIPARWXXVHXJWC2FL4AI6",
                    "arn": "arn:aws:iam::instance-profile/test-three-ec2-instances-elk-demo-Logstash1InstanceProfileC3035819-1F2LI7JM16FVM"
                },
                "state": {
                    "code": 16,
                    "name": "running"
                },
                "security_groups": [
                    {
                        "group_id": "sg-0e5dffa834a036fab",
                        "group_name": "Ansible_sec_group"
                    }
                ],
                "client_token": "test-Logst-8UF6RX33BH06",
                "virtualization_type": "hvm",
                "architecture": "x86_64",
                "public_ip_address": "3.x.x.x",
                "tags": {
                    "Name": "logstash1",
                    "aws:cloudformation:logical-id": "Logstash1A1594E87",
                    "srg:environment": "TST",
                    "aws:cloudformation:stack-id": "arn:aws:cloudformation:ap-southeast-2:117557247443:stack/test-three-ec2-instances-elk-demo/ca8ef2b0-63ad-11eb-805f-02630ffccc8c",
                    "sg:function": "Storage",                    
                    "aws:cloudformation:stack-name": "test-three-ec2-instances-elk-demo",
                    "elastic_role": "logstash",
                    "sg:owner": "Platforms@paparapa.com"
                },
                "key_name": "AWS_SRG_key",
                "image_id": "ami-09f765d333a8ebb4b",
                "ena_support": true,
                "hibernation_options": {
                    "configured": false
                },
                "capacity_reservation_specification": {
                    "capacity_reservation_preference": "open"
                },
                "public_dns_name": "ec2-3-x-x-x.ap-southeast-2.compute.amazonaws.com",
                "block_device_mappings": [
                    {
                        "device_name": "/dev/xvda",
                        "ebs": {
                            "status": "attached",
                            "delete_on_termination": true,
                            "attach_time": "2021-01-31T10:22:21+00:00",
                            "volume_id": "vol-058662934ffba3a68"
                        }
                    }
                ],
                "metadata_options": {
                    "http_endpoint": "enabled",
                    "state": "applied",
                    "http_tokens": "optional",
                    "http_put_response_hop_limit": 1
                },
                "placement": {
                    "group_name": "",
                    "tenancy": "default",
                    "availability_zone": "ap-southeast-2a"
                },
                "enclave_options": {
                    "enabled": false
                },
                "ami_launch_index": 0,
                "hypervisor": "xen",
                "network_interfaces": [
                    {
                        "status": "in-use",
                        "description": "",
                        "subnet_id": "subnet-0d5f856afab8f0eec",
                        "source_dest_check": true,
                        "interface_type": "interface",
                        "ipv6_addresses": [],
                        "network_interface_id": "eni-09b045668ac59990c",
                        "private_dns_name": "ip-10-x-x-x.ap-southeast-2.compute.internal",
                        "attachment": {
                            "status": "attached",
                            "device_index": 0,
                            "attachment_id": "eni-attach-0700cd11dfb27e2dc",
                            "delete_on_termination": true,
                            "attach_time": "2021-01-31T10:22:20+00:00"
                        },
                        "private_ip_addresses": [
                            {
                                "private_ip_address": "10.x.x.x",
                                "private_dns_name": "ip-10-x-x-x.ap-southeast-2.compute.internal",
                                "association": {
                                    "public_ip": "3.x.x.x",
                                    "public_dns_name": "ec2-3-x-x-x.ap-southeast-2.compute.amazonaws.com",
                                    "ip_owner_id": "amazon"
                                },
                                "primary": true
                            }
                        ],
                        "mac_address": "02:d1:13:01:59:b2",
                        "private_ip_address": "10.x.x.x",
                        "vpc_id": "vpc-0016dcdf5abe4fef0",
                        "groups": [
                            {
                                "group_id": "sg-0e5dffa834a036fab",
                                "group_name": "Ansible_sec_group"
                            }
                        ],
                        "association": {
                            "public_ip": "3.x.x.x",
                            "public_dns_name": "ec2-3-x-x-x.ap-southeast-2.compute.amazonaws.com",
                            "ip_owner_id": "amazon"
                        },
                        "owner_id": "117557247443"
                    }
                ],
                "launch_time": "2021-01-31T10:22:20+00:00",
                "instance_id": "i-0482bb8ca1bef6006",
                "instance_type": "t2.medium",
                "root_device_name": "/dev/xvda",
                "state_transition_reason": "",
                "private_ip_address": "10.x.x.x",
                "vpc_id": "vpc-0016dcdf5abe4fef0",
                "product_codes": []
            }
        ],
        "failed": false,
        "changed": false
    },
    "_ansible_verbose_always": true,
    "_ansible_no_log": false,
    "changed": false
}

目标是获取私有ip地址并附加端口号。

通过下面的任务,我得到了具有节点ip地址["10.x.x.x"]的列表

代码语言:javascript
复制
  - name: Getting EC2 instance ip address
    set_fact:
      instance_ip: "{{ logstash_instance | json_query('instances[*].network_interfaces[*].private_ip_address') | flatten }}"

在剧中的下一个任务中,我试图附加端口号,但我一直在得到

"'10.x.x.x:5044'“

代码语言:javascript
复制
  - name: Get everything between quotes and append port 5044
    set_fact:
      logstash_hosts: "{{ instance_ip | map('regex_replace', '^(.*)$', '\\1:5044') | list }}"

下面是模板输出:

代码语言:javascript
复制
# ------------------------------ Logstash Output -------------------------------
output.logstash:
  hosts: "['10.x.x.x:5044']"

我需要去掉双引号,并将干净的变量'10.x.x.x:5044‘传递给我的模板文件。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-31 13:04:24

您可以尝试创建一个新的list变量,并将端口号附加到每个元素,使用以下方法:

代码语言:javascript
复制
  - set_fact:
      logstash_hosts: "{{ logstash_hosts|default([]) + [ item ~ ':5044' ] }}"
    with_items: "{{ instance_ip }}"

然后在模板中:

代码语言:javascript
复制
output.logstash:
  hosts: {{ logstash_hosts|to_yaml }}

另外,由于Logstash配置是YAML格式的文件,所以使用YAML列表语法并直接使用instance_ip变量(并避免使用set_fact)。然后模板将如下所示:

代码语言:javascript
复制
output.logstash:
  hosts:
{% for ip in instance_ip %}
  - {{ ip }}:5044
{% endfor %}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65976069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档