2017-03-09 38 views
1

我有一些Ansible剧本Ansible,ec2_remote_facts - 错误是:“字典对象有没有属性

--- 

- name: Sandbox 
    hosts: localhost 
    connection: local 
    gather_facts: true 
    tasks: 

    - name: Get facts by filter 
    ec2_remote_facts: 
     region: "{{ aws_default_region }}" 
     filters: 
     instance-state-name: running 
    register: ec2_remote_facts 

    - name: debug 
    debug: var=ec2_remote_facts 

    - name: Add running sandbox instances to in-memory inventory host group 
    add_host: 
     hostname: "{{ item.public_ip }}" 
     groups: running 
    with_items: "{{ ec2_remote_facts.instances }}" 

- name: Configure provisioned servers 
    hosts: running 
    gather_facts: true 
    user: ec2-user 
    become: true 
    roles: 
    - base 

当我运行这个剧本,我得到一个错误:

TASK [Add running sandbox instances to in-memory inventory host group] ********* 
fatal: [127.0.0.1]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'dict object' has no attribute 'public_ip' 

我我有点困惑,因为在EC2创建和配置期间,相同的代码工作正常。

在第一次玩debug: var=ec2_remote_facts显示我的实例,我在第二个剧本期间创建。

--- 

- name: Sandbox instances 
    hosts: localhost 
    connection: local 
    gather_facts: false 

    tasks: 

    - name: Launch a sandbox instance 
    ec2: 
     keypair: "{{ aws_default_keypair }}" 
     aws_access_key: "{{ aws_access_key }}" 
     aws_secret_key: "{{ aws_secret_key }}" 
     instance_type: "{{ aws_default_instance_type }}" 
     image: "{{ aws_default_ami_image }}" 
     region: "{{ aws_default_region }}" 
     group: "{{ aws_default_security_group_name }}" 
     count: "{{ aws_default_count_of_instances }}" 
     instance_tags: 
     Name: "{{ aws_default_instance_tag_name }}" 
     Group: "{{ aws_default_instance_tag_group }}" 
     wait: yes 
    register: ec2 

    - name: debug 
    debug: var=ec2 

    - name: Add new sandbox instances to in-memory inventory host group 
    add_host: 
     hostname: "{{ item.public_ip }}" 
     groupname: running 
    with_items: "{{ ec2.instances }}" 

    - name: Wait for SSH to come up 
    wait_for: 
     host: "{{ item.public_dns_name }}" 
     port: 22 
     delay: 60 
     timeout: 320 
     state: started 
    with_items: "{{ ec2.instances }}" 

- name: Configure provisioned servers 
    hosts: running 
    gather_facts: false 
    user: ec2-user 
    become: true 
    roles: 
    - base 

Ansible版本是2.2.1.0

我缺少什么?提前致谢!

+0

为什么不在第一次播放时在debug:var = ec2中出错?相反,'debug:var = ec2_remote_facts'的输出是什么? – techraf

+0

'debug:var = ec2_remote_facts'显示我现有的实例 – BattleLoot

+0

我没有要求您的解释。我要求你在问题中包含输出。 – techraf

回答

2

请注意ec2ec2_remote_facts的结果不一致。

ec2填充private_ippublic_ip
ec2_remote_facts填充private_ip_addresspublic_ip_address

为每种情况适当地更改您的add_host任务。

+0

它的作品,我的错字错误...非常感谢! – BattleLoot

相关问题