gpt4 book ai didi

dictionary - 循环遍历包含 Ansible 列表的字典列表?

转载 作者:行者123 更新时间:2023-12-05 04:25:33 24 4
gpt4 key购买 nike

我有一个 Ansible 角色,它创建应该安装特定包的计划任务。

这样使用:

scheduled_install:

- name: install package x,y,z on the same day
package:
- xpackage
- ypackage
- zpackage
action: install
day: '2022-08-08'
time: '10:00:00'

- name: install package a on another day
package: apackage
action: install
day: '2022-10-12'
time: '12:00:00'

如果我每个字典只使用一个包(apackage),它工作正常,但是我希望能够添加一个包列表。

(任务)只有一个包,我是这样工作的:

- name: 'Scheduled Install'
win_scheduled_task:
name: Scheduled Install of {{ item.package }}
actions:
- path: powershell.exe
arguments: choco install {{ item.package }}
triggers:
- type: time
start_boundary: '{{ item.day }}T{{ item.time }}'
username: SYSTEM
state: present
enabled: yes
loop: "{{ scheduled_install }}"

循环“scheduled_install”。我试过使用子元素:How to loop over this dictionary in Ansible?但我无法让它发挥作用,也无法弄清楚如何正确地将它连接在一起以获得所需的输出,并且没有找到与我的案例太相似的案例。

我本质上是在寻找一种方法来遍历数据以获取此信息:

"msg": "Package=xpackage, day=2022-08-08, time=10:00:00"
"msg": "Package=ypackage, day=2022-08-08, time=10:00:00"
"msg": "Package=zpackage, day=2022-08-08, time=10:00:00"
"msg": "Package=apackage, day=2022-10-12, time=12:00:00"

Ansible 版本:5.7.0

最佳答案

使用with_subelements .首先将所有属性 转换为列表。创建列表

package_lists: "{{ scheduled_install|json_query('[].[package]')|
map('flatten')|
map('community.general.dict_kv', 'package')|
list }}"

给予

package_lists:
- package:
- xpackage
- ypackage
- zpackage
- package:
- package

合并字典列表

scheduled_install_lists: "{{ scheduled_install|zip(package_lists)|
map('combine')|
list }}"

给予

scheduled_install_lists:
- action: install
day: '2022-08-08'
name: install package x,y,z on the same day
package:
- xpackage
- ypackage
- zpackage
time: '10:00:00'
- action: install
day: '2022-10-12'
name: install package a on another day
package:
- apackage
time: '12:00:00'

现在你可以迭代字典了

    - debug:
msg: >-
Package={{ item.1 }},
day={{ item.0.day }},
time={{ item.0.time }}
with_subelements:
- "{{ scheduled_install_lists }}"
- package

给出(删节)

  msg: Package=xpackage, day=2022-08-08, time=10:00:00
msg: Package=ypackage, day=2022-08-08, time=10:00:00
msg: Package=zpackage, day=2022-08-08, time=10:00:00
msg: Package=apackage, day=2022-10-12, time=12:00:00

<支持>
  • 完整剧本的示例
- hosts: localhost
vars:
scheduled_install:
- name: install package x,y,z on the same day
package:
- xpackage
- ypackage
- zpackage
action: install
day: '2022-08-08'
time: '10:00:00'
- name: install package a on another day
package: apackage
action: install
day: '2022-10-12'
time: '12:00:00'
package_lists: "{{ scheduled_install|json_query('[].[package]')|
map('flatten')|
map('community.general.dict_kv', 'package')|
list }}"
scheduled_install_lists: "{{ scheduled_install|zip(package_lists)|
map('combine')|
list }}"
tasks:
- debug:
msg: >-
Package={{ item.1 }},
day={{ item.0.day }},
time={{ item.0.time }}
with_subelements:
- "{{ scheduled_install_lists }}"
- package

关于dictionary - 循环遍历包含 Ansible 列表的字典列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73208645/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com