Regex in Ansible Playbook YAML

2,513 views
Skip to first unread message

Danny Beutler

unread,
Dec 16, 2013, 7:42:23 PM12/16/13
to ansible...@googlegroups.com
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

Matt Martz

unread,
Dec 16, 2013, 10:27:36 PM12/16/13
to ansible...@googlegroups.com, Danny Beutler
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] }}'
-- 
Matt Martz
ma...@sivel.net
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Danny Beutler

unread,
Dec 17, 2013, 12:39:48 AM12/17/13
to ansible...@googlegroups.com, Danny Beutler
So Simple ha :) Thanks Matt!
Reply all
Reply to author
Forward
0 new messages