---

# this task list is not complete because Ansible openstack modules 
#   are not installed with this version of Ansible.

# system test for new compute node install
- name: ping OpenStack controller node
  ping:


- name: create network
  become: no
  os_network:
    name: mynetwork
    auth:
      auth_url: "{{ os_auth_url }}"
      project_domain_id: "{{ os_project_domain_id }}" 
      user_domain_id: "{{ os_user_domain_id }}"
      username: "{{ os_username }}"
      password: "{{ os_password }}"
      project_name: "{{ os_project_name }}"
  register: mynetwork_results


- name: dump network JSON
  debug:
    var: mynetwork_results.id

- name: create subnet
  become: no
  os_subnet:
    state: present
    network_name: mynetwork
    name: mynetwork_sub1
    cidr: 192.168.0.0/24
    auth:
      auth_url: "{{ os_auth_url }}"
      project_domain_id: "{{ os_project_domain_id }}" 
      user_domain_id: "{{ os_user_domain_id }}"
      username: "{{ os_username }}"
      password: "{{ os_password }}"
      project_name: "{{ os_project_name }}"

#- name: create router
#  os_router:
#    state: present
#    name: router1
#    auth:
#      auth_url: "{{ os_auth_url }}"
#      project_domain_id: "{{ os_project_domain_id }}" 
#      user_domain_id: "{{ os_user_domain_id }}"
#      username: "{{ os_username }}"
#      password: "{{ os_password }}"
#      project_name: "{{ os_project_name }}"
#    network: ext-net
#    interfaces:
#      - mynetwork_sub1

#    nics:
#      - net-id: "{{ mynetwork_results.id }}"
#      - net-name: mynetwork

- name: create new VM instance
  become: no
  os_server:
    state: present
    name: vm1
    image: e3835f3a-bfc2-4ac9-96ff-ac8abb50a643
    flavor: m1.small
    security_groups: default
    auto_ip: no
    nics:
      - net-id: "{{ mynetwork_results.id }}"
    meta:
      hostname: test1
    auth:
      auth_url: "{{ os_auth_url }}"
      project_domain_id: "{{ os_project_domain_id }}" 
      user_domain_id: "{{ os_user_domain_id }}"
      username: "{{ os_username }}"
      password: "{{ os_password }}"
      project_name: "{{ os_project_name }}"

- name: get new VM details
  become: no
  os_server_facts:
    server: vm1
    auth:
      auth_url: "{{ os_auth_url }}"
      project_domain_id: "{{ os_project_domain_id }}" 
      user_domain_id: "{{ os_user_domain_id }}"
      username: "{{ os_username }}"
      password: "{{ os_password }}"
      project_name: "{{ os_project_name }}"
  register: myvm_results


- name: dump info about my VM
  debug: msg="my VM  {{ myvm_results.ansible_facts.openstack_servers[0].name }} {{ myvm_results.ansible_facts.openstack_servers[0].id }} {{ myvm_results.ansible_facts.openstack_servers[0].status }}"


- name: remove the new VM instance
  become: no
  os_server:
    state: absent
    name: vm1
    image: e3835f3a-bfc2-4ac9-96ff-ac8abb50a643
    auth:
      auth_url: "{{ os_auth_url }}"
      project_domain_id: "{{ os_project_domain_id }}" 
      user_domain_id: "{{ os_user_domain_id }}"
      username: "{{ os_username }}"
      password: "{{ os_password }}"
      project_name: "{{ os_project_name }}"

- name: remove subnet
  become: no
  os_subnet:
    state: absent
    network_name: mynetwork
    name: mynetwork_sub1
    cidr: 192.168.0.0/24
    auth:
      auth_url: "{{ os_auth_url }}"
      project_domain_id: "{{ os_project_domain_id }}" 
      user_domain_id: "{{ os_user_domain_id }}"
      username: "{{ os_username }}"
      password: "{{ os_password }}"
      project_name: "{{ os_project_name }}"

# ignore errors for this task.  seems like os_network module has a bug
# and fails when I delete a network.
- name: remove network
  become: no
  os_network:
    name: mynetwork
    state: absent
    endpoint_type: internal
    timeout: 200
    wait: yes
    auth:
      auth_url: "{{ os_auth_url }}"
      project_domain_id: "{{ os_project_domain_id }}" 
      user_domain_id: "{{ os_user_domain_id }}"
      username: "{{ os_username }}"
      password: "{{ os_password }}"
  ignore_errors: yes
  register: mynet_results


- name: dump info about deleted network
  debug: msg="my network  {{ mynet_results }}"

