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