add dynamic ansible inventory script
Change-Id: I3e8ba79d0d6ec8e66e5118638ec9325ee78d50ef
diff --git a/roles/head-node/files/pod-inventory b/roles/head-node/files/pod-inventory
new file mode 100644
index 0000000..29571de
--- /dev/null
+++ b/roles/head-node/files/pod-inventory
@@ -0,0 +1,26 @@
+#!/bin/bash
+
+maas login cord http://10.2.0.1/MAAS/api/1.0 '{{ apikey.stdout }}' > /dev/null
+NODES=$(maas cord nodes list | jq '.[].hostname' | sed -e 's/"//g')
+maas logout cord
+
+cat <<EO_HEAD
+{
+ "head" : {
+ "hosts" : [
+ "$(hostname --long)"
+ ]
+ },
+ "compute" : {
+ "hosts" : [
+EO_HEAD
+
+for i in $(echo $NODES | sed -e 's/\([a-zA-Z0-9_-.]*\)/"\1"/g' -e 's/ /, /g'); do
+echo " $i"
+done
+
+cat << EO_TAIL
+ ]
+ }
+}
+EO_TAIL
diff --git a/roles/head-node/tasks/main.yml b/roles/head-node/tasks/main.yml
index 6735f20..d8a535e 100644
--- a/roles/head-node/tasks/main.yml
+++ b/roles/head-node/tasks/main.yml
@@ -44,6 +44,12 @@
group=maas
mode=0755
+- name: Capture MAAS API Key
+ become: yes
+ command: maas-region-admin apikey --username={{ maas.user }}
+ register: apikey
+ changed_when: false
+
- name: Ensure Compute Node Playbook
become: yes
template:
@@ -55,3 +61,12 @@
with_items:
- compute-node.yml
- switch-node.yml
+
+- name: Ensure Dynamic Inventory Script
+ become: yes
+ template:
+ src=files/pod-inventory
+ dest=/etc/maas/ansible/pod-inventory
+ owner=maas
+ group=maas
+ mode=0755
diff --git a/roles/head-node/vars/main.yml b/roles/head-node/vars/main.yml
index e97daf1..0093928 100644
--- a/roles/head-node/vars/main.yml
+++ b/roles/head-node/vars/main.yml
@@ -7,3 +7,6 @@
location: "{{ prov_location | default('http://gerrit.opencord.org/maas') }}"
role_path: "{{ prov_role_path | default('roles') }}"
role: "{{ prov_role | default('compute-node') }}"
+
+maas:
+ user: "{{ maas_user | default('cord') }}"