【问题标题】:Ansible set_fact jinja2 for mapping a list and integerAnsible set_fact jinja2 用于映射列表和整数
【发布时间】:2019-05-13 22:28:21
【问题描述】:

我正在尝试在 Ansible 中编写一个“set_fact”语句,它基本上尝试将列表值与相同项目输出变量中的相应整数映射。我在引用附加和评估表达式以将列表值与相应值(另一个值)绑定时遇到语法错误

我已经尝试过如代码 sn-p 所示使用 map 和 lambda 来映射值。基本上“结果”是格式。

结果=[{_ansible_item_label=[0, item1], status=int1}, {_ansible_item_label=[0, item2], status=int2}, {_ansible_item_label=[0, item3], status=int3}]

- name: Matching item to values
      set_fact:
        append: "{{ append|default([]) + map(lambda x, y: x+ ':' +y, item._ansible_item_label, item.status) }}"
      with_items: "{{results|list}}"
      loop_control:
        label: "{{ item._ansible_item_label}}"
      register: append

预期数据类型:字典 预期结果:{ "item1": "int1", "item2": "int2", "item3": "int3" } 如果状态没有值,我们如何有效地处理它。

【问题讨论】:

    标签: ansible jinja2


    【解决方案1】:

    首先,我认为您不能在 jinja 中使用带有 map 的 lambda(但很高兴证明我是错误的)。无论如何,没有必要这样做。正如您将在下面看到的,您可以使用简单的 ansible 表达式来实现您的结果。

    然后,您有一个实现错误:使用您已经设置的相同 var 注册 set_fact 的结果将用运行 set_fact 的结果覆盖内容(即成功 + 所有其他信息)。

    要处理没有状态的元素,您只需使用default。

    这是我制作的测试剧本:

    ---
    - name: Create dictionary from other 
      hosts: localhost
    
      vars:
        result:
          - _ansible_item_label:
              - 0
              - item1
            status: int1
          - _ansible_item_label:
              - 0
              - item2
            status: int2
          - _ansible_item_label:
              - 0
              - item3
    
      tasks:
        - name: Create other dict
          set_fact:
            append: >-
              {{
                append
                | default([])
                | combine({item._ansible_item_label.1: item.status|default('no status')})
              }}
          loop: "{{ result }}"
    
        - name: Show result
          debug:
            var: append
    

    结果

    PLAY [Create dictionary from other] *********************************************************
    
    TASK [Gathering Facts] **********************************************************************
    ok: [localhost]
    
    TASK [Create other dict] ********************************************************************
    ok: [localhost] => (item={'_ansible_item_label': [0, 'item1'], 'status': 'int1'})
    ok: [localhost] => (item={'_ansible_item_label': [0, 'item2'], 'status': 'int2'})
    ok: [localhost] => (item={'_ansible_item_label': [0, 'item3']})
    
    TASK [Show result] **************************************************************************
    ok: [localhost] => {
        "append": {
            "item1": "int1",
            "item2": "int2",
            "item3": "no status"
        }
    }
    
    PLAY RECAP **********************************************************************************
    localhost                  : ok=3    changed=0    unreachable=0    failed=0
    

    【讨论】:

    • 效果很好。谢谢!!
    猜你喜欢
    • 2022-10-14
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-13
    • 2015-08-20
    相关资源
    最近更新 更多