Ansible 评估制表符分隔字符串列表

Ansible 评估制表符分隔字符串列表

我正在运行一个命令,之后 stdout 被捕获到一个变量中。命令输出列出了由制表符分隔的值。所以我有类似的东西

var.stdout_lines: [
  "a1\tb1\tc1",
  "a2\tb2\tc2",
  "a3\tb3\tc3"
]

为了进一步处理,我想将这些行分成列表,例如

[
  [a1, b1, c1],
  [a2, b2, c2],
  [a3, b3, c3]
]

我怎样才能做到这一点?

答案1

拆分项目

result: "{{ out.stdout_lines | map('split') }}"

  • 用于测试的完整剧本示例
- hosts: localhost

  vars:

    out:
      stdout_lines: [
        "a1\tb1\tc1",
        "a2\tb2\tc2",
        "a3\tb3\tc3"]

    result: "{{ out.stdout_lines | map('split') }}"

  tasks:

    - debug:
        var: out.stdout_lines

    - debug:
        var: result

(env) > cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=23.10
DISTRIB_CODENAME=mantic
DISTRIB_DESCRIPTION="Ubuntu 23.10"
(env) > python3 -m pip list | grep ansible
ansible                   9.5.1
ansible-compat            4.1.11
ansible-core              2.16.6
ansible-lint              24.2.2
ansible-runner            2.3.6

相关内容