尽管变量不为null,但获取Ansible运行时错误dict对象'没有属性'stdout_lines'

下面是我的剧本,其中有一个变量running_processes,其中包含一个(一个或多个)PID列表

接下来,我读取每个pid的用户ID。到目前为止一切都很好。

然后我尝试使用curr_user_ids-debug module变量中打印用户ID列表,这是当我收到错误消息:'dict object'没有属性'stdout_lines'

我期望curr_user_ids包含一个或多个条目,如下面共享的输出所示。

    - name: Get running processes list from remote host
      shell: "ps -few | grep java | grep -v grep | awk '{print $2}'"
      changed_when: false
      register: running_processes

    - name: Gather USER IDs from processes id before killing.
      shell: "id -nu `cat /proc/{{ running_processes.stdout }}/loginuid`"
      register: curr_user_ids
      with_items: "{{ running_processes.stdout_lines }}"

    - debug: msg="USER ID LIST HERE:{{ curr_user_ids.stdout }}"
      with_items: "{{ curr_user_ids.stdout_lines }}"

TASK [Get running processes list from remote host] **********************************************************************************************************
task path: /app/wls/startstop.yml:22
ok: [10.9.9.111] => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"},"changed": false,"cmd": "ps -few | grep java | grep -v grep  | awk '{print $2}'","delta": "0:00:00.166049","end": "2019-11-06 11:49:42.298603","rc": 0,"start": "2019-11-06 11:49:42.132554","stderr": "","stderr_lines": [],"stdout": "24032","stdout_lines": ["24032"]}

TASK [Gather USER IDS of processes id before killing.] ******************************************************************************************************
task path: /app/wls/startstop.yml:59
changed: [10.9.9.111] => (item=24032) => {"ansible_loop_var": "item","changed": true,"cmd": "id -nu `cat /proc/24032/loginuid`","delta": "0:00:00.116639","end": "2019-11-06 11:46:41.205843","item": "24032","start": "2019-11-06 11:46:41.089204","stdout": "user1","stdout_lines": ["user1"]}

TASK [debug] ************************************************************************************************************************************************
task path: /app/wls/startstop.yml:68
fatal: [10.9.9.111]: FAILED! => {"msg": "'dict object' has no attribute 'stdout_lines'"}

您能否建议我为什么会收到错误以及如何解决该错误?

谢谢

wtuuser 回答:尽管变量不为null,但获取Ansible运行时错误dict对象'没有属性'stdout_lines'

没什么好注意的,为什么您的解决方案不起作用。

任务Get running processes list from remote host返回以换行符分隔的\n字符串。因此,您需要对其进行处理,然后首先将输出转换为适当的列表对象。

任务Gather USER IDs from processes id before killing.返回一个包含键results的字典,其中值的类型为list,因此您需要对其进行迭代并为每个元素获取stdout的值。

这就是我解决的方法。

---

- hosts: "localhost"
  gather_facts: true
  become: true
  tasks:
      - name: Set default values
        set_fact:
          process_ids: []
          user_names: []

      - name: Get running processes list from remote host
        shell: "ps -few | grep java | grep -v grep | awk '{print $2}'"
        changed_when: false
        register: running_processes

      - name: Register a list of Process ids (Split newline from output before)
        set_fact:
          process_ids: "{{ running_processes.stdout.split('\n') }}"

      - name: Gather USER IDs from processes id before killing.
        shell: "id -nu `cat /proc/{{ item }}/loginuid`"
        register: curr_user_ids
        with_items: "{{ process_ids }}"

      - name: Register a list of User names (Out of result from before)
        set_fact:
          user_names: "{{ user_names + [item.stdout] | unique }}"
        when: item.rc == 0
        with_items:
          - "{{ curr_user_ids.results }}"

      - name: Set unique entries in User names list
        set_fact:
          user_names: "{{ user_names | unique }}"

      - name: DEBUG
        debug:
          msg: "{{ user_names }}"
,

变量 curr_user_ids 记录每次迭代的结果

register: curr_user_ids
with_items: "{{ running_processes.stdout_lines }}"

结果列表存储在

curr_user_ids.results

看看变量

- debug:
    var: curr_user_ids

并循环显示stdout_lines

- debug:
    var: item.stdout_lines
  loop: "{{ curr_user_ids.results }}"
本文链接:https://www.f2er.com/3154442.html

大家都在问