Ansible 2:是否可以使用变量作为字典键

Ansible 2: Is it possible to use a variable as a dictionary key

我想用一个变量来定义我想使用的接口。

下面的剧本不起作用:

- hosts: all
  vars:
    eth_to_use: eth0
  tasks:
  - debug: msg="{{ansible_{{eth_to_use}}.ipv4.address}}"

我是这样知道的:

- hosts: localhost
  vars:
    eth_to_use: en0
  tasks:
  - debug: msg="{{hostvars[inventory_hostname]['ansible_'+eth_to_use].ipv4[0].address}}"