raptor85
raptor85

Reputation: 125

'dict object' has no attribute 'stdout_lines'

I am trying to extract the status (http) (Enabled/ Disabled) for each server and display it in the logs. It is able to check for http status but when I try to store it, it is giving following error.

- name: Check http status(yes or no)
  shell: |
    sshpass -p "{{ imc_prod_password }}" ssh -T -o StrictHostKeyChecking=no {{ imc_username }}@"{{ item.cimcNames }}" << EOL
    scope http
    show detail | grep Enabled
    EOL
   register: http_status
   loop: "{{ user_list.list }}"
      
 - debug:
     msg: '{{ http_status.stdout_lines | select("Enabled") | list }}'

The command show detail gives following output

Enabled: no

Output - Line 59 points to - debug:

TASK [cimc_reip : Check http status(yes or no)] ********************************
changed: [hostname] => (item={'PreferredDNS': 'x.x.x.x', 'IPdetails': 'x.x.x.x', 'Alternate': 'x.x.x.x', 'id': '1', 'GWDetails': 'x.x.x.x', 'Subnetdetails': 'x.x.x.x', 'cimcNames': 'test-server-01-r'})
changed: [hostname] => (item={'PreferredDNS': 'x.x.x.x', 'IPdetails': 'x.x.x.x', 'Alternate': 'x.x.x.x', 'id': '2', 'GWDetails': 'x.x.x.x', 'Subnetdetails': 'x.x.x.x', 'cimcNames': 'test-server-02-r'})
TASK [cimc_reip : debug] *******************************************************
fatal: [hostname]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout_lines'\n\nThe error appears to be in '/builds/xxxxx/xxxx/playbooks/roles/cimc_reip/tasks/main.yml': line 59, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - debug:\n    ^ here\n"}

Upvotes: 2

Views: 7960

Answers (1)

U880D
U880D

Reputation: 12142

As already mentioned in the comments you are registering the result set in a loop in one variable. You will get therefore an dictionary and in your example an error

The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout_lines'

The following test shows it

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    USERNAME: ['test'] # list
    PASSWORD: 'password'

    target_host: "test.example.com"

  tasks:

  - name: Check http status
    shell: |
      sshpass -p '{{ PASSWORD }}' ssh -T -o StrictHostKeyChecking=no {{ item }}@"{{ target_host }}" << EOF
      sudo lsof -Pi TCP
      EOF
    register: http_status
    loop: "{{ USERNAME }}" # list

  - debug:
      msg: "{{ http_status | type_debug }}"

resulting into an output of

TASK [debug] ************
ok: [test.example.com] =>
  msg: dict

You can loop over the http_status.results list in the dictionary as follow

  - name: Show result
    debug:
      msg: "{{ item.stdout_lines }}"  
    loop: "{{ http_status.results }}" # list
    loop_control:
      label: "{{ item.item }}" # with USERNAME looped over to limit the displayed output

resulting into an output of

TASK [Show result] *****************************************************************************
ok: [test.example.com] => (item=test) =>
  msg:
  - COMMAND      PID       USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
  - httpd        384       root   19u  IPv4 12345678      0t0  TCP test.example.com:443 (LISTEN)

Upvotes: 3

Related Questions