英文:
Ansible : Splitting a string based on delimiter
问题
我在ansible变量中有以下字符串,xyz
xyz = " /u01/dbbackup_nfs/orabackups/odaserver-c/database/857970682/akash_2/db"
我想将这个字符串分割,直到停在orabackups,然后将变量abc的值设置为如下。
```yaml
set_fact:
abc: " /u01/dbbackup_nfs"
对此的任何建议将非常有帮助。
<details>
<summary>英文:</summary>
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
</details>
# 答案1
**得分**: 1
以下是翻译的部分:
```yaml
abc: "/{{ xyz.split('/')[1:3]|join('/') }}"
给出了你想要的结果
abc: /u01/dbbackup_nfs
<hr>
<sup>
Q: "我需要从第一个斜杠(/)开始,所以我将其更改为xyz.split('/')[0:3]|join('/')
"
A: 这取决于路径是绝对路径还是相对路径。如果不确定,可以使用条件语句
abc: |
{% if xyz[0] == '/' %}
/{{ xyz.split('/')[1:3]|join('/') }}
{% else %}
/{{ xyz.split('/')[0:2]|join('/') }}
{% endif %}
用于测试的Playbook
- 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"
给出了
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
</sup>
英文:
The declaration below
abc: "/{{ xyz.split('/')[1:3]|join('/') }}"
gives what you want
abc: /u01/dbbackup_nfs
<hr>
<sup>
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
</sup>
答案2
得分: 0
"I want to split this string ..."
我想要分割这个字符串...
A minimal example playbook utilizing the split
filter in order to show how it works
一个最小的示例playbook,使用split
过滤器来展示它的工作原理
---
- 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
更多文档和问答
split
filter – split a string into a list- How slicing in Python works
split
过滤器 - 将字符串拆分为列表- Python中切片的工作原理
英文:
> 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论