Home:ALL Converter>Ansible playbook and return values during a failure

Ansible playbook and return values during a failure

Ask Time:2021-06-27T21:35:05         Author:user5544

Json Formatter

I have an ansible playbook as below, the objective is to run a ping check across a number of hosts, and the playbook should return if the ping was successful or not per host. This works by running a ping on a windows host to ascertain connectivity, the status of the connectivity is then passed onto a powershell script. The powershell script will record the result for analysis.

What I want the script to now do is report back if connectivity fails for a particular host, at the moment with the playbook, if host connectivity fails, the entire playbook errors. The idea is that if i have 10 hosts, and connectivity fails for 2 and works for 8 hosts, the failed ping response should be returned to the powershell script for 2 hosts, and 8 hosts should send a return value of connection successful to the powershell script.

---
  - name: Get host facts
    set_fact:
      serverdomain: "{{ansible_domain}}"
      server_ip: "{{ansible_ip_addresses[1]}}"

  - name: Host Ping Check
    ignore_errors: yes
    win_ping:
    register: var_ping

  - name: Get Host name
    debug: msg="{{the_host_name}}"
  
  - name: Set Execution File and parameters
    set_fact:
      scriptfile: "{{ansible_user_dir}}\\scripts\\host_check.ps1"
      params: "-servername '{{the_host_name}}' -response var_ping.failed"
  
  - name: Execute script
    win_command: powershell.exe "{{scriptfile}}" "{{params}}"

Author:user5544,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/68151735/ansible-playbook-and-return-values-during-a-failure
yy