下面是我的剧本,它的变量running_processes
包含一个pids列表(一个或多个)
[接下来,我读取每个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", "rc": 0, "start": "2019-11-06 11:46:41.089204", "stderr": "", "stderr_lines": [], "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'"}
您能否建议我为什么会收到错误以及如何解决该错误?
谢谢
变量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 }}"