CORD-654 configure MAAS via ansible module

Change-Id: I9f1b3b83b41d2cf1e87660d4a50fd3267ad1679a
diff --git a/roles/compute-node/files/delete-fabric-config b/roles/compute-node/files/delete-fabric-config
index 4ddce2d..fbe2f5c 100755
--- a/roles/compute-node/files/delete-fabric-config
+++ b/roles/compute-node/files/delete-fabric-config
@@ -2,4 +2,4 @@
 
 CFG=${1:-"config-tibit.json"}
 
-curl -v -XDELETE -H Content-Type: application/json http://karaf:karaf@onos-fabric:8181/onos/v1/network/configuration 
+curl -v -XDELETE -H Content-Type: application/json http://karaf:karaf@onos-fabric:8181/onos/v1/network/configuration
diff --git a/roles/head-node/files/commands/cord b/roles/head-node/files/commands/cord
index 0d63dc8..9f675d5 100755
--- a/roles/head-node/files/commands/cord
+++ b/roles/head-node/files/commands/cord
@@ -16,7 +16,7 @@
     for CMD in $ALL_FILES; do
         test -x "$CMD" && COMMANDS="$COMMANDS $CMD"
     done
-       
+
     # Process comands for usage information
     # Output all commands and their help information to file
     # so it can be sorted. The format will be:
diff --git a/roles/head-node/files/commands/cord-harvest b/roles/head-node/files/commands/cord-harvest
index fd43182..8cabd11 100755
--- a/roles/head-node/files/commands/cord-harvest
+++ b/roles/head-node/files/commands/cord-harvest
@@ -50,7 +50,7 @@
         fi
         ;;
     check)
-        RUNNING=$($SSH_OPT docker inspect --format="'{{ .State.Running }}'" harvester) 
+        RUNNING=$($SSH_OPT docker inspect --format="'{{ .State.Running }}'" harvester)
 	if [ $? -ne 0 ]; then
 	    >&2 echo "Unable to execute docker or locate harvester container, if not running on the head node please specify the server address"
 	    exit 1
@@ -112,4 +112,4 @@
         exit 1
         ;;
 esac
-  
+
diff --git a/roles/maas/tasks/config-maas.yml b/roles/maas/tasks/config-maas.yml
new file mode 100644
index 0000000..d7e0d1c
--- /dev/null
+++ b/roles/maas/tasks/config-maas.yml
@@ -0,0 +1,74 @@
+---
+- name: Ensure PIP
+  become: yes
+  apt:
+    name: python-pip
+    state: present
+
+- name: Ensure Prerequisites
+  become: yes
+  pip:
+    name: "{{item}}"
+    state: present
+  with_items:
+    - maasclient==0.3
+    - requests_oauthlib
+    - ipaddress
+
+- name: Ensure SSH Key
+  maas_sshkey:
+    key: '{{apikey.stdout}}'
+    maas: 'http://{{mgmt_ip_address.stdout}}/MAAS/api/1.0'
+    sshkey: '{{maas.user_sshkey}}'
+    state: present
+
+- name: Ensure Administrative Zone
+  maas_zone:
+    key: '{{apikey.stdout}}'
+    maas: 'http://{{mgmt_ip_address.stdout}}/MAAS/api/1.0'
+    name: administrative
+    state: present
+
+- name: Ensure Cluster
+  maas_cluster:
+    key: '{{apikey.stdout}}'
+    maas: 'http://{{mgmt_ip_address.stdout}}/MAAS/api/1.0'
+    name: 'Cluster master'
+    status: enabled
+    domain: '{{maas.domain}}'
+
+- name: Ensure Management Interface
+  maas_cluster_interface:
+    key: '{{apikey.stdout}}'
+    maas: 'http://{{mgmt_ip_address.stdout}}/MAAS/api/1.0'
+    cluster_name: 'Cluster master'
+    name: '{{interfaces.management}}'
+    interface: '{{interfaces.management}}'
+    management: dhcpdns
+    ip: "{{networks.management | ipaddr(1) | ipaddr('address')}}"
+    subnet_mask: "{{networks.management | ipaddr('netmask')}}"
+    broadcast_ip: "{{networks.management | ipaddr('broadcast')}}"
+    ip_range_low: '{{ranges.management.low}}'
+    ip_range_high: '{{ranges.management.high}}'
+    state: present
+
+- name: Ensure Subnet Configuration
+  maas_subnet:
+    key: '{{apikey.stdout}}'
+    maas: 'http://{{mgmt_ip_address.stdout}}/MAAS/api/1.0'
+    name: '{{networks.management}}'
+    gateway_ip: "{{networks.management | ipaddr(1) | ipaddr('address')}}"
+    dns_servers: "{{networks.management | ipaddr(1) | ipaddr('address')}}"
+
+- name: Ensure Upstream DNS Server
+  maas:
+    key: '{{apikey.stdout}}'
+    maas: 'http://{{mgmt_ip_address.stdout}}/MAAS/api/1.0'
+    upstream_dns: '{{maas.upstream_dns}}'
+    state: present
+
+- name: Ensure Boot Resources
+  maas_boot_resource:
+    key: '{{apikey.stdout}}'
+    maas: 'http://{{mgmt_ip_address.stdout}}/MAAS/api/1.0'
+    state: import
diff --git a/roles/maas/tasks/main.yml b/roles/maas/tasks/main.yml
index fd136b1..98ac070 100644
--- a/roles/maas/tasks/main.yml
+++ b/roles/maas/tasks/main.yml
@@ -182,18 +182,8 @@
   tags:
     - maas_restart
 
-#- name: Ensure latest bootstrap image
-#  become: yes
-#  docker_image:
-#    name: localhost:5000/cord-maas-bootstrap:{{ docker.tag }}
-#    pull: yes
-
-- name: Configure MAAS
-  become: yes
-  command: docker run docker-registry:5000/cord-maas-bootstrap:{{ docker.tag }} --apikey='{{apikey.stdout}}' --sshkey='{{maas.user_sshkey}}' --url='http://{{mgmt_ip_address.stdout}}/MAAS/api/1.0' --network='{{networks.management}}' --network-low='{{ranges.management.low}}' --network-high='{{ranges.management.high}}' --interface='{{interfaces.management}}' --zone='administrative' --cluster='Cluster master' --domain='{{maas.domain}}'
-  register: maas_config_result
-  changed_when: maas_config_result.stdout.find("CHANGED") != -1
-  failed_when: "maas_config_result.rc != 0 or 'ERROR' in maas_config_result.stdout"
+- name: Ensure MAAS Configuration
+  include: config-maas.yml
 
 - name: Custom MAAS Configuration Template
   become: yes
@@ -264,9 +254,10 @@
     - maas_restart
 
 - name: Ensure Boot Resource Import Started
-  become: yes
-  shell: maas login cord http://localhost/MAAS/api/1.0/ "{{apikey.stdout}}" && maas cord boot-resources import && maas logout cord
-  changed_when: true
+  maas_boot_resource:
+    key: '{{apikey.stdout}}'
+    maas: 'http://{{mgmt_ip_address.stdout}}/MAAS/api/1.0'
+    state: import
 
 - name: Ensure VirtualBox Power Management
   include: virtualbox.yml
diff --git a/roles/maas/vars/main.yml b/roles/maas/vars/main.yml
index 59bee09..cb3a397 100644
--- a/roles/maas/vars/main.yml
+++ b/roles/maas/vars/main.yml
@@ -21,6 +21,7 @@
     # CHANGE:
     #   'domain' specifies the domain name configured in to MAAS
     domain: "{{ domain | default('cord.lab') }}"
+    upstream_dns: "{{ upstream_dns | default('8.8.8.8 8.8.8.4') }}"
 
 interfaces:
     # CHANGE:
diff --git a/roles/onos-fabric/files/bin/ping-test.sh b/roles/onos-fabric/files/bin/ping-test.sh
index d7b894e..4bbcef9 100755
--- a/roles/onos-fabric/files/bin/ping-test.sh
+++ b/roles/onos-fabric/files/bin/ping-test.sh
@@ -2,7 +2,7 @@
 
 HOSTS="10.3.1.1 10.3.1.2 10.3.2.1 10.3.2.2 10.3.1.254 10.3.2.254 192.168.10.1 8.8.8.8"
 
-ME=$(ifconfig | grep "10\.3\.[0-9]\.[0-9]" | sed -e 's/.*addr:\(10\.3\.[0-9]\.[0-9]\).*/\1/g' 2> /dev/null) 
+ME=$(ifconfig | grep "10\.3\.[0-9]\.[0-9]" | sed -e 's/.*addr:\(10\.3\.[0-9]\.[0-9]\).*/\1/g' 2> /dev/null)
 echo "FROM: $ME"
 for TO in $HOSTS; do
     T=$(ping -q -c 1 -W 1 -I eth0 $TO | grep rtt | awk '{print $4}' | sed -e 's|/| |g') #sed -e 's|r| |')
diff --git a/roles/onos-fabric/templates/fabric-network-config.json.j2 b/roles/onos-fabric/templates/fabric-network-config.json.j2
index f39cf60..f29134c 100644
--- a/roles/onos-fabric/templates/fabric-network-config.json.j2
+++ b/roles/onos-fabric/templates/fabric-network-config.json.j2
@@ -212,7 +212,7 @@
         "org.onosproject.core" : {
             "core" : {
                 "linkDiscoveryMode" : "STRICT" // enable strict link validation
-            }    
+            }
         }
     }
 }