# When hostnames have hyphens in them

**URL:** <https://forum.ansible.com/t/when-hostnames-have-hyphens-in-them/26933>\
**Category:** Ansible Project\
**Created:** [March 8, 2018, 11:10am UTC](https://forum.ansible.com/t/when-hostnames-have-hyphens-in-them/26933 "2018-03-08T11:10:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Richard\_Sobey](https://avatars.discourse-cdn.com/v4/letter/r/439d5e/32.png) [@Richard\_Sobey](https://forum.ansible.com/u/Richard_Sobey)\
**Post date:** [March 8, 2018, 11:10am UTC](https://forum.ansible.com/t/when-hostnames-have-hyphens-in-them/26933/1 "2018-03-08T11:10:35Z")

</div>

Hi all,

I’ve come across an issue which might be a showstopper for me; either way I wouldn’t mind some advice.

My aim is to lookup in a vaulted file the name of a variable that is based around the hostname of the server. My servers have hyphens in the hostname.

I have got something like this:

- name: Copy private key  
copy:  
content: “{{ ansible\_hostname }}\_private\_key”  
dest: /root/.ssh/id\_rsa

And in my vaulted vars file a variable called “server-name\_private\_key”.

Now since variables cannot have hyphens in them, but my server hostname does, how can I lookup the variable dynamically based on which host the task is running on?

I could always have a separate task for each host and explicitly define the variable but keeping it short is my goal.

Thanks  
Richard

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [March 8, 2018, 1:50pm UTC](https://forum.ansible.com/t/when-hostnames-have-hyphens-in-them/26933/2 "2018-03-08T13:50:12Z")

</div>

variables cannot have hyphens but hash/dictionary keys can

for example, this would work: private\_keys['host-name'] or  
private\_keys[ansible\_hostname]
