在ansible中将调试变量输出拆分为两个单独的变量

山吉本

我正在使用以下代码片段,其中将打印图像详细信息:

- set_fact:
    image_name: "{{ load.results|map(attribute='stdout_lines')|list }}"

- debug:
    var: image_name

输出:

TASK [set_fact] ***************************************************************************************************************************************************************************
ok: [xx.xx.xx.xx]

TASK [debug] ******************************************************************************************************************************************************************************
ok: [xx.xx.xx.xx] => {
  "image_name": [
    [
        "Loaded image(s): localhost/cim:v1.5"
    ],
    [
        "Loaded image(s): localhost/cim:v1.8"
    ]
  ]
}

有没有一种方法可以将图像名称和标签存储在set_fact其自身下的两个单独变量中或以任何其他形式存储,以便我可以将这两个变量重用于下一个任务?

β.εηοιτ.βε

您可以使用regex_findall过滤器来实现此目的。

这里使用的正则表达式是(\S*):(\S+). 如果需要,可以在此处找到更多解释

鉴于剧本:

- hosts: all
  gather_facts: no
  vars:
    load:
      results:
        - stdout_lines:
          - "Loaded image(s): localhost/cim:v1.5"
        - stdout_lines:
          - "Loaded image(s): localhost/cim:v1.8"
      
  tasks:
    - set_fact:
        images: "{{ images | default([]) + item | regex_findall('(\\S*):(\\S+)') }}"
      loop: "{{ load.results | map(attribute='stdout_lines') | flatten }}"
  
    - debug:
        msg: "This image repository is `{{ item.0 }}` and its tag is `{{ item.1 }}`"
      loop: "{{ images }}"

这产生了回顾:

PLAY [all] *********************************************************************************************************

TASK [set_fact] ****************************************************************************************************
ok: [localhost] => (item=Loaded image(s): localhost/cim:v1.5)
ok: [localhost] => (item=Loaded image(s): localhost/cim:v1.8)

TASK [debug] *******************************************************************************************************
ok: [localhost] => (item=['localhost/cim', 'v1.5']) => {
    "msg": "This image repository is `localhost/cim` and its tag is `v1.5`"
}
ok: [localhost] => (item=['localhost/cim', 'v1.8']) => {
    "msg": "This image repository is `localhost/cim` and its tag is `v1.8`"
}

PLAY RECAP *********************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在C#中将字符串拆分为两个单独的变量

将输出拆分为两个变量

C中将字符串拆分为两个变量

如何在 TypeScript 中将一个字符串拆分为两个变量?

如何在R中将单个列中的内容拆分为两个单独的列?

在R中将一个变量拆分为多个变量

将Integer拆分为两个单独的Integer

将YAML文件拆分为两个单独的文件

列拆分为两个单独的列

在 R 中将日期时间列(包含一个字符)拆分为两个单独的列

在python中将列表分为两个单独的列表

我可以将这个双变量 for 循环拆分为两个嵌套循环吗?

将 jquery 的 Daterange 值拆分为两个 PHP 变量

将字符串(从随机行)拆分为两个变量

用逗号将模型的 CharField 拆分为两个变量

Python3,从txt文件中逐行读取并将行拆分为两个变量

使用 NodeJS 将字符串拆分为两个变量

在python中将数组拆分为两个较小的数组

在C中将数组拆分为两个

Ansible 将字符串拆分为 n 个变量

R:将变量级别分为两个变量

Linux将数组拆分为单独的变量

在保存在csv之前将一个单元格数据拆分为两个变量

在JavaScript中将单个字符串拆分为多个单独的变量

如何在PHP中将单词字符串拆分为单独的字符串变量?

将日期和时间变量分为两个单独的变量,一个日期和另一个时间

一个变量返回两个单独的数组

如何在SQL Server 2014中将结果从一个字段拆分为两个单独的字段

如何在MATLAB中将一个单元格元素拆分为两个单独的单元格元素