I have some Ansible playbook
---
- 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
When I run this playbook, I am getting next error:
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'
I am a little confused, because the same code works fine during ec2 creation and provisioning.
In the first play debug: var=ec2_remote_facts
shows me my instances, which I created during second playbook.
---
- 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 version is 2.2.1.0
What am I missing? Thanks in advance!
Beware of inconsistency in results of ec2
and ec2_remote_facts
.
ec2
populates private_ip
and public_ip
.
ec2_remote_facts
populates private_ip_address
and public_ip_address
.
Change your add_host
task appropriately for each case.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.