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 ) }}
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"
]
}