如何使用动态变量查询ansible字典

时间:2018-12-03 05:56:11

标签: variables ansible

如何使用动态变量查询ansible词典 我想使用ansible从dell的idrac中读取序列号,然后根据序列号设置地址。 我的源代码就是这样

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

现在我收到此错误,

PLAY [set iDRAC Ipaddr] *******************************************************************************************************************************************

TASK [get dell server service-tag] ********************************************************************************************************************************

changed: [192.168.0.120]

TASK [show svctag] ************************************************************************************************************************************************
ok: [192.168.0.120] => {
"msg": {
    "changed": true,
    "failed": false,
    "rc": 0,
    "stderr": "Shared connection to 192.168.0.120 closed.\r\n",
    "stdout": "xxx30S2\r\n",
    "stdout_lines": [
        "xxx30S2"
    ]
}

}

TASK [show network] ***********************************************************************************************************************************************
ok: [192.168.0.120] => {
"msg": "192.168.0.120"

}

TASK [set idrac ip svctag to vars] ********************************************************************************************************************************
ok: [192.168.0.120]

TASK [show SVCTAG] ************************************************************************************************************************************************

好:[192.168.0.120] => {     “味精”:[         “ xxx30S2”     ] }

TASK [show network 2] *********************************************************************************************************************************************
fatal: [192.168.0.120]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: dict object has no element ['xxx30S2']\n\nThe error appears to have been in '/Users/code/ansible/dell/Dell-EMC-Ansible-Modules-for-iDRAC/examples/test5.yml': line 31, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: show network 2\n      ^ here\n"}
    to retry, use: --limit @/Users/code/ansible/dell/Dell-EMC-Ansible-Modules-for-iDRAC/examples/test5.retry

PLAY RECAP ********************************************************************************************************************************************************
192.168.0.120             : ok=5    changed=1    unreachable=0    failed=1

我在哪里可以犯错误?

1 个答案:

答案 0 :(得分:2)

JAYANAGAR

svctag.stdout_lines是一个列表,而不是字符串。试试:

- name: set idrac ip svctag to vars
  set_fact:
    SVCTAG: "{{ svctag.stdout_lines }}"