Как запросить словарь ANSI с динамическими переменными - PullRequest
0 голосов
/ 03 декабря 2018

Как сделать запрос в словаре ansible с динамическими переменными. Я хочу использовать ansible, чтобы прочитать серийный номер из idrac dell, а затем установить адрес в соответствии с серийным номером.Мой исходный код выглядит так:

---
- 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] ************************************************************************************************************************************************

ok: [192.168.0.120] => {"msg": ["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 голосов
/ 03 декабря 2018
- name: set idrac ip svctag to vars
  set_fact:
    SVCTAG: "{{ svctag.stdout_lines }}"

svctag.stdout_lines - это список, а не строка.Попробуйте:

- name: set idrac ip svctag to vars
  set_fact:
    SVCTAG: "{{ svctag.stdout_lines[0] }}"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...