如何始终返回任务的'changed'或'ok'



我正在运行一本剧本来杀死进程。当没有杀戮过程时,"现在杀死"任务不会返回任何东西。如何强迫它返回"更改"或"确定"?

我已经尝试设置'change_when:true',但它不起作用。

- hosts: WORK
  gather_facts: no
  pre_tasks:
    - setup:
        gather_subset: min
  tasks:
    - name: Get running processes @weight 1
      shell:
        ps -fu `whoami` |grep -v $$ |grep -v PID |grep -v sshd |grep -v ansible |grep -v $$ |awk '{print $2}'
      register:
        running_processes
    - name: Now killing
      command:
        kill -9 {{ item }}
      with_items:
        "{{ running_processes.stdout_lines }}"
PLAY [WORK] ******************************************************************************************************************************************************************
TASK [setup] *****************************************************************************************************************************************************************
ok: [tstqat36_qatwrk90]
TASK [Get running processes @weight 1] ***************************************************************************************************************************************
changed: [tstqat36_qatwrk90]
TASK [Now killing] ***********************************************************************************************************************************************************
PLAY RECAP *******************************************************************************************************************************************************************
tstqat36_qatwrk90          : ok=2    changed=1    unreachable=0    failed=0

我的意思是,你可以做这样的黑客,但我认为这不是一个好主意:

- name: Now killing
  shell: |
      [ "{{ item }}" != __flag__ ] && kill -9 "{{ item }}"
  register: result
  failed_when: result.rc != 0 and item != "__flag__"
  changed_when: result.rc == 0 and item != "__flag__"
  with_items:
    - __flag__
    - "{{ running_processes.stdout_lines }}"

无论 running_processes.stdout_lines中是否有项目。

,这将始终报告"确定"。

最新更新