# Regex in Ansible Playbook YAML

**URL:** <https://forum.ansible.com/t/regex-in-ansible-playbook-yaml/14167>\
**Category:** Ansible Project\
**Created:** [December 17, 2013, 12:42am UTC](https://forum.ansible.com/t/regex-in-ansible-playbook-yaml/14167 "2013-12-17T00:42:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Danny\_Beutler](https://avatars.discourse-cdn.com/v4/letter/d/bbe5ce/32.png) [@Danny\_Beutler](https://forum.ansible.com/u/Danny_Beutler)\
**Post date:** [December 17, 2013, 12:42am UTC](https://forum.ansible.com/t/regex-in-ansible-playbook-yaml/14167/1 "2013-12-17T00:42:23Z")

</div>

Hello,

I am trying to configure a playbook for setting up nodes in an open stack cluster. As part of the openstack cluster, you can pass a “group” variable which will put nodes in the group on different compute nodes. We already have a naming convention that allows us to do this fairly easily.  
For example, our database servers are named loc-env-ssdb-01, loc-env-ssdb-02, loc-env-ssdb-03, etc.  
Since we want all the db servers on different compute nodes, we should just be able to pass the node name, trimmed of the last three characters, into the group value. This would keep the vms on different hosts. However, I can’t find a great way to do this in the YAML file.

Here is what I have so far:

nova\_compute:  
state: present  
login\_username: ‘{{ tenant\_username }}’  
login\_password: ‘{{ tenant\_password }}’  
login\_tenant\_name: ‘{{ tenant\_env }}’  
auth\_url: ‘{{ auth\_url }}’  
flavor\_id: ‘{{ flavor\_id }}’  
key\_name: ‘{{ keypair\_name }}’  
name: ‘{{ item.vm\_name }}’  
wait\_for: 440  
image\_id: ‘{{ image.id }}’  
nics:

- net-id: ‘{{ network\_id }}’  
userdata: ‘{{ userdata }}’  
meta: #‘{{item.meta }}’  
group: ‘{{ item.vm\_name }}’ # This is where I want a regex or something to trim the last 3 characters from the vm name and pass that in as the group.  
#role: ‘{{ item.meta.role }}’  
register: vm  
with\_items: vms

Any help would be appreciated.

Thanks,  
Danny

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [December 17, 2013, 3:27am UTC](https://forum.ansible.com/t/regex-in-ansible-playbook-yaml/14167/2 "2013-12-17T03:27:36Z")

</div>

There’s no need to use regex if you know you always only need to remove the last 3 characters. For this you can using slice notation:

group: ‘{{ item.vm\_name[:-3] }}’

---

<div class="post-metadata">

**Author:** ![Danny\_Beutler](https://avatars.discourse-cdn.com/v4/letter/d/bbe5ce/32.png) [@Danny\_Beutler](https://forum.ansible.com/u/Danny_Beutler)\
**Post date:** [December 17, 2013, 5:39am UTC](https://forum.ansible.com/t/regex-in-ansible-playbook-yaml/14167/3 "2013-12-17T05:39:48Z")

</div>

So Simple ha 🙂 Thanks Matt!
