ansible - “dict对象”没有属性 'stdout_lines'

标签 ansible ansible-2.x ansible-role

我正在尝试提取每个服务器的状态(http)(启用/禁用)并将其显示在日志中。它能够检查 http 状态,但是当我尝试存储它时,它给出以下错误。

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

命令显示详细信息给出以下输出

Enabled: no

输出 - 第 59 行指向 - 调试:

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

最佳答案

正如评论中已经提到的,您在一个变量中是 registering the result set in a loop 。因此,您将得到一本字典,并且在您的示例中出现错误

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

下面的测试表明了这一点

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

导致输出

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

您可以按如下方式循环遍历字典中的http_status.results列表

  - 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

导致输出

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)

关于ansible - “dict对象”没有属性 'stdout_lines',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73044649/

相关文章:

ansible - 无法通过本地连接使用 ansible_user

ssh - 仅当满足条件并在 Ansible Playbook 中重新启动 SSH 服务时才在 Ansible 循环中处理项目

Ansible 获取模块与目标目录的问题

Ansible - 将 group_vars 和 host_vars 中的变量一起放到一个列表中

docker - docker容器上的Ansible命令?

python - 使用 ansible API 打印 hoSTList

Ansible - 运行任务列表直到成功

ansible - 在具有操作系统特定默认值的角色中定义 Ansible 变量,该默认值可以轻松覆盖

Ansible:我可以从命令行执行角色吗?