1

I have the following string in an ansible variable, xyz

xyz = "/u01/dbbackup_nfs/orabackups/odaserver-c/database/857970682/akash_2/db"

I want to split this string such that I stop at orabackups and do a set_fact of the variable abc , such that abc will have the following value.

set_fact:
  abc: "/u01/dbbackup_nfs"

Any suggestions on this would be really helpful

Akash-BLR
  • 63
  • 4

2 Answers2

1

The declaration below

  abc: "/{{ xyz.split('/')[1:3]|join('/') }}"

gives what you want

  abc: /u01/dbbackup_nfs

Q: "I needed from the first slash(/), so made it xyz.split('/')[0:3]|join('/')"

A: This depends on whether the path is absolute or relative. You can use a condition if you're not sure

  abc: |
    {% if xyz[0] == '/' %}
    /{{ xyz.split('/')[1:3]|join('/') }}
    {% else %}
    /{{ xyz.split('/')[0:2]|join('/') }}
    {% endif %}

The playbook for testing

- hosts: localhost

  vars:

    abc: |
      {% if xyz[0] == '/' %}
      /{{ xyz.split('/')[1:3]|join('/') }}
      {% else %}
      /{{ xyz.split('/')[0:2]|join('/') }}
      {% endif %}

  tasks:

    - debug:
        msg: "{{ xyz.split('/')|to_yaml }}"
      vars:
        xyz: "u01/dbbackup_nfs/orabackups/odaserver-c/database/857970682/akash_2/db"

    - debug:
        msg: "{{ xyz.split('/')|to_yaml }}"
      vars:
        xyz: "/u01/dbbackup_nfs/orabackups/odaserver-c/database/857970682/akash_2/db"

    - debug:
        var: abc
      vars:
        xyz: "u01/dbbackup_nfs/orabackups/odaserver-c/database/857970682/akash_2/db"

    - debug:
        var: abc
      vars:
        xyz: "/u01/dbbackup_nfs/orabackups/odaserver-c/database/857970682/akash_2/db"

gives

LAY [localhost] ******************************************************************************

TASK [debug] **********************************************************************************
ok: [localhost] => 
  msg: |-
    [u01, dbbackup_nfs, orabackups, odaserver-c, database, '857970682', akash_2, db]

TASK [debug] **********************************************************************************
ok: [localhost] => 
  msg: |-
    ['', u01, dbbackup_nfs, orabackups, odaserver-c, database, '857970682', akash_2, db]

TASK [debug] **********************************************************************************
ok: [localhost] => 
  abc: |-
    /u01/dbbackup_nfs

TASK [debug] **********************************************************************************
ok: [localhost] => 
  abc: |-
    /u01/dbbackup_nfs

PLAY RECAP ************************************************************************************
localhost: ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • 1
    Thanks Vladir, I needed from the first slash(/), so made it xyz.split('/')[0:3]|join('/') and it works well.. – Akash-BLR Jul 10 '23 at 11:54
  • You've said you needed the first **two** elements of the path `/u01/dbbackup_nfs`. Yet `[0:3]` comprises **three** elements. – Vladimir Botka Jul 10 '23 at 14:10
0

I want to split this string ...

A minimal example playbook utilizing the split filter in order to show how it works

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    PATH_TO_SPLIT: "/user/folder/files/db"

  tasks:

  - name: Split path
    debug:
      msg: "{{ PATH_TO_SPLIT | split ('/') }}"

  - name: Constructed path
    debug:
      msg: "/{{ PATH_TO_SPLIT.split('/')[1] }}/{{ PATH_TO_SPLIT.split('/')[2] }}"

will result into an output of

TASK [Split path] ******
ok: [localhost] =>
  msg:
  - ''
  - user
  - folder
  - files
  - db

TASK [Constructed path] *******
ok: [localhost] =>
  msg: /user/folder

Further Documenation and Q&A

U880D
  • 8,601
  • 6
  • 24
  • 40