【问题标题】:get ansible facts dynamically in playbook在剧本中动态获取 ansible 事实
【发布时间】:2020-04-07 14:39:32
【问题描述】:

我有如下事实,

 "facter_partitions": {
            "/dev/loop0": {
                "backing_file": "/a",
                "size": "10.92 GiB",
                "size_bytes": 3080000000
            },
            "/dev/loop1": {
                "backing_file": "/b",
                "size": "11.92 GiB",
                "size_bytes": 1080000000
            },
            "/dev/loop10": {
                "backing_file": "/c",
                "size": "12.02 GiB",
                "size_bytes": 2064000000
            }
            },

我们如何在 playbook 中动态获取键值对。可能正在使用 for 循环。

【问题讨论】:

    标签: loops ansible ansible-facts


    【解决方案1】:

    这是一本字典。您必须使用dict2items 将其转换为列表,然后您可以访问每个项目的键值对,

    ---
    - hosts: localhost
      gather_facts: false
      vars:
         "facter_partitions": {
                "/dev/loop0": {
                    "backing_file": "/a",
                    "size": "10.92 GiB",
                    "size_bytes": 3080000000
                },
                "/dev/loop1": {
                    "backing_file": "/b",
                    "size": "11.92 GiB",
                    "size_bytes": 1080000000
                },
                "/dev/loop10": {
                    "backing_file": "/c",
                    "size": "12.02 GiB",
                    "size_bytes": 2064000000
                }
           }
      tasks:
        - debug: msg="{{ item.key }} -  {{ item.value.backing_file }}"
          loop: "{{ facter_partitions | dict2items }}"
    
    

    这将产生类似的输出,

    TASK [debug] *******************************************************************
    ok: [127.0.0.1] => (item={'key': u'/dev/loop10', 'value': {u'size_bytes': 2064000000, u'backing_file': u'/c', u'size': u'12.02 GiB'}}) => {
        "msg": "/dev/loop10 -  /c"
    }
    ok: [127.0.0.1] => (item={'key': u'/dev/loop1', 'value': {u'size_bytes': 1080000000, u'backing_file': u'/b', u'size': u'11.92 GiB'}}) => {
        "msg": "/dev/loop1 -  /b"
    }
    ok: [127.0.0.1] => (item={'key': u'/dev/loop0', 'value': {u'size_bytes': 3080000000, u'backing_file': u'/a', u'size': u'10.92 GiB'}}) => {
        "msg": "/dev/loop0 -  /a"
    }
    

    【讨论】:

    • 感谢您的信息,但如果我尝试在下面的剧本中获取值 backing_file,它会失败 - 调试:msg:“{{ item.key }} - {{ item.value.backing_file }} " loop: "{{ facter_partitions | dict2items }}" 错误是:'dict object' has no attribute 'backing_file
    • 使用with_dict: "{{ facter_partitions }}" 代替loop 并尝试。两者是相同的。不知道为什么它没有选择嵌套键
    • 与_dict一起使用。 value.backing_file 失败并出现同样的错误,** {"msg": "该任务包含一个带有未定义变量的选项。错误是:'dict object' has no attribute 'backing_file** . 它适用于 value.size
    • 感谢@franklinsijo,问题已解决。实际上,在其他情况下缺少 backing_file。 when: item.value.backing_file is defined 声明解决了问题
    • 啊,好吧,由于问题中的示例dict没有遗漏任何键,所以我没有想到添加条件。
    猜你喜欢
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多