0

I am trying to put together a playbook that will connect to hosts and report back if the connection was successful or not. The result of the connection check should be stored in a variable. I had a crack at this by using the ansible facts, however I cannot workout how to handle unsuccessful connections to hosts.

---
  - name: Set Facts variables
    set_fact:
      server_domain: "{{ ansible_domain}}"
      server_ip: "{{ ansible_ip_addresses[1] }}"

  - name: Set PowerShell parameters
    set_fact:
      ps_params: '-server_ip "{{ ansible_ip_addresses[1] }}" -server_domain "{{ ansible_domain }}"'
  
  - name: Execute Script
    win_shell: "powershell.exe -file \\PSfile.ps1 {{ps_params}}"
user5544
  • 131
  • 2
  • 9
  • Regarding your question "_how do I log servers where ... not work?_" the answer is depending on your configuration and what you try to achieve; Ansible with [retry files](https://docs.ansible.com/ansible/latest/reference_appendices/config.html#retry-files-enabled) is already doing it for you. – U880D Jun 21 '21 at 04:34
  • I have edited the question. I just need to be able to check for successful or failed connection and store the result in a variable. – user5544 Jun 21 '21 at 15:29
  • A part of your question is answered under [How to use Ansible module wait_for together with loop?](https://stackoverflow.com/questions/57161961/) or [Test if a server is reachable from host and has port open with Ansible](https://stackoverflow.com/questions/39800368/). Other parts, like storing results in a variable or write results or variable values into a log file, I leave for you and your research here in SO. – U880D Jun 22 '21 at 04:22

1 Answers1

0

Try this:

---
- hosts: all
  gather_facts: yes
  ignore_errors: true
  ignore_unreachable: true
  vars:
# email_file        The filename to attach to the email
# email_recipient   email address to send the email to
# email_subject     text to appear in the subject line
    email_subject: "Windows Server Connection Report for {{ awx_inventory_name }}"
    email_file: ./failed_connect_report.csv
    ansible_port: 5985

  tasks:

  - name: Write header and ensure the email file exists
    lineinfile:
      path: "{{ email_file }}"
      line: 'VM Name,IP Address,Distribution,Port,Note,Port,Note'
      state: present
      mode: '0774'
      create: yes
      insertafter: EOF
    become: no
    delegate_to: localhost
    run_once: True
    when: email_recipient != ''

  - name: Test connectivity to port {{ ansible_port }}
    win_ping:
    register: result

  - name: Did we fail to talk to this host?
    block:
      - set_fact:
          ip_addresses: "unknown"
      - set_fact:
          distribution: "unknown"
      - name: Did we fail to talk to this host?
        set_fact:
          line1: "{{ inventory_hostname }},{{ ip_addresses }},{{ distribution }},{{ ansible_port }},FAILED - {{ result.msg | replace(',',' ') }}"
        delegate_to: localhost
      - set_fact:
          failed1: "True"
    when: ((result.unreachable is defined and
          result.unreachable == True) or
          (result.failed is defined and
          result.failed == True)) and
          email_recipient != ''


  - name: Log the success
    set_fact:
        line1: "{{ inventory_hostname }},{{ ansible_facts.ip_addresses.0 }},{{ ansible_facts.distribution }},{{ ansible_port }},SUCCESS"
    delegate_to: localhost
    when: (failed1 is not defined) and
          email_recipient != ''