# Passing variables between hosts

**URL:** <https://forum.ansible.com/t/passing-variables-between-hosts/33078>\
**Category:** Ansible Project\
**Created:** [June 3, 2020, 5:57am UTC](https://forum.ansible.com/t/passing-variables-between-hosts/33078 "2020-06-03T05:57:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Morgan](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@Andrew\_Morgan](https://forum.ansible.com/u/Andrew_Morgan)\
**Post date:** [June 3, 2020, 5:57am UTC](https://forum.ansible.com/t/passing-variables-between-hosts/33078/1 "2020-06-03T05:57:02Z")

</div>

Hello all,

I have a playbook that is 99% complete but fails on the last step.My inventory file and playbook are below. I only added the last parts

`

[dev]  
dev\_jenkins ansible\_ssh\_host=44.244.444.44 ansible\_python\_interpreter=/usr/bin/python27  
will have multiple servers

[dev:vars]  
ansible\_ssh\_user=atest  
ansible\_ssh\_private\_key\_file=/Users/confluence/Desktop/atest  
ansible\_become\_user=root  
ansible\_become\_method=sudo  
ansible\_become\_password=changeme  
ansible\_ssh\_pass= changeme

[test]

[test:vars]  
ansible\_ssh\_user=atest  
ansible\_ssh\_private\_key\_file=/Users/confluence/Desktop/atest  
ansible\_become\_user=root  
ansible\_become\_method=sudo  
ansible\_become\_password= changeme  
ansible\_ssh\_pass= changeme

`

1)takes image of live server  
2)launches the server  
3)verify login  
4)kill the server

The issue I am having is with killing the server - name: Terminate instances that were previously launched

- hosts: all

- name: Launch instance  
ec2:  
key\_name: ansible  
group: “{{ group }}”  
instance\_type: m1.small  
image: “{{ image }}”  
wait: true  
region: “{{ region }}”  
vpc\_subnet\_id: “{{ subnet}}”  
assign\_public\_ip: yes  
register: ec2

- name: Add new instance to host group  
add\_host:  
hostname: “{{ item.public\_ip }}”  
groupname: test  
loop: “{{ ec2.instances }}”

- name: Waiting for SSH to come up  
delegate\_to: “{{ item.public\_ip }}”  
wait\_for\_connection:  
delay: 60  
timeout: 320  
loop: “{{ ec2.instances }}”

- hosts: test. ##blank in the inventory file, but new instance added to it so i can connect to the new instance with password and key  
become: True  
gather\_facts: True

tasks:

- name: Testing out the servers  
shell: “. /etc/profile; echo $DEV\_ENV”  
args:  
executable: /bin/bash  
register: ENTRY

- name: Output for test server  
debug: msg=“{{ ENTRY.stdout }}”

- name: getting instance id again  
shell: “wget -q -O - [http://169.254.169.254/latest/meta-data/instance-id](http://169.254.169.254/latest/meta-data/instance-id)”  
register: instanceterminate

- name: Terminate instances that were previously launched  
ec2:  
state: ‘absent’  
instance\_ids: “{{ instanceterminate.stdout }}”  
region: us-east-1

# when: ENTRY.rc == 0 or ENTRY.rc \>= 2

Problem:

In my inventory file, I have unique parameters per host like **ansible\_python\_interpreter=/usr/bin/python27** , maybe different for each server. So when I add the newly spun up instance from the freshly taken ami I add it to a group called test which is blank in my inventory file(it has the key and password needed to login into the machine.) I do not know of a way to specify the **ansible\_python\_interpreter** that was used to be used again in this last step and I cannot specify it because it will change from server to server, and so commands to terminate the instance gives error.

`changed=false msg: boto required for this module`

Is there a way I can make this work?

Thank you

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [June 3, 2020, 6:37am UTC](https://forum.ansible.com/t/passing-variables-between-hosts/33078/2 "2020-06-03T06:37:24Z")

</div>

You seem to be trying to terminate an instance from that very instance?

That doesn’t sound like it would work.  
You should use another host.

---

<div class="post-metadata">

**Author:** ![Andrew\_Morgan](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@Andrew\_Morgan](https://forum.ansible.com/u/Andrew_Morgan)\
**Post date:** [June 7, 2020, 12:02am UTC](https://forum.ansible.com/t/passing-variables-between-hosts/33078/3 "2020-06-07T00:02:38Z")

</div>

Thank Mr.Visser, using delegate\_to worked

- name: Terminate instances that were previously launched  
ec2:  
state: ‘absent’  
instance\_ids: “{{ instanceterminate.stdout }}”  
region: us-east-1  
delegate\_to: dev\_java

# when: ENTRY.rc == 0 or ENTRY.rc \>= 2
