---
- name: List virtual networks
  virt_net:
    command: list_nets
  register: net_exists

- name: Display networks
  debug:
    msg: "{{ lookup('template','network.xml.j2') }}"
  when: item.name not in net_exists.list_nets
  with_items: "{{ host_interfaces }}"

- name: Steps to setup network using Libvirt
  block:
    - name: Define virtual networks
      virt_net:
        command: define
        name: "{{ item.name }}"
        xml: '{{ lookup("template", "network.xml.j2") }}'
      with_items: "{{ host_interfaces }}"
    - name: Autostart virtual networks
      virt_net:
        autostart: True
        name: "{{ item.name }}"
      with_items: "{{ host_interfaces }}"
    - name: Start virtual networks
      virt_net:
        state: active
        autostart: True
        name: "{{ item.name }}"
      with_items: "{{ host_interfaces }}"
  when: item.name not in net_exists.list_nets

---
- name: List virtual networks
  virt_net:
    command: list_nets
  register: net_exists

- name: Collect facts about existing Libvirt networks
  virt_net:
    command: facts

- name: Display networks
  debug:
    msg: "{{ lookup('template','network.xml.j2') }}"
  when: item.name not in net_exists.list_nets
  with_items: "{{ host_interfaces }}"

- name: Steps to setup network using Libvirt
  block:
    - name: Define virtual networks
      virt_net:
        command: define
        name: "{{ item.name }}"
        xml: '{{ lookup("template", "network.xml.j2") }}'
      with_items: "{{ host_interfaces }}"
    - name: Autostart virtual networks
      virt_net:
        autostart: True
        name: "{{ item.name }}"
      with_items: "{{ host_interfaces }}"
    - name: Start virtual networks
      virt_net:
        state: active
        autostart: True
        name: "{{ item.name }}"
      with_items: "{{ host_interfaces }}"
  when: item.name in ansible_libvirt_networks and ansible_libvirt_networks[item.name].state == 'inactive'

Fix setup Libvirt network when network was inactive
Fix setup Libvirt network when network was inactive
