# How to assign to an object with arbitrary characters in key

**URL:** <https://forum.ansible.com/t/how-to-assign-to-an-object-with-arbitrary-characters-in-key/29813>\
**Category:** Ansible Project\
**Created:** [March 3, 2019, 10:12am UTC](https://forum.ansible.com/t/how-to-assign-to-an-object-with-arbitrary-characters-in-key/29813 "2019-03-03T10:12:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oleg\_Semyonov](https://avatars.discourse-cdn.com/v4/letter/o/3ec8ea/32.png) [@Oleg\_Semyonov](https://forum.ansible.com/u/Oleg_Semyonov)\
**Post date:** [March 3, 2019, 10:12am UTC](https://forum.ansible.com/t/how-to-assign-to-an-object-with-arbitrary-characters-in-key/29813/1 "2019-03-03T10:12:08Z")

</div>

I have a list of objects with names like this: ‘obj-0001’, ‘obj-0002’ and need to set\_stats with some data about these objects in the following structure:

“data”: {  
“obj-0001”: { “a”: 1, “b”: 2 },

“obj-0002”: { “a”: 3, “b”: 4 }

}

They then can be referenced as data[‘obj-0001’].a

set\_stats:

data:  
‘obj\_0001’: 5 # this works

‘obj-0002’: 6 # this does NOT work: the variable name ‘obj-0002’ is not valid

As far as I understand, there is no limitation on character set in keys while they are accessed using quotes in brackets: data[‘obj-0002’].

Is there any way to make such assignment in ansible? I really do not want to restrict character sets in hash keys to alphanumeric only if possible.

---

<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:** [March 3, 2019, 1:48pm UTC](https://forum.ansible.com/t/how-to-assign-to-an-object-with-arbitrary-characters-in-key/29813/2 "2019-03-03T13:48:48Z")

</div>

There are limits as they are variables in Ansible.  
From  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_variables.html#creating-valid-variable-names](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#creating-valid-variable-names)

"Variable names should be letters, numbers, and underscores. Variables should always start with a letter.

foo\_port is a great variable. foo5 is fine too.

foo-port, foo port, foo.port and 12 are not valid variable names."
