# Adding single quotes when I use regex\_search('\[^=\]\*$')

**URL:** <https://forum.ansible.com/t/adding-single-quotes-when-i-use-regex-search/34690>\
**Category:** Ansible Project\
**Created:** [February 26, 2021, 4:12pm UTC](https://forum.ansible.com/t/adding-single-quotes-when-i-use-regex-search/34690 "2021-02-26T16:12:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pravin\_V](https://avatars.discourse-cdn.com/v4/letter/p/c68b51/32.png) [@Pravin\_V](https://forum.ansible.com/u/Pravin_V)\
**Post date:** [February 26, 2021, 4:12pm UTC](https://forum.ansible.com/t/adding-single-quotes-when-i-use-regex-search/34690/1 "2021-02-26T16:12:45Z")

</div>

Hello Everyone,  
When I use the regex\_search in tasks, it is adding single quotes in the starting and end.  
Like for this -

- sw01 GigabitEthernet0/0 IF-MIB.ifSpeed = 1000000000  
when I use regex\_search-

- name: SET FACT - Interface SPEED -1  
set\_fact:  
speeds: “{{ item.0 | regex\_search(‘[^=]\*$’) | regex\_replace(’ ', ‘’) }}”

Converting above value to -

ok: [localhost] =\> (item=[‘sw01 GigabitEthernet0/0 IF-MIB.ifSpeed = 1000000000’])

My goal is fetch the value after ‘=’  
sw01 GigabitEthernet0/0 IF-MIB.ifSpeed = 1000000000  
to  
1000000000

please advise.

---

<div class="post-metadata">

**Author:** ![Roberto\_J\_R\_Paz](https://avatars.discourse-cdn.com/v4/letter/r/d2c977/32.png) [@Roberto\_J\_R\_Paz](https://forum.ansible.com/u/Roberto_J_R_Paz)\
**Post date:** [February 26, 2021, 4:43pm UTC](https://forum.ansible.com/t/adding-single-quotes-when-i-use-regex-search/34690/2 "2021-02-26T16:43:31Z")

</div>

Maybe you can use split.

item.split(“=”)[1]

should refer to what is at the right of “=”.

---

<div class="post-metadata">

**Author:** ![Pravin\_V](https://avatars.discourse-cdn.com/v4/letter/p/c68b51/32.png) [@Pravin\_V](https://forum.ansible.com/u/Pravin_V)\
**Post date:** [February 26, 2021, 4:54pm UTC](https://forum.ansible.com/t/adding-single-quotes-when-i-use-regex-search/34690/3 "2021-02-26T16:54:41Z")

</div>

Thank you,  
This is how I wrote-

- name: SET FACT - Interface SPEED -1  
set\_fact:  
speed\_list1: “{{ item.split(‘=’)[1] | default(omit) }}”  
with\_items:

- ‘{{ speed\_list }}’

- name: Debug Speed  
debug: msg=“{{ speed\_list1 }}”

Still the output is same-

TASK [Debug Speed] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\>  
msg: ’ 0’

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [February 26, 2021, 5:47pm UTC](https://forum.ansible.com/t/adding-single-quotes-when-i-use-regex-search/34690/4 "2021-02-26T17:47:38Z")

</div>

> Hello Everyone,  
> When I use the regex\_search in tasks, it is adding single quotes in the starting and end.  
> Like for this -  
> - sw01 GigabitEthernet0/0 IF-MIB.ifSpeed = 1000000000  
> when I use regex\_search-  
> - name: SET FACT - Interface SPEED -1  
> set\_fact:  
> speeds: "{{ item.0 | regex\_search('[^=]\*$') | regex\_replace(' ', '') }}"
> 
> Converting above value to -  
> ok: [localhost] =\> (item=['sw01 GigabitEthernet0/0 IF-MIB.ifSpeed = 1000000000'])
> 
> My goal is fetch the value after '='  
> sw01 GigabitEthernet0/0 IF-MIB.ifSpeed = 1000000000  
> to  
> 1000000000
> 
> please advise.

The regex\_search filter is superfluous in this case, try:

&nbsp;&nbsp;&nbsp;regex\_replace('^.\*=(.\*)$', '\\1')

\\1 references the part of the string matched by (.\*).

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Pravin\_V](https://avatars.discourse-cdn.com/v4/letter/p/c68b51/32.png) [@Pravin\_V](https://forum.ansible.com/u/Pravin_V)\
**Post date:** [March 1, 2021, 7:14pm UTC](https://forum.ansible.com/t/adding-single-quotes-when-i-use-regex-search/34690/5 "2021-03-01T19:14:35Z")

</div>

Thank you so much, Racke.  
It worked like a charm. I was using it with rest API in JSON format. When I debug, it was adding single quotes(‘) but when I passed the command in URI module as you advised, it was not adding single quotes (’).

Thanks once again!!!

---

<div class="post-metadata">

**Author:** ![Pravin\_V](https://avatars.discourse-cdn.com/v4/letter/p/c68b51/32.png) [@Pravin\_V](https://forum.ansible.com/u/Pravin_V)\
**Post date:** [March 3, 2021, 12:54pm UTC](https://forum.ansible.com/t/adding-single-quotes-when-i-use-regex-search/34690/6 "2021-03-03T12:54:44Z")

</div>

Hello Racke,  
I’m trying to pass the numeric values of an variable into JSON.

- name: Device ID  
set\_fact:  
“device\_id”: “{{ device\_item\_netbox.id }}”

- name: DEBUG - DEVICE ID  
debug: msg=“{{ device\_id }}”

Output -

TASK [DEBUG - DEVICE ID] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\>  
msg: ‘3483’

When I try to pass the same into JSON body-

- name: Create Interface - Netbox  
uri:  
url: “{{ netbox\_url }}/api/dcim/interfaces/”  
return\_content: yes  
method: POST  
validate\_certs: false  
status\_code: 200,201  
headers:  
Authorization: “Token {{ netbox\_server\_token }}”  
body\_format: json  
body:  
{  
“device”: “{{ device\_id }}”,  
“name”: “GigabitEthernet0/0”,  
“mac\_address”: “0001e896655b”,  
“type”: “10gbase-t”,  
“enabled”: “true”,  
“description”: “GigabitEthernet0/0”  
}

Verbose output -

fatal: [localhost]: FAILED! =\> changed=false  
allow: GET, POST, HEAD, OPTIONS, TRACE  
api\_version: ‘2.8’  
content: ‘{“non\_field\_errors”:[“The fields device, name must make a unique set.”]}’  
content\_length: ‘72’  
content\_type: application/json  
elapsed: 0  
invocation:  
module\_args:  
attributes: null  
body:  
description: GigabitEthernet0/0  
device: ‘3483’  
enabled: ‘true’  
mac\_address: 0001e896655b  
name: GigabitEthernet0/0  
type: 10gbase-t

But when I add something with variables like below, it removes adding quotes -

body:  
{  
“device”: “{{ device\_id }} m”,

Verbose output -

module\_args:  
attributes: null  
body:  
description: GigabitEthernet0/0  
device: 3483 m

Please advise how can I fetch/pass only numeric digits without quotes.

---

<div class="post-metadata">

**Author:** ![Pushparaj\_Gothandam](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/pushparaj_gothandam/32/5538_2.png) [@Pushparaj\_Gothandam](https://forum.ansible.com/u/Pushparaj_Gothandam)\
**Post date:** [March 4, 2021, 5:54am UTC](https://forum.ansible.com/t/adding-single-quotes-when-i-use-regex-search/34690/7 "2021-03-04T05:54:43Z")

</div>

Try something like this  
“device”: “{{ device\_id | int }}”
