繁体   English   中英

Ansible / jinja2 output

[英]Ansible / jinja2 output

我在 RHEL 7.4 上运行 Ansible 2.7.5。 (我知道它很旧;我会尽快升级——保证)。 zip 功能出现在 Ansible 2.3 中。 无论如何,我似乎无法通过 zip 过滤器获得几个列表来正确创建字典。

我从变量文件中读取了列表。 该列表是network_interfaces['computer_type']循环中的进一步向下; 这是来自调试 output:

[{u'interface': u'bond0.160', u'notes': u'note160'},
 {u'interface': u'bond0.197', u'notes': u'note197'},
 {u'interface': u'bond1', u'notes': u'bonded, numa1, broadcom device'}]

问题是,我试图将您在上面看到的接口名称连同它们的值一起放入一个字典中(现在作为一个例子是“注释”)。 但是,当我尝试执行dict (_keys|zip(_vals))时,如下面的_reused_val的第一个示例所示,我得到了相应的错误消息编号 1。因此,为了调试,我重新运行了我的任务,未注释 #2,并且随后#3未注释。

- debug:
    msg: >
         ITEM: {{ item }}
         *        :::network_interfaces::: {{ network_interfaces[computer_type] }}
         *        :::_keys::: {{ _keys }}
         *        :::_vals::: {{ _vals }}
         *        :::_reused_val::: {{ _reused_val }}
  loop: [ " {{ computer_type }} " ]
  vars:
    computer_type: big_computer
    _keys: "{{ network_interfaces[computer_type]|map(attribute='interface')|list }}"
    _vals: "{{ network_interfaces[computer_type] }}" # returns a list
    _reused_val: "{{ dict(_keys|zip(_vals)) }}"    #1
    #_reused_val: "{{ _keys|zip(_vals) | list }}"  #2
    #_reused_val: "{{ _keys|zip(_vals) }}"         #3

output 显示了reused_val (我稍微调整了间距以使其更易于阅读):

1- "msg": "Unexpected templating type error occurred on ({{ dict(_keys|zip(_vals)) }}): <lambda>() takes exactly 0 arguments (1 given)"
2- [(u'bond0.160', {u'interface': u'bond0.160', u'notes': u'note160'}),
    (u'bond0.197', {u'interface': u'bond0.197', u'notes': u'note197'}),
    (u'bond1', {u'interface': u'bond1', u'notes': u'bonded, numa1, broadcom device'})]
3- <itertools.izip object at 0x7f311c048dd0>

#1 是我想要开始工作的 - dict(_keys|zip(_vals))

我不知道为什么#3 显示 itertools.izip object。 我认为它应该返回一个列表。 对于#2,我不知道这些括号是干什么用的。 是因为列表中有 3 个字典吗?

最后,这里是案例#3 的消息的完整 output; 为了更容易阅读,我稍微打断了几行:

"ITEM:  big_computer  *        :::network_interfaces:::
[{u'interface': u'bond0.160', u'notes': u'note160'}, {u'interface': u'bond0.197', u'notes': u'note1
97'}, {u'interface': u'bond1', u'notes': u'bonded, numa1, broadcom device'}]
*        :::_keys::: [u'bond0.160', u'bond0.197', u'bond1']
*        :::_vals::: [{u'interface': u'bond0.160', u'notes': u'note160'}, {u'interface': u'bond0.197', u'notes': u'note197'}, {u'interface': u'bond1', u'notes': u'bonded, numa1, broadcom device'}]
*        :::_reused_val::: <itertools.izip object at 0x7fc768049cb0>\n"

这项工作是我在Ansible 的问题中工作的结果:需要组合来自 2 个文件的信息以完成稍后的任务我试图按照示例进行操作,但经过一天的工作,我仍然卡住了。 谢谢。

我认为你应该升级你的 Ansible。

使用 ansible 2.10.8,以下剧本运行没有错误,我认为它会产生您想要的 output。 这与您所拥有的基本相同,只是我在debug任务中使用了一个列表来生成更清晰的 output。

- hosts: localhost
  gather_facts: false
  vars:
    network_interfaces:
      big_computer:
        - interface: bond0.160
          notes: note160
        - interface: bond0.197
          notes: note197
        - interface: bond1
          notes: 'bonded, numa1, broadcom devices'

  tasks:
    - debug:
        msg:
          - "ITEM: {{ item }}"
          - "network_interfaces: {{ network_interfaces[computer_type] }}"
          - "keys: {{ _keys }}"
          - "vals: {{ _vals }}"
          - "reused_val: {{ _reused_val }}"
      loop: [ " {{ computer_type }} " ]
      vars:
        computer_type: big_computer
        _keys: "{{ network_interfaces[computer_type]|map(attribute='interface')|list }}"
        _vals: "{{ network_interfaces[computer_type] }}" # returns a list
        _reused_val: "{{ dict(_keys|zip(_vals)) }}"    #1

运行它会产生:

TASK [debug] *********************************************************************************************************************************************************************************
ok: [localhost] => (item= big_computer ) => {
    "msg": [
        "ITEM:  big_computer ",
        "network_interfaces: [{'interface': 'bond0.160', 'notes': 'note160'}, {'interface': 'bond0.197', 'notes': 'note197'}, {'interface': 'bond1', 'notes': 'bonded, numa1, broadcom devices'}]",
        "keys: ['bond0.160', 'bond0.197', 'bond1']",
        "vals: [{'interface': 'bond0.160', 'notes': 'note160'}, {'interface': 'bond0.197', 'notes': 'note197'}, {'interface': 'bond1', 'notes': 'bonded, numa1, broadcom devices'}]",
        "reused_val: {'bond0.160': {'interface': 'bond0.160', 'notes': 'note160'}, 'bond0.197': {'interface': 'bond0.197', 'notes': 'note197'}, 'bond1': {'interface': 'bond1', 'notes': 'bonded, numa1, broadcom devices'}}"
    ]
}

如果我使用 Ansible 2.7.5 运行它,我仍然看不到您报告的任何错误(这就是为什么在您的问题中包含一个可运行示例总是一个好主意的原因之一),但我不明白与 2.10.8 相同的行为。 我得到:

TASK [debug] *********************************************************************************************************************************************************************************
ok: [localhost] => (item= big_computer ) => {
    "msg": [
        "ITEM:  big_computer ",
        "network_interfaces: [{'interface': 'bond0.160', 'notes': 'note160'}, {'interface': 'bond0.197', 'notes': 'note197'}, {'interface': 'bond1', 'notes': 'bonded, numa1, broadcom devices'}]",
        "keys: ['bond0.160', 'bond0.197', 'bond1']",
        "vals: [{'interface': 'bond0.160', 'notes': 'note160'}, {'interface': 'bond0.197', 'notes': 'note197'}, {'interface': 'bond1', 'notes': 'bonded, numa1, broadcom devices'}]",
        "reused_val: {'[': '[', \"'\": 's', 'b': \"'\", 'o': 'n', 'n': 'o', 'd': 't', '0': 'd', '.': '0', '1': 'e', '6': 'a', ',': \"'\", ' ': ',', '9': '1', '7': '6', ']': \"'\"}"
    ]
}

所以,除了最后一步,一切都很好。 这里最大的问题是 Ansible 2.7.5 不能很好地处理字符串值以外的任何内容。 当你写这个...

_keys: "{{ network_interfaces[computer_type]|map(attribute='interface')|list }}"

结果是一个字符串值,而不是一个列表,而在 Ansible 的更新版本中, _keys变量是一个列表。 我们可以通过显式序列化和反序列化 JSON 的所有内容来解决此问题:

- debug:
    msg:
      - "ITEM: {{ item }}"
      - "network_interfaces: {{ network_interfaces[computer_type] }}"
      - "keys: {{ _keys }}"
      - "vals: {{ _vals }}"
      - "reused_val: {{ _reused_val }}"
  loop: [ " {{ computer_type }} " ]
  vars:
    computer_type: big_computer
    _keys: "{{ network_interfaces[computer_type]|map(attribute='interface')|list|to_json }}"
    _vals: "{{ network_interfaces[computer_type]|to_json }}"
    _reused_val: "{{ dict(_keys|from_json|zip(_vals|from_json)) }}"

这会产生与 2.10.8 的原始版本相同的 output:

TASK [debug] *********************************************************************************************************************************************************************************
ok: [localhost] => (item= big_computer ) => {
    "msg": [
        "ITEM:  big_computer ",
        "network_interfaces: [{'interface': 'bond0.160', 'notes': 'note160'}, {'interface': 'bond0.197', 'notes': 'note197'}, {'interface': 'bond1', 'notes': 'bonded, numa1, broadcom devices'}]",
        "keys: [\"bond0.160\", \"bond0.197\", \"bond1\"]",
        "vals: [{\"interface\": \"bond0.160\", \"notes\": \"note160\"}, {\"interface\": \"bond0.197\", \"notes\": \"note197\"}, {\"interface\": \"bond1\", \"notes\": \"bonded, numa1, broadcom devices\"}]",
        "reused_val: {'bond0.160': {'interface': 'bond0.160', 'notes': 'note160'}, 'bond0.197': {'interface': 'bond0.197', 'notes': 'note197'}, 'bond1': {'interface': 'bond1', 'notes': 'bonded, numa1, broadcom devices'}}"
    ]
}

...但这是一个糟糕的解决方案,只需升级就会更清洁。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM