# Manipulating part of a custom variable

**URL:** <https://forum.ansible.com/t/manipulating-part-of-a-custom-variable/28932>\
**Category:** Ansible Project\
**Created:** [November 2, 2018, 8:49pm UTC](https://forum.ansible.com/t/manipulating-part-of-a-custom-variable/28932 "2018-11-02T20:49:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [November 2, 2018, 8:49pm UTC](https://forum.ansible.com/t/manipulating-part-of-a-custom-variable/28932/1 "2018-11-02T20:49:29Z")

</div>

I have a variable called ‘oldid’ set to a value of ‘0004fb0000060000b824b442d236a8b7’

I wish to manipulate it so that I do the following:

1. strip off the last 12 characters (slicing?)
2. Re-add 12 more characters that are a random numbers between 0-9

Here is what I have, but unsure if I can even do this (it is failing). Syntax is wrong, but is hopefully descriptive in nature. Wonder if my approach is wrong too?

`  
tasks:

- name: Set new\_id  
set\_fact:  
new\_id: “{{ oldid[:-12]{{ 9 | random }}”

`

I need to repeat the {{ 9 | random }} a total of 12 times… don’t know if there is a shortcut way to do that other than retyping the command over and over.

---

<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:** [November 2, 2018, 9:24pm UTC](https://forum.ansible.com/t/manipulating-part-of-a-custom-variable/28932/2 "2018-11-02T21:24:21Z")

</div>

Set random from a high number should do it

&nbsp;&nbsp;{{ oldid[:-12] ~ 1000000000000 | random(100000000000) }}"

this will only change from 100.000.000.000 to to 999.999.999.999 so your 12 last digit will never start with 0.

If you need to generate the same "random" number each time you need a predictable seed like inventory\_hostname

&nbsp;&nbsp;{{ oldid[:-12] ~ 1000000000000 | random(100000000000,seed=inventory\_hostname) }}"

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [November 2, 2018, 9:35pm UTC](https://forum.ansible.com/t/manipulating-part-of-a-custom-variable/28932/3 "2018-11-02T21:35:01Z")

</div>

Awesome, that works! Using a large number for randomizing didn’t even occur to me. Just one follow-up question. Can you explain to me what the tilde ~ is doing? Having a hard time finding info on it.

---

<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:** [November 2, 2018, 9:43pm UTC](https://forum.ansible.com/t/manipulating-part-of-a-custom-variable/28932/4 "2018-11-02T21:43:06Z")

</div>

It Jinja for concatenate strings[1].  
Sometimes you see people using + and that will work if everything is stings, but if they are numbers Jinja will add them together, so the safest thing is to use ~ to concatenate.

[1] [http://jinja.pocoo.org/docs/2.10/templates/#other-operators](http://jinja.pocoo.org/docs/2.10/templates/#other-operators)

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [November 2, 2018, 9:43pm UTC](https://forum.ansible.com/t/manipulating-part-of-a-custom-variable/28932/5 "2018-11-02T21:43:58Z")

</div>

Thank you

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [November 5, 2018, 5:53pm UTC](https://forum.ansible.com/t/manipulating-part-of-a-custom-variable/28932/6 "2018-11-05T17:53:55Z")

</div>

How can I throw in a custom string? Everything I try seems to fail:

Working:

`newname: "{{ oldname.stdout ~ ansible_date_time.date }}"`  
Returns:  
myserver2018-11-05

Desired:  
myserver-COPY-2018-11-05

---

<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:** [November 5, 2018, 5:59pm UTC](https://forum.ansible.com/t/manipulating-part-of-a-custom-variable/28932/7 "2018-11-05T17:59:05Z")

</div>

newname: "{{ oldname.stdout ~ '-COPY-' ~ ansible\_date\_time.date }}"

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [November 5, 2018, 6:00pm UTC](https://forum.ansible.com/t/manipulating-part-of-a-custom-variable/28932/8 "2018-11-05T18:00:40Z")

</div>

Ah, it was the single quotes that I was missing. Thank you.
