简体   繁体   中英

How to query ansible dictionary with dynamic variables

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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