Amazon ec2 Ansible一直抱怨公共ip没有定义,这已经是ec2项目注册中的一个属性

Amazon ec2 Ansible一直抱怨公共ip没有定义,这已经是ec2项目注册中的一个属性,amazon-ec2,ansible,Amazon Ec2,Ansible,我一直在尝试应用simple playbook来启动ec2实例,并尝试获取实例的公共ip并编辑主机文件,以便可以在该实例上执行下一行playbook。但每次我添加items.public\u ip时,它都会抛出未定义的错误。我错过什么了吗 > fatal: [localhost]: FAILED! => { > "failed": true, > "msg": "the field 'args' has an in

我一直在尝试应用simple playbook来启动ec2实例,并尝试获取实例的公共ip并编辑主机文件,以便可以在该实例上执行下一行playbook。但每次我添加items.public\u ip时,它都会抛出未定义的错误。我错过什么了吗

>     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:
    'unicode
    > object' has no attribute 'public_ip'\n\nThe error appears to have been
    > in <some path>  line 76, 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"
    >     }
我的任务部分:

 - 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="[vm]" line="{{ item.public_ip }}"
    with_items: ec2_instances

任何帮助都会有帮助

我能够通过提供vpc_subnet_id以及ec2模块中定义的公共_ip属性来找出答案,以便在剧本中进一步使用。我认为可以强制使用network interface属性来获取ec2实例的实际使用情况。

尝试
使用以下项目:“{{ec2.instances}}”
尝试以下方法:

   - name: Check ec2_public_ip
     command: curl http://169.254.169.254/latest/meta-data/public-ipv4
     register: ec2_public_ip
参考:

尽管我尝试了直接方法在VAR中提供这些公共dns名称和id,但运气不佳,但json for ec2提供了所有详细信息…不确定为什么它没有收到..:(可能是
   - name: Check ec2_public_ip
     command: curl http://169.254.169.254/latest/meta-data/public-ipv4
     register: ec2_public_ip