Ansible一直在抱怨未定义的public_ip,这已经是ec2项目注册的属性

问题描述 投票:0回答:2

我一直在尝试应用简单的playbook来启动ec2实例并尝试获取实例的public_ip并编辑hosts文件,以便在该实例上执行下一行的playbook。但每次我添加items.public_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"
    >     }

我的调试ec2项目确实显示列表中的公共IP。

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

任何帮助都会有所帮助!!

amazon-ec2 ansible
2个回答
0
投票

我能够通过提供vpc_subnet_id以及ec2模块中定义的public_ip属性来找出答案,因此可以在playbooks中使用其他任务。我认为可以强制使用网络接口属性来获得ec2实例的实际使用。


0
投票

试试with_items: "{{ ec2.instances }}"

© www.soinside.com 2019 - 2024. All rights reserved.