如何在任务中访问寄存器变量

时间:2018-07-15 14:48:46

标签: ansible-2.x

这是我的EC2实例的输出。我正在尝试访问“ instance_type”。 这是我的任务。

ec2:
  key_name: redhat
  group: MY_EC2
  instance_type: t2.micro
  image: ami-cfe4b2b0
  region: us-east-1
  zone: us-east-1a
  wait: true
  exact_count: 1
  count_tag:
    name: MyProjectInstances
  instance_tags:
    name: Ansible
register: ec2

-set_fact:       实例:“ {{ec2}}”   -调试:       信息:“ {{inst}}”

通过尝试此操作,我可以到达Instances块。

  • 调试:   msg:“ {{inst.instances}}”,但无法继续进行,得到未定义变量的错误。 #

输出:

ok: [localhost] => {
    "msg": {
        "changed": true,
        "failed": false,
        "instance_ids": [
            "i-0be089202b191769e"
        ],
        "instances": [
            {
                "ami_launch_index": "0",
                "architecture": "x86_64",
                "block_device_mapping": {
                    "/dev/xvda": {
                        "delete_on_termination": true,
                        "status": "attached",
                        "volume_id": "vol-02b129004f1a5fb89"
                    }
                },
                "dns_name": "ec2-34-204-84-170.compute-1.amazonaws.com",
                "ebs_optimized": false,
                "groups": {
                    "sg-06c09a2c83d7b1a96": "MY_EC2"
                },
                "hypervisor": "xen",
                "id": "i-0be089202b191769e",
                "image_id": "ami-cfe4b2b0",
                "instance_type": "t2.micro",
                "kernel": null,
                "key_name": "redhat",
                "launch_time": "2018-07-15T14:34:43.000Z",
                "placement": "us-east-1a",
                "private_dns_name": "ip-172-31-35-24.ec2.internal",
                "private_ip": "172.31.35.24",
                "public_dns_name": "ec2-34-204-84-170.compute-1.amazonaws.com",
                "public_ip": "34.204.84.170",
                "ramdisk": null,
                "region": "us-east-1",
                "root_device_name": "/dev/xvda",
                "root_device_type": "ebs",
                "state": "running",
                "state_code": 16,
                "tags": {
                    "name": "Ansible"
                },
                "tenancy": "default",
                "virtualization_type": "hvm"
            }

1 个答案:

答案 0 :(得分:0)

请尝试以下操作:

    - name: Get instance Type
      debug: msg={{ inst | json_query('instances[].instance_type') }}