Adding single quotes when I use regex_search('[^=]*$')

289 views
Skip to first unread message

Pravin V

unread,
Feb 26, 2021, 11:12:46 AM2/26/21
to Ansible Project
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.

Roberto Paz

unread,
Feb 26, 2021, 11:43:31 AM2/26/21
to Ansible Project
Maybe you can use split.

item.split("=")[1]

should refer to what is at the right of "=".

Pravin V

unread,
Feb 26, 2021, 11:54:42 AM2/26/21
to Ansible Project
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'

Stefan Hornburg (Racke)

unread,
Feb 26, 2021, 12:47:58 PM2/26/21
to ansible...@googlegroups.com
The regex_search filter is superfluous in this case, try:

regex_replace('^.*=(.*)$', '\\1')

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

Regards
Racke

>
> --
> 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 <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/4c18f52b-b989-4dca-945a-5272c8e6d60cn%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/4c18f52b-b989-4dca-945a-5272c8e6d60cn%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

OpenPGP_signature

Pravin V

unread,
Mar 1, 2021, 2:14:35 PM3/1/21
to Ansible Project
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!!!

Pravin V

unread,
Mar 3, 2021, 7:54:44 AM3/3/21
to Ansible Project
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.

Rajthecomputerguy

unread,
Mar 4, 2021, 12:55:32 AM3/4/21
to ansible...@googlegroups.com
Try something like this
"device": "{{ device_id   |  int }}"



To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d01fd39a-005d-4bd9-89d2-a10aa1bb4a6fn%40googlegroups.com.


--

Thanks,

Pushparaj G


Reply all
Reply to author
Forward
0 new messages