2016-08-29 7 views
1

私はec2インスタンスを自動化することができますか?私はec2インスタンスを作成し、ec2_remote_factsモジュールを介してパブリックDNS名を取得しようとしています。出力に定義されていない変数を与えています。ec2インスタンスを入手するにはどうすればいいのでしょうか?

playbook: 

- hosts: localhost 
    connection: local 

    tasks: 
    - name: ec2 instance facts 
     ec2_remote_facts: 
     region: ap-southeast-2 
     filters: 
      instance-state-name: running 
     register: ec2 

    - debug: var=ec2.instances.public_name 


Output:PLAY [localhost] *************************************************************** 

TASK [setup] ******************************************************************* 
ok: [localhost] 

TASK [create ec2 instance] ***************************************************** 
ok: [localhost] 

TASK [debug] ******************************************************************* 
ok: [localhost] => { 
    "ec2.instances.public_name": "VARIABLE IS NOT DEFINED!" 
} 

PLAY RECAP ********************************************************************* 
localhost     : ok=3 changed=0 unreachable=0 failed=0 



Output for ec2.instances. 

ok: [localhost] => { 
    "ec2.instances": [ 
     { 
      "ami_launch_index": "0", 
      "architecture": "x86_64", 
      "client_token": "", 
      "ebs_optimized": false, 
      "groups": [ 
       { 
        "id": "sg-6c016a08", 
        "name": "default" 
       } 
      ], 
      "hypervisor": "xen", 
      "id": "i-915b1813", 
      "image_id": "ami-fedafc9d", 
      "instance_profile": null, 
      "interfaces": [ 
       { 
        "id": "eni-96de4acf", 
        "mac_address": "0a:14:ac:64:c4:13" 
       } 
      ], 
      "kernel": null, 
      "key_name": "ansible.key", 
      "launch_time": "2016-08-29T07:32:10.000Z", 
      "monitoring_state": "disabled", 
      "persistent": false, 
      "placement": { 
       "tenancy": "default", 
       "zone": "ap-southeast-2c" 
      }, 
      "private_dns_name": "ip-xx-xx-xx-107.ap-southeast-2.compute.internal", 
      "private_ip_address": "xx.xx.xx.107", 
      "public_dns_name": "ec2-xx-xxx-xx-80.ap-southeast-2.compute.amazonaws.com", 
      "ramdisk": null, 
      "region": "ap-southeast-2", 
      "requester_id": null, 
      "root_device_type": "ebs", 
      "source_destination_check": "true", 
      "spot_instance_request_id": null, 
      "state": "running", 
      "tags": { 
       "Name": "Demo" 
      }, 
      "virtualization_type": "hvm", 
      "vpc_id": "vpc-abcaf4ce" 
     } 
    ] 
} 

私はここで何が欠けていますか?

おかげ

Benjo

+1

フレンドリーアドバイス: 'dict1.var1.var2'変数が未定義の場合、' dict1.var1'の中身を調べて 'var2'が利用できない理由を調べてください。 –

+0

ec2.instancesの出力を更新しました – Ben

答えて

1

あなたはec2.instancesによく見ると、あなたがそれを注意することがあります。

  • それはリストであるので、あなたのいずれかのアクセス項目インデックスec2.instances[0]によって、またはwith_items:でそれらを反復構造。
  • public_name属性はありません。public_dns_nameのみです。
+0

ありがとうございましたKonstantin、あなたの提案通りに更新されています。 – Ben

関連する問題