I would need help to filter hosts with specific previous result, to be able to use add_host module. It seems add_host is ignoring the "when" condition, so probably I need to make a loop for the required hosts.
- name: Get chrome process ansible.windows.win_powershell: script: | (get-process chrome -ErrorAction SilentlyContinue).count -gt 0 register: ps_chrome_running Now I would need to filter for hosts where the ps_chrome_running.output[0] is true and put those hosts into a new group.
- name: Put on skip list ansible.builtin.add_host: name: '{{ item }}' groups: 'skip_chrome_actions' loop: "{{ hostvars|dict2items| selectattr('ps_chrome_running.output[0]', '==', true)| map(attribute='key') }}" delegate_to: localhost run_once: true I've tried already different methods, but somehow cannot get the clue of it. Could someone help with the loop filter to get only the list of hosts where ps_chrome_running.output[0] == true?
+1: ps_chrome_running.output[0] is bool already
register looks like this:
changed: [server1] => { "changed": true, "debug": [], "error": [], "host_err": "", "host_out": "", "information": [], "invocation": { "module_args": { "arguments": null, "chdir": null, "creates": null, "depth": 2, "error_action": "continue", "executable": null, "parameters": null, "removes": null, "script": "(get-process chrome -ErrorAction SilentlyContinue).count -eq 0\n", "sensitive_parameters": null } }, "output": [ true ], "result": {}, "verbose": [], "warning": [] } UPDATE: This playbook:
- name: "Get Chrome processes" hosts: all gather_facts: no tasks: - name: Get chrome process ansible.windows.win_powershell: script: | (get-process chrome -ErrorAction SilentlyContinue).count -gt 0 register: ps_chrome_running # failed_when: ps_chrome_running.output[0] - name: Check vars1 ansible.builtin.debug: msg: "item : {{ item }}" loop: "{{ hostvars | dict2items | json_query('[?value.ps_chrome_running.output[0]].key') }}" delegate_to: localhost run_once: true ignore_errors: true - name: Add host candidate ansible.builtin.debug: msg: "host: {{ item }}" loop: "{{ hostvars | dict2items | json_query('[?value.ps_chrome_running.output[0]].key') }}" delegate_to: localhost run_once: true ignore_errors: true - name: Put on skip list ansible.builtin.add_host: name: '{{ item }}' groups: 'skip_chrome_update' loop: "{{ hostvars | dict2items | json_query('[?value.ps_chrome_running.output[0]].key') }}" delegate_to: localhost run_once: true - debug: var: groups.skip_chrome_actions delegate_to: localhost run_once: true - name: Update Chrome hosts: '!skip_chrome_update' gather_facts: no tasks: - name: More task ansible.builtin.debug: msg: "should run on prev success" - name: More task2 ansible.windows.win_powershell: script: | write-output $env:computername Has this output:
PLAY [Get Chrome processes] **************************************************** TASK [Get chrome process] ****************************************************** changed: [Windows1] => {"changed": true, "debug": [], "error": [], "host_err": "", "host_out": "", "information": [], "output": [false], "result": {}, "verbose": [], "warning": []} changed: [Windows2] => {"changed": true, "debug": [], "error": [], "host_err": "", "host_out": "", "information": [], "output": [false], "result": {}, "verbose": [], "warning": []} TASK [Check vars1] ************************************************************* skipping: [Windows1] => {"skipped_reason": "No items in the list"} TASK [Add host candidate] ****************************************************** skipping: [Windows1] => {"skipped_reason": "No items in the list"} TASK [Put on skip list] ******************************************************** skipping: [Windows1] => {"changed": false, "skipped_reason": "No items in the list"} TASK [debug] ******************************************************************* ok: [Windows1 -> localhost] => { "groups.skip_chrome_actions": "VARIABLE IS NOT DEFINED!: 'dict object' has no attribute 'skip_chrome_actions'. 'dict object' has no attribute 'skip_chrome_actions'" } [WARNING]: Could not match supplied host pattern, ignoring: skip_chrome_update PLAY [Update Chrome] *********************************************************** TASK [More task] *************************************************************** ok: [Windows1] => { "msg": "should run on prev success" } ok: [Windows2] => { "msg": "should run on prev success" } TASK [More task2] ************************************************************** changed: [Windows1] => {"changed": true, "debug": [], "error": [], "host_err": "", "host_out": "", "information": [], "output": ["SM835222"], "result": {}, "verbose": [], "warning": []} changed: [Windows2] => {"changed": true, "debug": [], "error": [], "host_err": "", "host_out": "", "information": [], "output": ["SM834069"], "result": {}, "verbose": [], "warning": []} PLAY RECAP ********************************************************************* Windows2 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 Windows1 : ok=5 changed=2 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0