Reputation: 11
How do I query Ansible dictionary with dynamic variables? I want to use ansible to read the serial number from the idrac of dell, and then set the address according to the serial number. My source code:
---
- hosts: all
name: set iDRAC Ipaddr
gather_facts: False
vars:
svctag_test: xxx30S2
network_configs:
xxx30S2:
ip: 192.168.192.86
tasks:
- name: get dell server service-tag
raw: racadm getsvctag
register: svctag
- name: show svctag
debug:
msg="{{ svctag }}"
- name: show network
debug:
msg="{{ network_configs[svctag_test].ip }}"
- name: set idrac ip svctag to vars
set_fact:
SVCTAG: "{{ svctag.stdout_lines }}"
- name: show SVCTAG
debug:
msg="{{ SVCTAG }}"
- name: show network 2
debug:
msg="{{ network_configs[SVCTAG].ip }}"
#msg="{{ network_configs[SVCTAG] }}"
#msg="{{ hostvars[inventory_hostname][network_configs][SVCTAG] }}"
#msg="{{ lookup('vars', network_configs )[SVCTAG]}}"
- name: set dell server idrac ip form service-tag
raw: racadm config -g cfgLanNetworking -o cfgNicIpAddress "{{ network_configs[SVCTAG].ip }}"
Upvotes: 0
Views: 287
Reputation: 3046
- name: set idrac ip svctag to vars
set_fact:
SVCTAG: "{{ svctag.stdout_lines }}"
svctag.stdout_lines is a list, not a string. Try:
- name: set idrac ip svctag to vars
set_fact:
SVCTAG: "{{ svctag.stdout_lines[0] }}"
Upvotes: 3