Need Help in Ansible regex

Hi all,

I have a rpm whcich i have added in a list. I want to make it dynamic i.e when the version of rpm changes playbook should handle it.
I have build a logic but the playbook skips the task where i have mentioned the regex. Kindly help me .

  • hosts: localhost
    tasks:

  • name: Create a list
    set_fact:
    some_value:

  • dp-steps-common-3.0.12.rpm

  • debug:
    msg: “{{ some_value }}”

  • name: Print DP steps records
    set_fact:
    dp_record: “{{ item }}”
    when: item is defined and item == ‘regexp (dp-steps-common-[0-9./]+[0-9./]+[0-9][0-9./]rpm$)’
    with_items: “{{ some_value }}”

  • name: Create an empty list variable
    set_fact:
    nodes1:

  • name: Append string to ECE node list
    set_fact:
    nodes1: “{{ ece_nodes + [ item ] }}”
    with_items:

  • “{{ dp_record | default(None) }}”
    when: item is defined and item != None and item != “”

  • name: Display Lists
    debug:
    msg:

  • “{{ nodes1 }}”

OUTput:

PLAY [localhost] ****************************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************
ok: [localhost]

TASK [Create a list] ************************************************************************************************************************************************************************
ok: [localhost]

TASK [debug] ********************************************************************************************************************************************************************************
ok: [localhost] => {
“msg”: [
“dp-steps-common-3.0.12.rpm”
]
}

TASK [Print DP steps records] ***************************************************************************************************************************************************************
skipping: [localhost] => (item=dp-steps-common-3.0.12.rpm)

TASK [Create an empty list variable] ********************************************************************************************************************************************************
ok: [localhost]

TASK [Append string to ECE node list] *******************************************************************************************************************************************************
skipping: [localhost] => (item=)

TASK [Display Lists] ************************************************************************************************************************************************************************
ok: [localhost] => {
“msg”: [

]

Hi

Not quite sure what you are trying to do with the slash (“/”) symbol in your regex. If you want to use it to escape the dot (“.”) sign then you need to use a backslash ("") instead of a slash and you need to put it before the dot not after it.

So try this: dp-steps-common-[0-9.]+[0-9.]+[0-9][0-9.]rpm$

Also there is a quite handy webpage that helps you test your regex. Just visit regex101.com and type your regex in the small field on top and the string you want to capture with your regex in the large field below. Then you can play around with your regex and the webpage will tell you which part of your regex captured which part of the string.

Best,
Cyril

Hi all,

I have a rpm whcich i have added in a list. I want to make it dynamic
i.e when the version of rpm changes playbook should handle it.
I have build a logic but the playbook skips the task where i have
mentioned the regex. Kindly help me .

- hosts: localhost
tasks:
- name: Create a list
set_fact:
some_value:
- dp-steps-common-3.0.12.rpm

- debug:
msg: "{{ some_value }}"

- name: Print DP steps records
set_fact:
dp_record: "{{ item }}"
when: item is defined and item == 'regexp
(dp-steps-common-[0-9./]+[0-9./]+[0-9][0-9./]rpm$)'

In the first part, item will always be defined or the task won't run at all, so no need to test it.

In the second part, you're comparing two static strings here that will never match. You probably meant to do something like

when: item is regex('your regex')

See for examples: https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#testing-strings

V/r,
James Cassell