# string operations

**URL:** <https://forum.ansible.com/t/string-operations/12994>\
**Category:** Ansible Project\
**Created:** [June 20, 2013, 12:28pm UTC](https://forum.ansible.com/t/string-operations/12994 "2013-06-20T12:28:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Clifford\_Sanders](https://avatars.discourse-cdn.com/v4/letter/c/3be4f8/32.png) [@Clifford\_Sanders](https://forum.ansible.com/u/Clifford_Sanders)\
**Post date:** [June 20, 2013, 12:28pm UTC](https://forum.ansible.com/t/string-operations/12994/1 "2013-06-20T12:28:50Z")

</div>

Hi,

I need to replace characters in strings so i can change ‘com.bar.foo’ into ‘com/bar/foo’

common\_vars.yml:

---

<div class="post-metadata">

**Author:** ![jpmens1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jpmens1](https://forum.ansible.com/u/jpmens1)\
**Post date:** [June 20, 2013, 12:46pm UTC](https://forum.ansible.com/t/string-operations/12994/2 "2013-06-20T12:46:09Z")

</div>

> I need to replace characters in strings so i can change 'com.bar.foo' into  
> 'com/bar/foo'

The only thing that occurs to me is  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- local\_action: shell echo "${group\_id}" | sed 's|\.|/|g'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: new\_group\_id

Then use ${new\_group\_id.stdout}

Overkill probably ...

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP

---

<div class="post-metadata">

**Author:** ![martin\_f\_krafft](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@martin\_f\_krafft](https://forum.ansible.com/u/martin_f_krafft)\
**Post date:** [June 20, 2013, 12:52pm UTC](https://forum.ansible.com/t/string-operations/12994/3 "2013-06-20T12:52:55Z")

</div>

also sprach Clifford Sanders \<clifford650@gmail.com\> [2013.06.20.1428 +0200]:

> [http://nexus/releases/$\{com\.bar\.foo\}\.replace\(&quot;\.&quot;,&quot;/&quot;\)/$\{artifact\_id\}/$\{version\}](http://nexus/releases/$%5C%7Bcom%5C.bar%5C.foo%5C%7D%5C.replace%5C(&quot;%5C.&quot;,&quot;/&quot;%5C)/$%5C%7Bartifact%5C_id%5C%7D/$%5C%7Bversion%5C%7D)

Did you try ${foo.replace(…)} ?

Starting with version 1.2, you should be able to use Jinja2 filters:

&nbsp;&nbsp;{{ foo | replace('.', '/') }}

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [June 20, 2013, 4:10pm UTC](https://forum.ansible.com/t/string-operations/12994/4 "2013-06-20T16:10:57Z")

</div>

You shouldn’t even need to know it’s Jinja2 since we can access the .replace method on the string itself.

{{ foo.replace(‘.’,‘/’) }}

Should you wish to store this result, the new “set\_fact” module in 1.2 provides an easy way to do that without the “register” trick JP Mens showed as well.

---

<div class="post-metadata">

**Author:** ![Antonio\_Messina](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Antonio\_Messina](https://forum.ansible.com/u/Antonio_Messina)\
**Post date:** [June 20, 2013, 4:14pm UTC](https://forum.ansible.com/t/string-operations/12994/5 "2013-06-20T16:14:39Z")

</div>

> You shouldn't even need to know it's Jinja2 since we can access the .replace  
> method on the string itself.
> 
> {{ foo.replace('.','/') }}
> 
> Should you wish to store this result, the new "set\_fact" module in 1.2  
> provides an easy way to do that without the "register" trick JP Mens showed  
> as well.

AFAIK, these solutions are \*not\* available with ansible 1.1  
I know because I was looking for it and had to solve with a  
"local\_action" as well..

.a.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [June 20, 2013, 4:23pm UTC](https://forum.ansible.com/t/string-operations/12994/6 "2013-06-20T16:23:59Z")

</div>

Correct, Ansible 1.2 is where “{{ these }}” were added, to unify the way variables and templates work between templates and playbooks.

---

<div class="post-metadata">

**Author:** ![Clifford\_Sanders](https://avatars.discourse-cdn.com/v4/letter/c/3be4f8/32.png) [@Clifford\_Sanders](https://forum.ansible.com/u/Clifford_Sanders)\
**Post date:** [June 21, 2013, 3:20pm UTC](https://forum.ansible.com/t/string-operations/12994/7 "2013-06-21T15:20:27Z")

</div>

Thank you all for your support.

I’m trying to update to 1.2 but have a problem using this version in my vagrant development environment. This could be related to [https://github.com/ansible/ansible/pull/3147](https://github.com/ansible/ansible/pull/3147).

Regards,

Clifford
