---
- name: Manage WebLogic Domain
  hosts: host_servers 
  become: yes
  become_user: oracle
  gather_facts: true
  vars:
    weblogic_home: "/u01/oracle/middleware"
    scripts_home: "/home/oracle/scripts"
    wlst: "{{ weblogic_home }}/oracle_common/common/bin/wlst.sh"
    force_stop: false
    admin_url: "t3://{{ hostvars[inventory_hostname].ansible_host }}:{{ hostvars[inventory_hostname].admin_port }}"

  tasks:
    - name: Ensure scripts directory exists
      file:
        path: "/home/oracle/scripts"
        state: directory

    - name: Copy Python scripts to target server
      copy:
        src: "{{ item }}"
        dest: "/home/oracle/scripts/{{ item | basename }}"
        mode: 0755  # Ensure executable permissions
      with_fileglob:
        - /home/osboxes/ossdy-project/scripts/*.py    

    - name: Stop Managed Server
      shell: "{{ wlst }} -i {{ scripts_home }}/stop_managed_servers.py {{ hostvars[inventory_hostname].admin_username }} {{ hostvars[inventory_hostname].admin_password }} {{ admin_url }} {{ force_stop }}"
      register: result_stop_all
      ignore_errors: true

    - debug:
        var: result_stop_all.stdout_lines

    - name: Check Admin Server Port
      command: "netstat -an | grep '{{ hostvars[inventory_hostname].admin_port }}' | grep LISTEN"
      register: check_adm_port
      ignore_errors: true

    - set_fact:
        stop_command: "{{ weblogic_home }}/user_projects/domains/{{ hostvars[inventory_hostname].domain }}/bin/stopWebLogic.sh &"

    - name: Stop Admin Server
      shell: "{{ stop_command }}"
      when: admin_port|string in check_adm_port.stdout

    - name: Wait for wl port to be unavailable
      wait_for:
        host: "{{ hostvars[inventory_hostname].ansible_host }}"
        port: "{{ hostvars[inventory_hostname].admin_port }}"
        state: absent
        timeout: 800
      register: wait_adm

    - debug:
        var: wait_adm is succeeded

    - name: Check Node Manager Port
      command: "netstat -an | grep '{{ hostvars[inventory_hostname].node_manager_port }}' | grep LISTEN"
      register: check_nm_port
      ignore_errors: true

    - set_fact:
        stop_command: "{{ weblogic_home }}/user_projects/domains/{{ hostvars[inventory_hostname].domain }}/bin/stopNodeManager.sh &"

    - name: Stop Node Manager
      shell: "{{ stop_command }}"
      when: node_manager_port|string in check_nm_port.stdout

    - name: Wait for nm port to be unavailable
      wait_for:
        host: "{{ hostvars[inventory_hostname].ansible_host }}"
        port: "{{ hostvars[inventory_hostname].node_manager_port }}"
        state: absent
        timeout: 800
      register: wait_nm

    - name: Delete scripts directory from target server
      file:
        path: "/home/oracle/scripts"
        state: absent



---------------------------------------------------------------


---
- name: Manage WebLogic Domain
  hosts: host_servers
  become: yes
  become_user: oracle
  gather_facts: true
  vars:
    weblogic_home: "/u01/oracle/middleware"
    scripts_home: "/home/oracle/scripts"
    wlst: "{{ weblogic_home }}/oracle_common/common/bin/wlst.sh"  
    force_stop: false
    admin_url: "t3://{{ hostvars[inventory_hostname].ansible_host }}:{{ hostvars[inventory_hostname].admin_port }}"

  tasks:
    - name: Ensure scripts directory exists or create
      file:
        path: "/home/oracle/scripts"
        state: directory

    - name: Copy Python scripts to target server
      copy:
        src: "{{ item }}"
        dest: "/home/oracle/scripts/{{ item | basename }}"
        mode: 0755  # Ensure executable permissions
      with_fileglob:
        - /home/osboxes/ossdy-project/scripts/*.py

    - name: Check Node Manager Port
      command: "netstat -an | grep '{{ hostvars[inventory_hostname].node_manager_port }}' | grep LISTEN"
      register: check_nm_port
      ignore_errors: true

    - set_fact:
        start_command: "{{ weblogic_home }}/user_projects/domains/{{ hostvars[inventory_hostname].domain }}/bin/startNodeManager.sh &"

    - name: start Node Manager
      shell: "{{ start_command }}"
      when: node_manager_port|string not in check_nm_port.stdout

    - name: Wait for nm port to be available
      wait_for:
        host: "{{ hostvars[inventory_hostname].ansible_host }}"
        port: "{{ hostvars[inventory_hostname].node_manager_port }}"
        timeout: 800
      register: wait_nm

    - debug:
        var: wait_nm is succeeded

    - name: Check WebLogic Port
      command: "netstat -an | grep '{{ hostvars[inventory_hostname].admin_port }}' | grep LISTEN"
      register: check_wl_port
      ignore_errors: true

    - set_fact:
        start_command: "{{ weblogic_home }}/user_projects/domains/{{ hostvars[inventory_hostname].domain }}/bin/startWebLogic.sh &"

    - name: start Admin Server
      shell: "{{ start_command }}"
      when: admin_port|string not in check_wl_port.stdout

    - name: Wait for wl port to be available
      wait_for:
        host: "{{ hostvars[inventory_hostname].ansible_host }}"
        port: "{{ hostvars[inventory_hostname].admin_port }}"
        timeout: 800
      register: wait_adm

    - debug:
        var: wait_adm is succeeded

    - name: Start Managed Server
      shell: "{{ wlst }} -i {{ scripts_home }}/start_managed_servers.py {{ hostvars[inventory_hostname].admin_username }} {{ hostvars[inventory_hostname].admin_password }} {{ admin_url }}"
      register: result_start_all
      when: wait_adm is succeeded

    - debug:
        var: result_start_all.stdout_lines

    - name: Delete scripts directory from target server
      file:
        path: "/home/oracle/scripts"
        state: absent

Is it possible without set facts?
and i want one playbook with tags