我有一些Ansible剧本
---
- 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 :(得分:2)
请注意ec2
和ec2_remote_facts
的结果不一致。
ec2
填充private_ip
和public_ip
ec2_remote_facts
填充private_ip_address
和public_ip_address
。
针对每个案例适当更改add_host
任务。