ansible.vars.unsafe_proxy.AnsibleUnsafeText วัตถุ' ไม่มีแอตทริบิวต์ 'public_ip'

เหตุใดงานนี้ (จาก วิธีที่ดีที่สุดในการเปิดใช้อินสแตนซ์ aws ec2 ด้วย เข้าใจได้):

  - name: Add the newly created EC2 instance(s) to the local host group (located inside the directory)
    local_action: lineinfile 
                  dest="./hosts" 
                  regexp={{ item.public_ip }} 
                  insertafter="[webserver]" line={{ item.public_ip }}
    with_items: ec2.instances

สร้างข้อผิดพลาดนี้ใช่ไหม?

TASK [Add the newly created EC2 instance(s) to the local host group (located inside the directory)] ********************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'ansible.vars.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'public_ip'\n\nThe error appears to have been in '/Users/snowcrash/ansible-ec2/ec2_launch.yml': line 55, column 9, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n      - name: Add the newly created EC2 instance(s) to the local host group (located inside the directory)\n        ^ here\n"}

person Snowcrash    schedule 22.07.2017    source แหล่งที่มา


คำตอบ (2)


ปัญหาอยู่ที่นี่

with_items: ec2.instances

มันควรจะเป็น:

with_items: '{{ ec2.instances }}'

ec2 เป็นตัวแปรที่อ้างอิงพจนานุกรม ดังนั้นคุณจะต้องอ้างอิงพจนานุกรมด้วยไวยากรณ์ที่เหมาะสม

person Akil    schedule 16.10.2017
comment
ขอบคุณ!! นั่นช่วยได้ - person Souad; 22.05.2018

ใส่:

  - debug: msg="{{ ec2.instances }}"

ก่อนโค้ดนั้นและตรวจสอบว่ามีเนื้อหาของตัวแปรนั้นอะไรบ้าง ควรเป็นรายการพจนานุกรมที่แต่ละพจนานุกรมมีสมาชิก public_ip ไม่เช่นนั้นคุณจะได้รับข้อความที่คุณได้รับ

person plesiv    schedule 22.07.2017
comment
ฉันกำลังเปิดตัว 1 อินสแตนซ์ มีตัวแปร public_ip และมีค่า - person Snowcrash; 23.07.2017