# Host File as a command line variable

**URL:** <https://forum.ansible.com/t/host-file-as-a-command-line-variable/30270>\
**Category:** Ansible Project\
**Created:** [May 6, 2019, 11:15am UTC](https://forum.ansible.com/t/host-file-as-a-command-line-variable/30270 "2019-05-06T11:15:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Andrew](https://avatars.discourse-cdn.com/v4/letter/j/8491ac/32.png) [@John\_Andrew](https://forum.ansible.com/u/John_Andrew)\
**Post date:** [May 6, 2019, 11:15am UTC](https://forum.ansible.com/t/host-file-as-a-command-line-variable/30270/1 "2019-05-06T11:15:56Z")

</div>

Can i do this in my hosts/inventory file? Also, can i pass a host group in command line -e?

[env1]  
vm1 ansible\_host=vm001  
vm2 ansible\_host=vm002

[env2]  
vm1 ansible\_host=vm888  
vm2 ansible\_host=vm999

I want to use the same synonym host name in my playbook (vm1 or vm2 ) but point to different virtual machine depending on what group i specify in the yml file. (hosts: env1)  
This way i can make the code generic across groups with the same vm1 or vm2. I have duplicate VMs for testing and development.

thanks

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 9, 2019, 5:56pm UTC](https://forum.ansible.com/t/host-file-as-a-command-line-variable/30270/2 "2019-05-09T17:56:34Z")

</div>

You can't, groups is not for separating variable it's for grouping host together so you don't need to list them all.  
A host will get all variables defined in all groups it's a member of.

So your ansible\_host for vm1 in group env1 will be overwritten by ansible\_host for vm1 in group env2.
