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

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

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: &quot;/{{ xyz.split(&#39;/&#39;)[1:3]|join(&#39;/&#39;) }}&quot;

给出了你想要的结果

  abc: /u01/dbbackup_nfs

<hr>

<sup>

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

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

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

用于测试的Playbook

- hosts: localhost

  vars:

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

  tasks:

    - debug:
        msg: &quot;{{ xyz.split(&#39;/&#39;)|to_yaml }}&quot;
      vars:
        xyz: &quot;u01/dbbackup_nfs/orabackups/odaserver-c/database/857970682/akash_2/db&quot;

    - debug:
        msg: &quot;{{ xyz.split(&#39;/&#39;)|to_yaml }}&quot;
      vars:
        xyz: &quot;/u01/dbbackup_nfs/orabackups/odaserver-c/database/857970682/akash_2/db&quot;

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

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

给出了

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

TASK [debug] **********************************************************************************
ok: [localhost] =&gt; 
  msg: |-
        [u01, dbbackup_nfs, orabackups, odaserver-c, database, &#39;857970682&#39;, akash_2, db]

TASK [debug] **********************************************************************************
ok: [localhost] =&gt; 
  msg: |-
        [&#39;&#39;, u01, dbbackup_nfs, orabackups, odaserver-c, database, &#39;857970682&#39;, akash_2, db]

TASK [debug] **********************************************************************************
ok: [localhost] =&gt; 
  abc: |-
        /u01/dbbackup_nfs

TASK [debug] **********************************************************************************
ok: [localhost] =&gt; 
  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: &quot;/{{ xyz.split(&#39;/&#39;)[1:3]|join(&#39;/&#39;) }}&quot;

gives what you want

  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

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

The playbook for testing

- hosts: localhost

  vars:

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

  tasks:

    - debug:
        msg: &quot;{{ xyz.split(&#39;/&#39;)|to_yaml }}&quot;
      vars:
        xyz: &quot;u01/dbbackup_nfs/orabackups/odaserver-c/database/857970682/akash_2/db&quot;

    - debug:
        msg: &quot;{{ xyz.split(&#39;/&#39;)|to_yaml }}&quot;
      vars:
        xyz: &quot;/u01/dbbackup_nfs/orabackups/odaserver-c/database/857970682/akash_2/db&quot;

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

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

gives

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

TASK [debug] **********************************************************************************
ok: [localhost] =&gt; 
  msg: |-
        [u01, dbbackup_nfs, orabackups, odaserver-c, database, &#39;857970682&#39;, akash_2, db]

TASK [debug] **********************************************************************************
ok: [localhost] =&gt; 
  msg: |-
        [&#39;&#39;, u01, dbbackup_nfs, orabackups, odaserver-c, database, &#39;857970682&#39;, akash_2, db]

TASK [debug] **********************************************************************************
ok: [localhost] =&gt; 
  abc: |-
        /u01/dbbackup_nfs

TASK [debug] **********************************************************************************
ok: [localhost] =&gt; 
  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: &quot;/user/folder/files/db&quot;

  tasks:

  - name: Split path
    debug:
      msg: &quot;{{ PATH_TO_SPLIT | split (&#39;/&#39;) }}&quot;

  - name: Constructed path
    debug:
      msg: &quot;/{{ PATH_TO_SPLIT.split(&#39;/&#39;)[1] }}/{{ PATH_TO_SPLIT.split(&#39;/&#39;)[2] }}&quot;

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

TASK [Split path] ******
ok: [localhost] =&gt;
  msg:
  - &#39;&#39;
  - user
  - folder
  - files
  - db

TASK [Constructed path] ******
ok: [localhost] =&gt;
  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

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

  vars:

    PATH_TO_SPLIT: &quot;/user/folder/files/db&quot;

  tasks:

  - name: Split path
    debug:
      msg: &quot;{{ PATH_TO_SPLIT | split (&#39;/&#39;) }}&quot;

  - name: Constructed path
    debug:
      msg: &quot;/{{ PATH_TO_SPLIT.split(&#39;/&#39;)[1] }}/{{ PATH_TO_SPLIT.split(&#39;/&#39;)[2] }}&quot;

will result into an output of

TASK [Split path] ******
ok: [localhost] =&gt;
  msg:
  - &#39;&#39;
  - user
  - folder
  - files
  - db

TASK [Constructed path] *******
ok: [localhost] =&gt;
  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:

确定