从 Ansible 任务输出中提取值并从中创建一个变量

Extract value from Ansible task output and create a variable from it

我使用 elb_application_lb_info 模块来获取有关我的应用程序负载均衡器的信息。这是我为此使用的代码:

- name: Test playbook
  hosts: tag_elastic_role_logstash
  vars:
    aws_access_key: AKIARWXXVHXJS5BOIQ6P
    aws_secret_key: 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
  tasks:
    - name: Gather information about all ELBs
      elb_application_lb_info:
        aws_access_key: AKIXXXXXXXXXXXXXXXXXXX
        aws_secret_key: gG6a586XXXXXXXXXXXXXXXXXX
        region: ap-southeast-2
        names:
          - LoadBalancer
      register: albinfo
    - debug:
        msg: "{{ albinfo }}"

这工作正常,我得到以下输出:

 "load_balancers": [
            {
                "idle_timeout_timeout_seconds": "60",
                "routing_http2_enabled": "true",
                "created_time": "2021-01-26T23:58:27.890000+00:00",
                "access_logs_s3_prefix": "",
                "security_groups": [
                    "sg-094c894246db1bd92"
                ],
                "waf_fail_open_enabled": "false",
                "availability_zones": [
                    {
                        "subnet_id": "subnet-0195c9c0df024d221",
                        "zone_name": "ap-southeast-2b",
                        "load_balancer_addresses": []
                    },
                    {
                        "subnet_id": "subnet-071060fde585476e0",
                        "zone_name": "ap-southeast-2c",
                        "load_balancer_addresses": []
                    },
                    {
                        "subnet_id": "subnet-0d5f856afab8f0eec",
                        "zone_name": "ap-southeast-2a",
                        "load_balancer_addresses": []
                    }
                ],
                "access_logs_s3_bucket": "",
                "deletion_protection_enabled": "false",
                "load_balancer_name": "LoadBalancer",
                "state": {
                    "code": "active"
                },
                "scheme": "internet-facing",
                "type": "application",
                "load_balancer_arn": "arn:aws:elasticloadbalancing:ap-southeast-2:117557247443:loadbalancer/app/LoadBalancer/27cfc970d48501fd",
                "access_logs_s3_enabled": "false",
                "tags": {
                    "Name": "loadbalancer_test",
                    "srg:function": "Storage",
                    "srg:owner": "ISCloudPlatforms@superretailgroup.com",
                    "srg:cost-centre": "G110",
                    "srg:managed-by": "ISCloudPlatforms@superretailgroup.com",
                    "srg:environment": "TST"
                },
                "routing_http_desync_mitigation_mode": "defensive",
                "canonical_hosted_zone_id": "Z1GM3OXH4ZPM65",
                "dns_name": "LoadBalancer-203283612.ap-southeast-2.elb.amazonaws.com",
                "ip_address_type": "ipv4",
                "listeners": [
                    {
                        "default_actions": [
                            {
                                "target_group_arn": "arn:aws:elasticloadbalancing:ap-southeast-2:117557247443:targetgroup/test-ALBID-W04X8DBT450Q/c999ac1cda7b1d4a",
                                "type": "forward",
                                "forward_config": {
                                    "target_group_stickiness_config": {
                                        "enabled": false
                                    },
                                    "target_groups": [
                                        {
                                            "target_group_arn": "arn:aws:elasticloadbalancing:ap-southeast-2:117557247443:targetgroup/test-ALBID-W04X8DBT450Q/c999ac1cda7b1d4a",
                                            "weight": 1
                                        }
                                    ]
                                }
                            }
                        ],
                        "protocol": "HTTP",
                        "rules": [
                            {
                                "priority": "default",
                                "is_default": true,
                                "rule_arn": "arn:aws:elasticloadbalancing:ap-southeast-2:117557247443:listener-rule/app/LoadBalancer/27cfc970d48501fd/671ad3428c35c834/5b5953a49a886c03",
                                "conditions": [],
                                "actions": [
                                    {
                                        "target_group_arn": "arn:aws:elasticloadbalancing:ap-southeast-2:117557247443:targetgroup/test-ALBID-W04X8DBT450Q/c999ac1cda7b1d4a",
                                        "type": "forward",
                                        "forward_config": {
                                            "target_group_stickiness_config": {
                                                "enabled": false
                                            },
                                            "target_groups": [
                                                {
                                                    "target_group_arn": "arn:aws:elasticloadbalancing:ap-southeast-2:117557247443:targetgroup/test-ALBID-W04X8DBT450Q/c999ac1cda7b1d4a",
                                                    "weight": 1
                                                }
                                            ]
                                        }
                                    }
                                ]
                            }
                        ],
                        "listener_arn": "arn:aws:elasticloadbalancing:ap-southeast-2:117557247443:listener/app/LoadBalancer/27cfc970d48501fd/671ad3428c35c834",
                        "load_balancer_arn": "arn:aws:elasticloadbalancing:ap-southeast-2:117557247443:loadbalancer/app/LoadBalancer/27cfc970d48501fd",
                        "port": 9200
                    }
                ],
                "vpc_id": "vpc-0016dcdf5abe4fef0",
                "routing_http_drop_invalid_header_fields_enabled": "false"
            }
        ]

我需要获取负载均衡器的 dns 名称“dns_name”,并将其作为变量传递到另一个游戏中。

我尝试使用 json_query 但出现错误。这是代码:

    - name: save the Json data to a Variable as a Fact
      set_fact:
        jsondata: "{{ albinfo.stdout | from_json }}"

    - name: Get ALB dns name
      set_fact:
        dns_name: "{{ jsondata | json_query(jmesquery) }}"
      vars:
        jmesquery: 'load_balancers.dns_name'

    - debug:
        msg: "{{ dns_name }}"

这里是错误:

"msg": "The task includes an option with an undefined variable. The error was: Unable to look up a name or access an attribute in template string ({{ albinfo.stdout | from_json }}).\nMake sure your variable name does not contain invalid characters like '-': the JSON object must be str, bytes or bytearray

知道如何从上面的 json 中提取“dns_name”吗?

这是从上面的 json 输出中获取 dns_name 的方法:

- name: Get Application Load Balancer DNS Name
  set_fact:
    rezultat: "{{ albinfo | json_query('load_balancers[*].dns_name') }}"
- debug:
    msg: "{{ rezultat }}"