From 675378ba8aed72a96fa4f1ec753a5be62d97ac3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C5=A1per=20Fele-=C5=BDor=C5=BE?= Date: Wed, 13 Aug 2025 23:54:56 +0200 Subject: [PATCH] Improve os detection --- roles/facts/tasks/main.yml | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/roles/facts/tasks/main.yml b/roles/facts/tasks/main.yml index 756f9a1..4b5c6fe 100644 --- a/roles/facts/tasks/main.yml +++ b/roles/facts/tasks/main.yml @@ -1,31 +1,21 @@ -- name: Try connecting to linux - ping: - register: lin_ping_data - ignore_unreachable: true - ignore_errors: true +- name: Send valid bash or ps command with different results + raw: | + echo \#\# + register: raw_result -- name: Set shell to powershell +- name: Set shell type set_fact: - ansible_shell_type: powershell - when: lin_ping_data is undefined or lin_ping_data.unreachable is defined - -- name: Try connecting to windows - win_ping: - register: win_ping_data - ignore_unreachable: true - ignore_errors: true + ansible_shell_type: "{% if raw_result['stdout'][-4] == '#' %}sh{% else %}powershell{%endif%}" - name: Set connection parameters and paths for Windows become: false set_fact: - ansible_shell_type: powershell - ansible_connection: ssh ansible_become_method: runas ansible_become_flags: "" ansible_become_user: "{{ windows_user }}" logon_type: interactive installers: \\ucilnicesmb.fri1.uni-lj.si\ucilnice_d\install - when: win_ping_data is defined and win_ping_data.ping is defined + when: ansible_shell_type == "powershell" - name: Set connection parameters and paths for Ubuntu set_fact: @@ -33,4 +23,4 @@ ansible_become_flags: "" ansible_ssh_extra_args: '-o ForwardX11=yes' installers: /mnt/ucilnice_d/install - when: lin_ping_data is defined and lin_ping_data.ping is defined + when: ansible_shell_type == "sh"