从环境ID列表为空的”versions”中提取”version” – Jinja和Ansible

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

Jinja and Ansible - Extract "version" from "versions" where environment_ids list is empty

问题

我试图从其中一个版本环境ID列表为空的项目中获取版本文本。这是用于Foreman内容视图版本删除的。以下是一个示例数据集,但无论如何,我都无法获取它。

由于示例数据主要是代码,stackoverflow要求提供更多详细信息。

除此之外,在播放本中,我将使用Ansible社区集合theforeman.foreman.content_view_version来删除旧的未使用版本。

示例数据:

item:
- auto_publish: false
  component_ids:
  - 10
  - 11
  environments:
  - activation_keys:
    - 1
    hosts: []
    id: 1
    label: Library
    name: Library
    permissions:
      readable: true
  - activation_keys: []
    hosts: []
    id: 2
    label: DEV
    name: DEV
    permissions:
      readable: true
  - activation_keys: []
    hosts: []
    id: 3
    label: PROD
    name: PROD
    permissions:
      readable: true
  generated_for: none
  hosts: []
  id: 4
  import_only: false
  label: system-tester
  last_published: 2023-07-17 14:15:24 UTC
  name: comp Repo (noarch)
  version_count: 2
  versions:
  - environment_ids:
    - 1
    - 2
    - 3
    id: 9
    published: 2023-07-17 14:03:01 UTC
    version: '2.0'
  - environment_ids:
    - 1
    - 2
    - 3
    id: 12
    published: 2023-07-17 14:15:24 UTC
    version: '2.1'
- auto_publish: false
  component_ids:
  - 10
  - 11
  environments:
  - activation_keys:
    - 1
    hosts: []
    id: 1
    label: Library
    name: Library
    permissions:
      readable: true
  - activation_keys: []
    hosts: []
    id: 2
    label: DEV
    name: DEV
    permissions:
      readable: true
  - activation_keys: []
    hosts: []
    id: 3
    label: PROD
    name: PROD
    permissions:
      readable: true
  generated_for: none
  hosts: []
  id: 4
  import_only: false
  label: system-tester2
  last_published: 2023-07-17 14:15:24 UTC
  name: comp Repo (noarch)
  version_count: 2
  versions:
  - environment_ids: []
    id: 9
    published: 2023-07-17 14:03:01 UTC
    version: '2.0'
  - environment_ids:
    - 1
    - 2
    - 3
    id: 12
    published: 2023-07-17 14:15:24 UTC
    version: '2.1'
- activation_keys:
  - id: 3
    name: lx
  auto_publish: false
  component_ids:
  - 10
  - 11
  environments:
  - activation_keys:
    - 1
    hosts: []
    id: 1
    label: Library
    name: Library
    permissions:
      readable: true
  - activation_keys: []
    hosts: []
    id: 2
    label: DEV
    name: DEV
    permissions:
      readable: true
  - activation_keys: []
    hosts: []
    id: 3
    label: PROD
    name: PROD
    permissions:
      readable: true
  generated_for: none
  hosts: []
  id: 4
  import_only: false
  label: system-tester3
  last_published: 2023-07-17 14:15:24 UTC
  name: comp Repo (noarch)
  version_count: 2
  versions:
  - environment_ids: []
    id: 9
    published: 2023-07-17 14:03:01 UTC
    version: '2.0'
  - environment_ids:
    - 1
    - 2
    - 3
    id: 12
    published: 2023-07-17 14:15:24 UTC
    version: '2.1'

{{ (item.resources| map(attribute='version') | list ) }}

英文:

I'm trying to get the version text from one of these items where the versions environment_ids list is empty. This is for foreman content-view version deletion. Below is a sample dataset and for the life of me I can't get it.

Since the sample data is mostly code, stackoverflow is asking for more details.

Beyond this, in the playbook I'll be using the ansible community collection theforeman.foreman.content_view_version do delete the older unused versions.

Sample Data:

item:
- auto_publish: false
  component_ids:
  - 10
  - 11
  environments:
  - activation_keys:
    - 1
    hosts: []
    id: 1
    label: Library
    name: Library
    permissions:
      readable: true
  - activation_keys: []
    hosts: []
    id: 2
    label: DEV
    name: DEV
    permissions:
      readable: true
  - activation_keys: []
    hosts: []
    id: 3
    label: PROD
    name: PROD
    permissions:
      readable: true
  generated_for: none
  hosts: []
  id: 4
  import_only: false
  label: system-tester
  last_published: 2023-07-17 14:15:24 UTC
  name: comp Repo (noarch)
  version_count: 2
  versions:
  - environment_ids:
    - 1
    - 2
    - 3
    id: 9
    published: 2023-07-17 14:03:01 UTC
    version: '2.0'
  - environment_ids:
    - 1
    - 2
    - 3
    id: 12
    published: 2023-07-17 14:15:24 UTC
    version: '2.1'
- auto_publish: false
  component_ids:
  - 10
  - 11
  environments:
  - activation_keys:
    - 1
    hosts: []
    id: 1
    label: Library
    name: Library
    permissions:
      readable: true
  - activation_keys: []
    hosts: []
    id: 2
    label: DEV
    name: DEV
    permissions:
      readable: true
  - activation_keys: []
    hosts: []
    id: 3
    label: PROD
    name: PROD
    permissions:
      readable: true
  generated_for: none
  hosts: []
  id: 4
  import_only: false
  label: system-tester2
  last_published: 2023-07-17 14:15:24 UTC
  name: comp Repo (noarch)
  version_count: 2
  versions:
  - environment_ids: []
    id: 9
    published: 2023-07-17 14:03:01 UTC
    version: '2.0'
  - environment_ids:
    - 1
    - 2
    - 3
    id: 12
    published: 2023-07-17 14:15:24 UTC
    version: '2.1'
- activation_keys:
  - id: 3
    name: lx
  auto_publish: false
  component_ids:
  - 10
  - 11
  environments:
  - activation_keys:
    - 1
    hosts: []
    id: 1
    label: Library
    name: Library
    permissions:
      readable: true
  - activation_keys: []
    hosts: []
    id: 2
    label: DEV
    name: DEV
    permissions:
      readable: true
  - activation_keys: []
    hosts: []
    id: 3
    label: PROD
    name: PROD
    permissions:
      readable: true
  generated_for: none
  hosts: []
  id: 4
  import_only: false
  label: system-tester3
  last_published: 2023-07-17 14:15:24 UTC
  name: comp Repo (noarch)
  version_count: 2
  versions:
  - environment_ids: []
    id: 9
    published: 2023-07-17 14:03:01 UTC
    version: '2.0'
  - environment_ids:
    - 1
    - 2
    - 3
    id: 12
    published: 2023-07-17 14:15:24 UTC
    version: '2.1'

{{ (item.resources| map(attribute='version') | list ) }}

答案1

得分: 2

在您的示例数据中有两个项目,其中environment_ids为空列表。我们可以使用JMESPath表达式来提取版本文本,如下所示:

- debug:
    msg: "{{ item|json_query('[].versions[?!environment_ids].version[]') }}"

根据您的示例数据,上述的debug任务产生以下输出:

任务 [debug] ********************************************************************************************
ok: [localhost] => {
    "msg": [
        "2.0",
        "2.0"
    ]
}
英文:

There are two items in your sample data for which environment_ids is an empty list. We can use a JMESPath expression to extract the version text, like this:

- debug:
    msg: "{{ item|json_query('[].versions[?!environment_ids].version[]') }}"

Given your sample data, the above debug tasks produces the output:

TASK [debug] ********************************************************************************************
ok: [localhost] => {
    "msg": [
        "2.0",
        "2.0"
    ]
}

huangapple
  • 本文由 发表于 2023年7月18日 01:29:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706821.html
匿名

发表评论

匿名网友

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

确定