- name: Gather Node Manager Port Mappings for Each Domain
  set_fact:
    nm_ports_per_domain: >-
      {{
        nm_ports_per_domain | default({}) | combine(
          { domain: hostvars[item].domains[domain].node_manager_port }
        )
      }}
  loop: "{{ hostvars[host].domains | dict2items }}"
  loop_control:
    loop_var: domain
  vars:
    host: "{{ item }}"
  when: "'domains' in hostvars[item]"
  loop: "{{ groups['host_servers'] }}"
  tags: [always]

- name: Map Unique IPs to Node Manager Port Mappings
  set_fact:
    ip_to_nm_ports: >-
      {{
        ip_to_nm_ports | default({}) | combine(
          { hostvars[item].ansible_host: nm_ports_per_domain }
        )
      }}
  loop: "{{ groups['host_servers'] }}"
  tags: [always]
---------------
inventory.yml

all:
  children:
    host_servers:
      vars:
        ansible_user: oracle
        ansible_ssh_pass: "{{ default_ssh_pass }}"
      hosts:
        server1:
          ansible_host: 192.168.56.140
          ansible_ssh_pass: "{{ server1_ssh_pass }}"
          admin_password: "{{ server1_wl_pass }}"
          domains:
            base_domain:
              admin_username: weblogic
              admin_password: "{{ server1_base_domain_wl_pass }}"
              admin_port: 7001
              node_manager_port: 5556
            base_domain2:
              admin_username: weblogic2
              admin_password: "{{ server1_base_domain2_wl_pass }}"
              admin_port: 8001
              node_manager_port: 5557
        server2:
          ansible_host: 10.240.109.119
          ansible_ssh_pass: "{{ server2_ssh_pass }}"
          domains:
            adfint9_domain:
              admin_username: adfint9local
              admin_password: "{{ server2_adfint9_domain_wl_pass }}"
              admin_port: 7001
              node_manager_port: 5556
            adfint10_domain:
              node_manager_port: 5556
        server3:
          ansible_host: 10.240.109.120
          ansible_ssh_pass: "{{ server3_ssh_pass }}"
          domains:
            adfint10_domain:
              admin_username: adfint10local
              admin_password: "{{ server3_adfint10_domain_wl_pass }}"
              admin_port: 7001
              node_manager_port: 5556
            adfint9_domain:
              node_manager_port: 5556
-----------------------
error:

TASK [Gather Node Manager Port Mappings for Each Domain] ***********************
fatal: [server1]: FAILED! => {"msg": "The conditional check ''domains' in hostvars[item]' failed. The error was: error while evaluating conditional ('domains' in hostvars[item]): 'item' is undefined\n\nThe error appears to be in '/home/osboxes/ossdy-project3/playbook.yml': line 174, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: Gather Node Manager Port Mappings for Each Domain\n      ^ here\n"}
fatal: [server2]: FAILED! => {"msg": "The conditional check ''domains' in hostvars[item]' failed. The error was: error while evaluating conditional ('domains' in hostvars[item]): 'item' is undefined\n\nThe error appears to be in '/home/osboxes/ossdy-project3/playbook.yml': line 174, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: Gather Node Manager Port Mappings for Each Domain\n      ^ here\n"}
fatal: [server3]: FAILED! => {"msg": "The conditional check ''domains' in hostvars[item]' failed. The error was: error while evaluating conditional ('domains' in hostvars[item]): 'item' is undefined\n\nThe error appears to be in '/home/osboxes/ossdy-project3/playbook.yml': line 174, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: Gather Node Manager Port Mappings for Each Domain\n      ^ here\n"}