【问题标题】:ansible with_dict fails when provided with set_fact variable提供 set_fact 变量时,ansible with_dict 失败
【发布时间】:2019-05-11 15:15:49
【问题描述】:

我正在尝试为接口变量动态提供字典名称。

我的 ansible 任务如下所示。

- name: Setting interface list
  set_fact:
    one_fact: "{{ host_name }}_interfaces"

- name: deb
  debug: var={{ one_fact }}

- name: Managing Interfaces
  ios_interface:
    enabled: "{{ item['value']['enabled'] }}"
    name: "{{ item['key'] }}"
    state: "{{ item['value']['state'] }}"
  with_dict: "{{ one_fact }}"

字典看起来像这样

---
h1_interfaces:
  Ethernet1/1:
    description: Firewall
    enabled: true
    speed: auto
    state: present
  Ethernet1/2:
    description: asd
    enabled: true
    speed: auto
    state: present
h2_interfaces:
  Ethernet1/1:
    description: Firewall
    enabled: true
    speed: auto
    state: present
  Ethernet1/2:
    description: asd
    enabled: true
    speed: auto
    state: present

当我设置with_dict: {{ one_fact }} 时,我得到一个错误FAILED! => {"msg": "with_dict expects a dict"} 但是当我提供with_dict: {{ h1_interfaces }} 时,它就像一个魅力。我做错了什么?

【问题讨论】:

  • 我真的不明白你想要完成什么......with_dict 确实在期待一个字典,但one_fact 被初始化为一个字符串。您能否详细说明您的预期结果是什么?

标签: dictionary variables ansible


【解决方案1】:

显然您也有一个变量host_name,它设置为h1h2,并且您想访问字典:h1_interfaces/h2_interfaces

要动态构造变量名并访问它的值,你应该使用lookup plugin,请看下面的任务:

  - name: Setting interface list
    set_fact:
      one_fact: "{{ lookup('vars', myvar + '_interfaces') }}"
    vars:
      myvar: "{{ host_name }}"

还有一个稍微改动的剧本来展示结果:

剧本:

---
- hosts: localhost
  gather_facts: false
  vars:
    host_name: h1
    h1_interfaces:
      Ethernet1/1:
        description: Firewall
        enabled: true
        speed: auto
        state: present
      Ethernet1/2:
        description: asd
        enabled: true
        speed: auto
        state: present
    h2_interfaces:
      Ethernet1/1:
        description: Firewall
        enabled: true
        speed: auto
        state: present
      Ethernet1/2:
        description: asd
        enabled: true
        speed: auto
        state: present
    
 
  tasks:

  - name: Setting interface list
    set_fact:
      one_fact: "{{ lookup('vars', myvar + '_interfaces') }}"
    vars:
      myvar: "{{ host_name }}"

  - name: deb
    debug: var=one_fact

  - name: Managing Interfaces
    debug:
      msg: "enabled: {{ item['value']['enabled'] }}, name: {{ item['key'] }}, state: {{ item['value']['state'] }}"
    with_dict: "{{ one_fact }}"

结果:

TASK [Managing Interfaces] *********************************************************************************************************************************************************************************************
ok: [localhost] => (item={'key': 'Ethernet1/1', 'value': {'description': 'Firewall', 'enabled': True, 'speed': 'auto', 'state': 'present'}}) => {
    "msg": "enabled: True, name: Ethernet1/1, state: present"
}
ok: [localhost] => (item={'key': 'Ethernet1/2', 'value': {'description': 'asd', 'enabled': True, 'speed': 'auto', 'state': 'present'}}) => {
    "msg": "enabled: True, name: Ethernet1/2, state: present"
}

干杯

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-21
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 2020-03-15
    • 2018-04-23
    相关资源
    最近更新 更多