# Pass conditional value to environment variable based on host in Ansible playbook

**URL:** <https://forum.ansible.com/t/pass-conditional-value-to-environment-variable-based-on-host-in-ansible-playbook/25609>\
**Category:** Ansible Project\
**Created:** [September 27, 2017, 10:00am UTC](https://forum.ansible.com/t/pass-conditional-value-to-environment-variable-based-on-host-in-ansible-playbook/25609 "2017-09-27T10:00:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Parth\_Gandhi](https://avatars.discourse-cdn.com/v4/letter/p/958977/32.png) [@Parth\_Gandhi](https://forum.ansible.com/u/Parth_Gandhi)\
**Post date:** [September 27, 2017, 10:00am UTC](https://forum.ansible.com/t/pass-conditional-value-to-environment-variable-based-on-host-in-ansible-playbook/25609/1 "2017-09-27T10:00:18Z")

</div>

Hi,  
I need to pass conditional value based on the host in the same group to environment variable in playbook. IS there a way to do it?  
Here is the use case. I have a host file with a group having 2 servers

hosts:  
[rancher]  
hostA  
hostB

rancher\_create.yaml:

- hosts: xyz  
docker\_container:  
image: xyz  
env:  
IP: {{ hostA\_IP }}

the IP environment variable should take host A ip for HostA and hostB ip for HostB. Both the servers are used for same service in cluster and for cluster to setup there is a need to pass the individual IP to the docker container cmd.

Thanks,  
Parth

---

<div class="post-metadata">

**Author:** ![Mauricio\_Tavares](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Mauricio\_Tavares](https://forum.ansible.com/u/Mauricio_Tavares)\
**Post date:** [September 27, 2017, 10:10am UTC](https://forum.ansible.com/t/pass-conditional-value-to-environment-variable-based-on-host-in-ansible-playbook/25609/2 "2017-09-27T10:10:42Z")

</div>

> Hi,  
> I need to pass conditional value based on the host in the same group to  
> environment variable in playbook. IS there a way to do it?  
> Here is the use case. I have a host file with a group having 2 servers
> 
> hosts:  
> [rancher]  
> hostA  
> hostB
> 
> rancher\_create.yaml:  
> - hosts: xyz  
> &nbsp;&nbsp;docker\_container:  
> &nbsp;&nbsp;&nbsp;&nbsp;image: xyz  
> &nbsp;&nbsp;&nbsp;&nbsp;env:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IP: {{ hostA\_IP }}
> 
> the IP environment variable should take host A ip for HostA and hostB ip  
> for HostB. Both the servers are used for same service in cluster and for  
> cluster to setup there is a need to pass the individual IP to the docker  
> container cmd.

[http://docs.ansible.com/ansible/latest/intro\_inventory.html#host-variables](http://docs.ansible.com/ansible/latest/intro_inventory.html#host-variables)

---

<div class="post-metadata">

**Author:** ![Parth\_Gandhi](https://avatars.discourse-cdn.com/v4/letter/p/958977/32.png) [@Parth\_Gandhi](https://forum.ansible.com/u/Parth_Gandhi)\
**Post date:** [September 27, 2017, 11:34am UTC](https://forum.ansible.com/t/pass-conditional-value-to-environment-variable-based-on-host-in-ansible-playbook/25609/3 "2017-09-27T11:34:38Z")

</div>

Thanks, I was able to solve it using {{ ansible\_eth0.ipv4.address }}. However your link was very helpful in understanding how the variables work.
