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

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

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

问题

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

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

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

示例数据:

  1. item:
  2. - auto_publish: false
  3. component_ids:
  4. - 10
  5. - 11
  6. environments:
  7. - activation_keys:
  8. - 1
  9. hosts: []
  10. id: 1
  11. label: Library
  12. name: Library
  13. permissions:
  14. readable: true
  15. - activation_keys: []
  16. hosts: []
  17. id: 2
  18. label: DEV
  19. name: DEV
  20. permissions:
  21. readable: true
  22. - activation_keys: []
  23. hosts: []
  24. id: 3
  25. label: PROD
  26. name: PROD
  27. permissions:
  28. readable: true
  29. generated_for: none
  30. hosts: []
  31. id: 4
  32. import_only: false
  33. label: system-tester
  34. last_published: 2023-07-17 14:15:24 UTC
  35. name: comp Repo (noarch)
  36. version_count: 2
  37. versions:
  38. - environment_ids:
  39. - 1
  40. - 2
  41. - 3
  42. id: 9
  43. published: 2023-07-17 14:03:01 UTC
  44. version: '2.0'
  45. - environment_ids:
  46. - 1
  47. - 2
  48. - 3
  49. id: 12
  50. published: 2023-07-17 14:15:24 UTC
  51. version: '2.1'
  52. - auto_publish: false
  53. component_ids:
  54. - 10
  55. - 11
  56. environments:
  57. - activation_keys:
  58. - 1
  59. hosts: []
  60. id: 1
  61. label: Library
  62. name: Library
  63. permissions:
  64. readable: true
  65. - activation_keys: []
  66. hosts: []
  67. id: 2
  68. label: DEV
  69. name: DEV
  70. permissions:
  71. readable: true
  72. - activation_keys: []
  73. hosts: []
  74. id: 3
  75. label: PROD
  76. name: PROD
  77. permissions:
  78. readable: true
  79. generated_for: none
  80. hosts: []
  81. id: 4
  82. import_only: false
  83. label: system-tester2
  84. last_published: 2023-07-17 14:15:24 UTC
  85. name: comp Repo (noarch)
  86. version_count: 2
  87. versions:
  88. - environment_ids: []
  89. id: 9
  90. published: 2023-07-17 14:03:01 UTC
  91. version: '2.0'
  92. - environment_ids:
  93. - 1
  94. - 2
  95. - 3
  96. id: 12
  97. published: 2023-07-17 14:15:24 UTC
  98. version: '2.1'
  99. - activation_keys:
  100. - id: 3
  101. name: lx
  102. auto_publish: false
  103. component_ids:
  104. - 10
  105. - 11
  106. environments:
  107. - activation_keys:
  108. - 1
  109. hosts: []
  110. id: 1
  111. label: Library
  112. name: Library
  113. permissions:
  114. readable: true
  115. - activation_keys: []
  116. hosts: []
  117. id: 2
  118. label: DEV
  119. name: DEV
  120. permissions:
  121. readable: true
  122. - activation_keys: []
  123. hosts: []
  124. id: 3
  125. label: PROD
  126. name: PROD
  127. permissions:
  128. readable: true
  129. generated_for: none
  130. hosts: []
  131. id: 4
  132. import_only: false
  133. label: system-tester3
  134. last_published: 2023-07-17 14:15:24 UTC
  135. name: comp Repo (noarch)
  136. version_count: 2
  137. versions:
  138. - environment_ids: []
  139. id: 9
  140. published: 2023-07-17 14:03:01 UTC
  141. version: '2.0'
  142. - environment_ids:
  143. - 1
  144. - 2
  145. - 3
  146. id: 12
  147. published: 2023-07-17 14:15:24 UTC
  148. 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:

  1. item:
  2. - auto_publish: false
  3. component_ids:
  4. - 10
  5. - 11
  6. environments:
  7. - activation_keys:
  8. - 1
  9. hosts: []
  10. id: 1
  11. label: Library
  12. name: Library
  13. permissions:
  14. readable: true
  15. - activation_keys: []
  16. hosts: []
  17. id: 2
  18. label: DEV
  19. name: DEV
  20. permissions:
  21. readable: true
  22. - activation_keys: []
  23. hosts: []
  24. id: 3
  25. label: PROD
  26. name: PROD
  27. permissions:
  28. readable: true
  29. generated_for: none
  30. hosts: []
  31. id: 4
  32. import_only: false
  33. label: system-tester
  34. last_published: 2023-07-17 14:15:24 UTC
  35. name: comp Repo (noarch)
  36. version_count: 2
  37. versions:
  38. - environment_ids:
  39. - 1
  40. - 2
  41. - 3
  42. id: 9
  43. published: 2023-07-17 14:03:01 UTC
  44. version: '2.0'
  45. - environment_ids:
  46. - 1
  47. - 2
  48. - 3
  49. id: 12
  50. published: 2023-07-17 14:15:24 UTC
  51. version: '2.1'
  52. - auto_publish: false
  53. component_ids:
  54. - 10
  55. - 11
  56. environments:
  57. - activation_keys:
  58. - 1
  59. hosts: []
  60. id: 1
  61. label: Library
  62. name: Library
  63. permissions:
  64. readable: true
  65. - activation_keys: []
  66. hosts: []
  67. id: 2
  68. label: DEV
  69. name: DEV
  70. permissions:
  71. readable: true
  72. - activation_keys: []
  73. hosts: []
  74. id: 3
  75. label: PROD
  76. name: PROD
  77. permissions:
  78. readable: true
  79. generated_for: none
  80. hosts: []
  81. id: 4
  82. import_only: false
  83. label: system-tester2
  84. last_published: 2023-07-17 14:15:24 UTC
  85. name: comp Repo (noarch)
  86. version_count: 2
  87. versions:
  88. - environment_ids: []
  89. id: 9
  90. published: 2023-07-17 14:03:01 UTC
  91. version: '2.0'
  92. - environment_ids:
  93. - 1
  94. - 2
  95. - 3
  96. id: 12
  97. published: 2023-07-17 14:15:24 UTC
  98. version: '2.1'
  99. - activation_keys:
  100. - id: 3
  101. name: lx
  102. auto_publish: false
  103. component_ids:
  104. - 10
  105. - 11
  106. environments:
  107. - activation_keys:
  108. - 1
  109. hosts: []
  110. id: 1
  111. label: Library
  112. name: Library
  113. permissions:
  114. readable: true
  115. - activation_keys: []
  116. hosts: []
  117. id: 2
  118. label: DEV
  119. name: DEV
  120. permissions:
  121. readable: true
  122. - activation_keys: []
  123. hosts: []
  124. id: 3
  125. label: PROD
  126. name: PROD
  127. permissions:
  128. readable: true
  129. generated_for: none
  130. hosts: []
  131. id: 4
  132. import_only: false
  133. label: system-tester3
  134. last_published: 2023-07-17 14:15:24 UTC
  135. name: comp Repo (noarch)
  136. version_count: 2
  137. versions:
  138. - environment_ids: []
  139. id: 9
  140. published: 2023-07-17 14:03:01 UTC
  141. version: '2.0'
  142. - environment_ids:
  143. - 1
  144. - 2
  145. - 3
  146. id: 12
  147. published: 2023-07-17 14:15:24 UTC
  148. version: '2.1'

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

答案1

得分: 2

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

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

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

  1. 任务 [debug] ********************************************************************************************
  2. ok: [localhost] => {
  3. "msg": [
  4. "2.0",
  5. "2.0"
  6. ]
  7. }
英文:

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:

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

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

  1. TASK [debug] ********************************************************************************************
  2. ok: [localhost] => {
  3. "msg": [
  4. "2.0",
  5. "2.0"
  6. ]
  7. }

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:

确定