Nelson G.
Nelson G.

Reputation: 5441

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

I want to use a variable to define which interface I want to use.

And the following playbook doesn't work:

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

Upvotes: 1

Views: 546

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68329

I know about this way:

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

Upvotes: 3

Related Questions