【问题标题】:Convert a list to dictionary - Ansible YAML将列表转换为字典 - Ansible YAML
【发布时间】:2021-07-17 04:28:13
【问题描述】:

我有一本剧本,我收到一条错误消息

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.yaml 是 var 文件还是 ansible 任务文件?你的剧本核心是什么?可靠的版本?我们如何重现该错误以帮助您?
  • @idrissEliguene 我已经更新了剧本。 Ansible 核心 2.9.13 python 版本 = 2.7.17
  • @idrissEliguene 这是 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

标签: list dictionary ansible yaml jinja2


【解决方案1】:

问:"如何将 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

【讨论】:

    【解决方案2】:

    vars 文件是 yaml dict 文件,因此您的列表必须是 vars 的字段:

    my_vars:
      - Comments: FWP - Testing this
        IP: 10.1.2.3
        Name: gTest101
      - Comments: FWP - Applying this
        IP: 10.1.2.4
        Name: gTest102

    并且您不需要注册 include_vars 任务。只需循环列表 var 名称(此处为 my_vars)。

    ---
    - 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: add-host-object-to-group
        check_point.mgmt.cp_mgmt_host:
          name: "{{ item.Name }}"          
          ip_address: "{{ item.IP }}"      
          comments: "{{ item.Comments }}"
          groups: gTest1A
          state: present
          auto_publish_session: yes
        loop: "{{ my_vars }}"  
        delegate_to: Global
        ignore_errors: yes

    【讨论】:

    • 这是 task.yml 文件。它是动态创建的(我已更新文件)``` - 键:gTest101 值:注释:FWP - 测试此 IP:10.1.2.3 名称:gTest101 - 键:gTest102 值:注释:FWP - 应用此 IP:10.1。 2.4 名称:gTest102 ```
    猜你喜欢
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    相关资源
    最近更新 更多