Reputation: 495
I want to pass "private_dns_name" of my newly created ec2 instances to some other host in my ansible play book. I have tried using set_fact module to define the variable but no luck! This is ansible playbook entries for host1
- name: new EC2 instance
hosts: host1
tasks:
- name: Launch the new EC2 Instance
ec2:
aws_access_key: "{{ aws_id }}"
aws_secret_key: "{{ aws_key }}"
group_id: "{{ security_group_id }}"
instance_type: "{{ instance_type }}"
image: "{{ image }}"
key_name: "{{ ssh_keyname }}"
wait: yes
wait_timeout: 900
region: "{{ region }}"
count: 1
instance_tags:
Name: Prod-Engineering
register: ec2
- name: Register public DNS fact
set_fact: public_dns= "{{ item.private_dns_name }}"
with_items: ec2.instances
After defining set_fact i am using below sample code to grab the private_dns_name to my other host2:
- hosts: host2
become: yes
tasks:
- command: ipa host-add "{{ public_dns}}" --password=xxxxx
but i am getting public_dns variable not defined error, when executing my playbook. How can i retrieve/pass variable from newly created ec2 instance to another other host/ec2 instance?
Upvotes: 0
Views: 923
Reputation: 68339
Use ansible magic variables to access other hosts' facts:
- hosts: host2
become: yes
tasks:
- command: ipa host-add "{{ hostvars['host1']['public_dns'] }}" --password=xxxxx
Upvotes: 0