Ansible:根据分隔符拆分字符串

huangapple go评论137阅读模式
英文:

Ansible : Splitting a string based on delimiter

问题

  1. 我在ansible变量中有以下字符串,xyz
  2. xyz = " /u01/dbbackup_nfs/orabackups/odaserver-c/database/857970682/akash_2/db"
  3. 我想将这个字符串分割,直到停在orabackups,然后将变量abc的值设置为如下。
  4. ```yaml
  5. set_fact:
  6. abc: " /u01/dbbackup_nfs"

对此的任何建议将非常有帮助。

  1. <details>
  2. <summary>英文:</summary>
  3. I have the following string in an ansible variable, xyz

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

  1. 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"

  1. Any suggestions on this would be really helpful
  2. </details>
  3. # 答案1
  4. **得分**: 1
  5. 以下是翻译的部分:
  6. ```yaml
  7. abc: &quot;/{{ xyz.split(&#39;/&#39;)[1:3]|join(&#39;/&#39;) }}&quot;

给出了你想要的结果

  1. abc: /u01/dbbackup_nfs

<hr>

<sup>

Q: "我需要从第一个斜杠(/)开始,所以我将其更改为xyz.split(&#39;/&#39;)[0:3]|join(&#39;/&#39;)"

A: 这取决于路径是绝对路径还是相对路径。如果不确定,可以使用条件语句

  1. abc: |
  2. {% if xyz[0] == &#39;/&#39; %}
  3. /{{ xyz.split(&#39;/&#39;)[1:3]|join(&#39;/&#39;) }}
  4. {% else %}
  5. /{{ xyz.split(&#39;/&#39;)[0:2]|join(&#39;/&#39;) }}
  6. {% endif %}

用于测试的Playbook

  1. - hosts: localhost
  2. vars:
  3. abc: |
  4. {% if xyz[0] == &#39;/&#39; %}
  5. /{{ xyz.split(&#39;/&#39;)[1:3]|join(&#39;/&#39;) }}
  6. {% else %}
  7. /{{ xyz.split(&#39;/&#39;)[0:2]|join(&#39;/&#39;) }}
  8. {% endif %}
  9. tasks:
  10. - debug:
  11. msg: &quot;{{ xyz.split(&#39;/&#39;)|to_yaml }}&quot;
  12. vars:
  13. xyz: &quot;u01/dbbackup_nfs/orabackups/odaserver-c/database/857970682/akash_2/db&quot;
  14. - debug:
  15. msg: &quot;{{ xyz.split(&#39;/&#39;)|to_yaml }}&quot;
  16. vars:
  17. xyz: &quot;/u01/dbbackup_nfs/orabackups/odaserver-c/database/857970682/akash_2/db&quot;
  18. - debug:
  19. var: abc
  20. vars:
  21. xyz: &quot;u01/dbbackup_nfs/orabackups/odaserver-c/database/857970682/akash_2/db&quot;
  22. - debug:
  23. var: abc
  24. vars:
  25. xyz: &quot;/u01/dbbackup_nfs/orabackups/odaserver-c/database/857970682/akash_2/db&quot;

给出了

  1. LAY [localhost] ******************************************************************************
  2. TASK [debug] **********************************************************************************
  3. ok: [localhost] =&gt;
  4. msg: |-
  5. [u01, dbbackup_nfs, orabackups, odaserver-c, database, &#39;857970682&#39;, akash_2, db]
  6. TASK [debug] **********************************************************************************
  7. ok: [localhost] =&gt;
  8. msg: |-
  9. [&#39;&#39;, u01, dbbackup_nfs, orabackups, odaserver-c, database, &#39;857970682&#39;, akash_2, db]
  10. TASK [debug] **********************************************************************************
  11. ok: [localhost] =&gt;
  12. abc: |-
  13. /u01/dbbackup_nfs
  14. TASK [debug] **********************************************************************************
  15. ok: [localhost] =&gt;
  16. abc: |-
  17. /u01/dbbackup_nfs
  18. PLAY RECAP ************************************************************************************
  19. localhost: ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

</sup>

英文:

The declaration below

  1. abc: &quot;/{{ xyz.split(&#39;/&#39;)[1:3]|join(&#39;/&#39;) }}&quot;

gives what you want

  1. abc: /u01/dbbackup_nfs

<hr>

<sup>

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

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

  1. abc: |
  2. {% if xyz[0] == &#39;/&#39; %}
  3. /{{ xyz.split(&#39;/&#39;)[1:3]|join(&#39;/&#39;) }}
  4. {% else %}
  5. /{{ xyz.split(&#39;/&#39;)[0:2]|join(&#39;/&#39;) }}
  6. {% endif %}

The playbook for testing

  1. - hosts: localhost
  2. vars:
  3. abc: |
  4. {% if xyz[0] == &#39;/&#39; %}
  5. /{{ xyz.split(&#39;/&#39;)[1:3]|join(&#39;/&#39;) }}
  6. {% else %}
  7. /{{ xyz.split(&#39;/&#39;)[0:2]|join(&#39;/&#39;) }}
  8. {% endif %}
  9. tasks:
  10. - debug:
  11. msg: &quot;{{ xyz.split(&#39;/&#39;)|to_yaml }}&quot;
  12. vars:
  13. xyz: &quot;u01/dbbackup_nfs/orabackups/odaserver-c/database/857970682/akash_2/db&quot;
  14. - debug:
  15. msg: &quot;{{ xyz.split(&#39;/&#39;)|to_yaml }}&quot;
  16. vars:
  17. xyz: &quot;/u01/dbbackup_nfs/orabackups/odaserver-c/database/857970682/akash_2/db&quot;
  18. - debug:
  19. var: abc
  20. vars:
  21. xyz: &quot;u01/dbbackup_nfs/orabackups/odaserver-c/database/857970682/akash_2/db&quot;
  22. - debug:
  23. var: abc
  24. vars:
  25. xyz: &quot;/u01/dbbackup_nfs/orabackups/odaserver-c/database/857970682/akash_2/db&quot;

gives

  1. LAY [localhost] ******************************************************************************
  2. TASK [debug] **********************************************************************************
  3. ok: [localhost] =&gt;
  4. msg: |-
  5. [u01, dbbackup_nfs, orabackups, odaserver-c, database, &#39;857970682&#39;, akash_2, db]
  6. TASK [debug] **********************************************************************************
  7. ok: [localhost] =&gt;
  8. msg: |-
  9. [&#39;&#39;, u01, dbbackup_nfs, orabackups, odaserver-c, database, &#39;857970682&#39;, akash_2, db]
  10. TASK [debug] **********************************************************************************
  11. ok: [localhost] =&gt;
  12. abc: |-
  13. /u01/dbbackup_nfs
  14. TASK [debug] **********************************************************************************
  15. ok: [localhost] =&gt;
  16. abc: |-
  17. /u01/dbbackup_nfs
  18. PLAY RECAP ************************************************************************************
  19. 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过滤器来展示它的工作原理

  1. ---
  2. - hosts: localhost
  3. become: false
  4. gather_facts: false
  5. vars:
  6. PATH_TO_SPLIT: &quot;/user/folder/files/db&quot;
  7. tasks:
  8. - name: Split path
  9. debug:
  10. msg: &quot;{{ PATH_TO_SPLIT | split (&#39;/&#39;) }}&quot;
  11. - name: Constructed path
  12. debug:
  13. msg: &quot;/{{ PATH_TO_SPLIT.split(&#39;/&#39;)[1] }}/{{ PATH_TO_SPLIT.split(&#39;/&#39;)[2] }}&quot;

will result into an output of
将产生以下输出

  1. TASK [Split path] ******
  2. ok: [localhost] =&gt;
  3. msg:
  4. - &#39;&#39;
  5. - user
  6. - folder
  7. - files
  8. - db
  9. TASK [Constructed path] ******
  10. ok: [localhost] =&gt;
  11. msg: /user/folder

Further Documenation and Q&A
更多文档和问答

英文:

> I want to split this string ...

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

  1. ---
  2. - hosts: localhost
  3. become: false
  4. gather_facts: false
  5. vars:
  6. PATH_TO_SPLIT: &quot;/user/folder/files/db&quot;
  7. tasks:
  8. - name: Split path
  9. debug:
  10. msg: &quot;{{ PATH_TO_SPLIT | split (&#39;/&#39;) }}&quot;
  11. - name: Constructed path
  12. debug:
  13. msg: &quot;/{{ PATH_TO_SPLIT.split(&#39;/&#39;)[1] }}/{{ PATH_TO_SPLIT.split(&#39;/&#39;)[2] }}&quot;

will result into an output of

  1. TASK [Split path] ******
  2. ok: [localhost] =&gt;
  3. msg:
  4. - &#39;&#39;
  5. - user
  6. - folder
  7. - files
  8. - db
  9. TASK [Constructed path] *******
  10. ok: [localhost] =&gt;
  11. msg: /user/folder

Further Documenation and Q&A

huangapple
  • 本文由 发表于 2023年7月10日 19:36:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653347.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定