ansible.vars.unsafe_proxy.AnsibleUnsafeText 对象'没有属性'public_ip'

Sno*_*ash 3 amazon-ec2 amazon-web-services ansible

为什么要执行此任务(来自使用 ansible 启动 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
Run Code Online (Sandbox Code Playgroud)

创建这个错误?

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"}
Run Code Online (Sandbox Code Playgroud)

小智 6

问题在这里

with_items: ec2.instances
Run Code Online (Sandbox Code Playgroud)

它应该是:

with_items: '{{ ec2.instances }}'
Run Code Online (Sandbox Code Playgroud)

ec2 是引用字典的变量,因此您需要使用正确的语法来引用它