简体   繁体   English

ansible with_dict 在提供 set_fact 变量时失败

[英]ansible with_dict fails when provided with set_fact variable

I am trying to dynamically provide dictionary name for interface variables.我正在尝试为接口变量动态提供字典名称。

My ansible task looks like this.我的任务看起来像这样。

- 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 }}"

Dictionary looks something like this字典看起来像这样

---
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

When i set with_dict: {{ one_fact }} i get an error FAILED: => {"msg": "with_dict expects a dict"} But when i provide with with_dict: {{ h1_interfaces }} it works like a charm.当我设置with_dict: {{ one_fact }}我得到一个错误FAILED: => {"msg": "with_dict expects a dict"}但是当我提供with_dict: {{ h1_interfaces }}它就像一个魅力。 What am i doing wrong?我究竟做错了什么?

Apparently you have a variable host_name too, which is set to h1 or h2 , and you want to access the dictionaries: h1_interfaces / h2_interfaces .显然你也有一个变量host_name ,它被设置为h1h2 ,你想访问字典: h1_interfaces / h2_interfaces

To construct dynamically the variable name and access its value, you should use the lookup plugin , please see below task:要动态构造变量名并访问它的值,你应该使用查找插件,请看下面的任务:

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

and a slightly altered playbook to demonstrate the result:和一个稍微改变的剧本来证明结果:

playbook:剧本:

---
- 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 }}"

result:结果:

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"
}

cheers干杯

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

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