gpt4 book ai didi

list - 将列表转换为字典 - Ansible YAML

转载 作者:行者123 更新时间:2023-12-04 07:45:25 38 4
gpt4 key购买 nike

我有一个剧本,在那里我收到一条错误消息

fatal: [localhost]: FAILED! => {"ansible_facts": {"tasks": {}}, "ansible_included_var_files": [], "changed": false, "message": "/home/user/invoke_api/automation/tmp/task.yml must be stored as a dictionary/hash"}
task.yml
文件 task.yml 是动态创建的,并一直从另一个来源过滤以提供以下输出。
-   key: gTest101
value:
Comments: FWP - Testing this
IP: 10.1.2.3
Name: gTest101
- key: gTest102
value:
Comments: FWP - Applying this
IP: 10.1.2.4
Name: gTest102

问题 :如何将 task.yml 中的列表转换为字典?从列表转换为字典的代码是什么
playbook.yml
---
- name: Global Objects
hosts: check_point
connection: httpapi
gather_facts: False
vars_files:
- 'credentials/my_var.yml'
- 'credentials/login.yml'
tasks:
- name: read-new-tmp-file
include_vars:
file: tmp/task.yml
name: tasks
register: new_host

- name: add-host-object-to-group
check_point.mgmt.cp_mgmt_host:
name: "{{ item.value.Name | quote }}"
ip_address: "{{ item.value.IP | quote }}"
comments: "{{ item.value.Comments }}"
groups: gTest1A
state: present
auto_publish_session: yes
loop: "{{ new_host.dict | dict2items }}"
delegate_to: Global
ignore_errors: yes


Ansible 核心 2.9.13
python 版本= 2.7.17

最佳答案

问:“ 如何将 task.yml 中的列表转换为字典?
答:使用 items2dict .例如,读取文件并创建列表

    - set_fact:
l1: "{{ lookup('file', 'task.yml')|from_yaml }}"

    l1:
- key: gTest101
value:
Comments: FWP - Testing this
IP: 10.1.2.3
Name: gTest101
- key: gTest102
value:
Comments: FWP - Applying this
IP: 10.1.2.4
Name: gTest102
那么下面的任务
    - set_fact:
d1: "{{ l1|items2dict }}"
创建字典
  d1:
gTest101:
Comments: FWP - Testing this
IP: 10.1.2.3
Name: gTest101
gTest102:
Comments: FWP - Applying this
IP: 10.1.2.4
Name: gTest102

关于list - 将列表转换为字典 - Ansible YAML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67221899/

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