具有动态变量名的字典中的set_fact

时间:2017-07-21 23:28:51

标签: ansible-2.x

我有一个像这样的安全库存清单:

domaenenliste:
   "01":
      dhcp_start: 10.43.12.0
      dhcp_ende: 10.43.15.254
      server_id: 3
   "15":
   "16":
      dhcp_start: 10.43.128.26
      dhcp_ende: 10.43.131.255
      server_id: 2

我想在这个结构中设定事实,预期输出:

domaenenliste:
   "01":
      dhcp_start: 10.43.12.0
      dhcp_ende: 10.43.15.254
      server_id: 3
   "15":
      partner: foobar
   "16":
      dhcp_start: 10.43.128.26
      dhcp_ende: 10.43.131.255
      server_id: 2

我试着像这样分配它:

- debug:
    var: dom

- name: Test set
  set_fact:
    "{'{{domaenenliste[dom]}}':{'partner':'foobar'}}"

- debug:
    var: domaenenliste[dom].partner

- debug:
    var: domaenenliste['16']

创建以下输出:

TASK [calculate_missing_inventory_variables : debug] ****************************************************************************
task path: /home/mpw/gits/ansible-ffms/roles/calculate_missing_inventory_variables/tasks/main.yml:5
ok: [des1] => {
    "dom": "15"
}

TASK [calculate_missing_inventory_variables : Test set] *************************************************************************
task path: /home/mpw/gits/ansible-ffms/roles/calculate_missing_inventory_variables/tasks/main.yml:8
ok: [des1] => {
    "ansible_facts": {
        "_raw_params": {
            "": {
                "partner": "foobar"
            }
        }
    },
    "changed": false
}

TASK [calculate_missing_inventory_variables : debug] ****************************************************************************
task path: /home/mpw/gits/ansible-ffms/roles/calculate_missing_inventory_variables/tasks/main.yml:17
ok: [des1] => {
    "domaenenliste[dom].partner": "VARIABLE IS NOT DEFINED!"
}

TASK [calculate_missing_inventory_variables : debug] ****************************************************************************
task path: /home/mpw/gits/ansible-ffms/roles/calculate_missing_inventory_variables/tasks/main.yml:20
ok: [des1] => {
    "domaenenliste['16']": {
        "dhcp_ende": "10.43.131.255",
        "dhcp_start": "10.43.128.26",
        "server_id": 2
    }
}

如调试输出所示,变量仍未分配。使用此语法访问此结构有效。但我无法对其进行更改。

有人知道如何正确设置吗?

提前感谢您的帮助!

此致 的Matthias

1 个答案:

答案 0 :(得分:1)

您无法在Ansible中分配字典的子键,覆盖整个变量并进行必要的修改:

Argument 1: cannot convert from 'CellSection<TextInput>' to 'ICellSection'