Add Ansible roles for OpenStack hosts 52/109252/6
authorPawel Wieczorek <p.wieczorek2@samsung.com>
Tue, 16 Jun 2020 10:05:14 +0000 (12:05 +0200)
committerBartek Grzybowski <b.grzybowski@partner.samsung.com>
Thu, 30 Jul 2020 09:03:11 +0000 (09:03 +0000)
Key pair is generated upon host creation and removed after the host is
destroyed.

This patch is based on previous work by:
Krzysztof Opasiak <k.opasiak@samsung.com>

Issue-ID: INT-1601
Change-Id: I9acd0b68a3ee79a0d710c40e0a1cc8470dfacce5
Signed-off-by: Pawel Wieczorek <p.wieczorek2@samsung.com>
21 files changed:
deployment/noheat/infra-openstack/ansible/create.yml
deployment/noheat/infra-openstack/ansible/destroy.yml
deployment/noheat/infra-openstack/ansible/group_vars/all.yml
deployment/noheat/infra-openstack/ansible/roles/openstack/create_devstack_hosts/tasks/create_host.yml [new file with mode: 0644]
deployment/noheat/infra-openstack/ansible/roles/openstack/create_devstack_hosts/tasks/main.yml [new file with mode: 0644]
deployment/noheat/infra-openstack/ansible/roles/openstack/create_devstack_keypair/tasks/main.yml [new file with mode: 0644]
deployment/noheat/infra-openstack/ansible/roles/openstack/destroy_devstack_hosts/tasks/destroy_host.yml [new file with mode: 0644]
deployment/noheat/infra-openstack/ansible/roles/openstack/destroy_devstack_hosts/tasks/main.yml [new file with mode: 0644]
deployment/noheat/infra-openstack/ansible/roles/openstack/destroy_devstack_keypair/tasks/main.yml [new file with mode: 0644]
deployment/noheat/infra-openstack/vagrant/test/create_host.stderr [new file with mode: 0644]
deployment/noheat/infra-openstack/vagrant/test/create_host.stdout [new file with mode: 0644]
deployment/noheat/infra-openstack/vagrant/test/create_host.test [new file with mode: 0755]
deployment/noheat/infra-openstack/vagrant/test/create_keypair.stderr [new file with mode: 0644]
deployment/noheat/infra-openstack/vagrant/test/create_keypair.stdout [new file with mode: 0644]
deployment/noheat/infra-openstack/vagrant/test/create_keypair.test [new file with mode: 0755]
deployment/noheat/infra-openstack/vagrant/test/destroy_host.stderr [new file with mode: 0644]
deployment/noheat/infra-openstack/vagrant/test/destroy_host.stdout [new file with mode: 0644]
deployment/noheat/infra-openstack/vagrant/test/destroy_host.test [new file with mode: 0755]
deployment/noheat/infra-openstack/vagrant/test/destroy_keypair.stderr [new file with mode: 0644]
deployment/noheat/infra-openstack/vagrant/test/destroy_keypair.stdout [new file with mode: 0644]
deployment/noheat/infra-openstack/vagrant/test/destroy_keypair.test [new file with mode: 0755]

index dd21271..a2665f9 100644 (file)
@@ -5,3 +5,5 @@
   gather_facts: False
   roles:
     - openstack/create_devstack_network
+    - openstack/create_devstack_keypair
+    - openstack/create_devstack_hosts
index b63e070..4576125 100644 (file)
@@ -4,4 +4,6 @@
   connection: local
   gather_facts: False
   roles:
+    - openstack/destroy_devstack_hosts
+    - openstack/destroy_devstack_keypair
     - openstack/destroy_devstack_network
index 68497a0..1da1e8f 100644 (file)
@@ -1,4 +1,14 @@
 ---
 network:
-  name: "onap_ci_lab"
+  name: &network_name "onap_ci_lab"
   cidr: "192.168.1.0/24"
+
+keypair:
+  name: &keypair_name "onap_ci_lab"
+
+hosts:
+  - name: "operator0"
+    image: "cirros-0.5.1-x86_64-disk"
+    flavor: "cirros256"
+    keypair: *keypair_name
+    network: *network_name
diff --git a/deployment/noheat/infra-openstack/ansible/roles/openstack/create_devstack_hosts/tasks/create_host.yml b/deployment/noheat/infra-openstack/ansible/roles/openstack/create_devstack_hosts/tasks/create_host.yml
new file mode 100644 (file)
index 0000000..847150f
--- /dev/null
@@ -0,0 +1,9 @@
+---
+- name: Create host
+  os_server:
+    state: present
+    name: "{{ host.name }}"
+    image: "{{ host.image }}"
+    flavor: "{{ host.flavor }}"
+    key_name: "{{ host.keypair }}"
+    network: "{{ host.network }}"
diff --git a/deployment/noheat/infra-openstack/ansible/roles/openstack/create_devstack_hosts/tasks/main.yml b/deployment/noheat/infra-openstack/ansible/roles/openstack/create_devstack_hosts/tasks/main.yml
new file mode 100644 (file)
index 0000000..f79314c
--- /dev/null
@@ -0,0 +1,4 @@
+---
+- include: create_host.yml host={{ item }}
+  with_items:
+    - "{{ hosts }}"
diff --git a/deployment/noheat/infra-openstack/ansible/roles/openstack/create_devstack_keypair/tasks/main.yml b/deployment/noheat/infra-openstack/ansible/roles/openstack/create_devstack_keypair/tasks/main.yml
new file mode 100644 (file)
index 0000000..4ac8a48
--- /dev/null
@@ -0,0 +1,19 @@
+- name: Create keypair
+  os_keypair:
+    state: present
+    name: "{{ keypair.name }}"
+  register: keypair
+
+- name: Create local public key
+  local_action:
+    module: copy
+    content: "{{ keypair.key.public_key }}"
+    dest: "~/.ssh/{{ keypair.key.name }}.pub"
+    mode: 0600
+
+- name: Create local private key
+  local_action:
+    module: copy
+    content: "{{ keypair.key.private_key }}"
+    dest: "~/.ssh/{{ keypair.key.name }}"
+    mode: 0600
diff --git a/deployment/noheat/infra-openstack/ansible/roles/openstack/destroy_devstack_hosts/tasks/destroy_host.yml b/deployment/noheat/infra-openstack/ansible/roles/openstack/destroy_devstack_hosts/tasks/destroy_host.yml
new file mode 100644 (file)
index 0000000..e9cedce
--- /dev/null
@@ -0,0 +1,5 @@
+---
+- name: Destroy host
+  os_server:
+    name: "{{ host.name }}"
+    state: absent
diff --git a/deployment/noheat/infra-openstack/ansible/roles/openstack/destroy_devstack_hosts/tasks/main.yml b/deployment/noheat/infra-openstack/ansible/roles/openstack/destroy_devstack_hosts/tasks/main.yml
new file mode 100644 (file)
index 0000000..eddf097
--- /dev/null
@@ -0,0 +1,4 @@
+---
+- include: destroy_host.yml host={{ item }}
+  with_items:
+    - "{{ hosts }}"
diff --git a/deployment/noheat/infra-openstack/ansible/roles/openstack/destroy_devstack_keypair/tasks/main.yml b/deployment/noheat/infra-openstack/ansible/roles/openstack/destroy_devstack_keypair/tasks/main.yml
new file mode 100644 (file)
index 0000000..b106ee0
--- /dev/null
@@ -0,0 +1,12 @@
+- name: Destroy keypair
+  os_keypair:
+    state: absent
+    name: "{{ keypair.name }}"
+
+- name: Destroy local keypair
+  file:
+    state: absent
+    path: "{{ item }}"
+  with_items:
+    - "~/.ssh/{{ keypair.name }}.pub"
+    - "~/.ssh/{{ keypair.name }}"
diff --git a/deployment/noheat/infra-openstack/vagrant/test/create_host.stderr b/deployment/noheat/infra-openstack/vagrant/test/create_host.stderr
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/deployment/noheat/infra-openstack/vagrant/test/create_host.stdout b/deployment/noheat/infra-openstack/vagrant/test/create_host.stdout
new file mode 100644 (file)
index 0000000..25c23dd
--- /dev/null
@@ -0,0 +1 @@
+"operator0"
diff --git a/deployment/noheat/infra-openstack/vagrant/test/create_host.test b/deployment/noheat/infra-openstack/vagrant/test/create_host.test
new file mode 100755 (executable)
index 0000000..d36e288
--- /dev/null
@@ -0,0 +1,21 @@
+#!/bin/sh
+
+export HOST_NAME='operator0'
+
+export VAGRANT_CWD='..'
+
+set_up() {
+    vagrant up --provision-with=run_playbook_destroy
+    vagrant up --provision-with=run_playbook_create
+}
+
+check() {
+    local host="$1"
+    vagrant ssh operator --no-tty -c \
+        "export OS_CLOUD=openstack; openstack server list -fcsv" \
+        | grep "$host" \
+        | cut -d',' -f2
+}
+
+set_up >/dev/null # drop provisioning output
+check "$HOST_NAME"
diff --git a/deployment/noheat/infra-openstack/vagrant/test/create_keypair.stderr b/deployment/noheat/infra-openstack/vagrant/test/create_keypair.stderr
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/deployment/noheat/infra-openstack/vagrant/test/create_keypair.stdout b/deployment/noheat/infra-openstack/vagrant/test/create_keypair.stdout
new file mode 100644 (file)
index 0000000..3638253
--- /dev/null
@@ -0,0 +1 @@
+"onap_ci_lab"
diff --git a/deployment/noheat/infra-openstack/vagrant/test/create_keypair.test b/deployment/noheat/infra-openstack/vagrant/test/create_keypair.test
new file mode 100755 (executable)
index 0000000..3e1dbfe
--- /dev/null
@@ -0,0 +1,21 @@
+#!/bin/sh
+
+export KEYPAIR_NAME='onap_ci_lab'
+
+export VAGRANT_CWD='..'
+
+set_up() {
+    vagrant up --provision-with=run_playbook_destroy
+    vagrant up --provision-with=run_playbook_create
+}
+
+check() {
+    local key="$1"
+    vagrant ssh operator --no-tty -c \
+        "export OS_CLOUD=openstack; openstack keypair list -fcsv" \
+        | grep "$key" \
+        | cut -d',' -f1
+}
+
+set_up >/dev/null # drop provisioning output
+check "$KEYPAIR_NAME"
diff --git a/deployment/noheat/infra-openstack/vagrant/test/destroy_host.stderr b/deployment/noheat/infra-openstack/vagrant/test/destroy_host.stderr
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/deployment/noheat/infra-openstack/vagrant/test/destroy_host.stdout b/deployment/noheat/infra-openstack/vagrant/test/destroy_host.stdout
new file mode 100644 (file)
index 0000000..30d7e15
--- /dev/null
@@ -0,0 +1 @@
+Host operator0 not found.
diff --git a/deployment/noheat/infra-openstack/vagrant/test/destroy_host.test b/deployment/noheat/infra-openstack/vagrant/test/destroy_host.test
new file mode 100755 (executable)
index 0000000..9db374e
--- /dev/null
@@ -0,0 +1,21 @@
+#!/bin/sh
+
+export HOST_NAME='operator0'
+
+export VAGRANT_CWD='..'
+
+set_up() {
+    vagrant up --provision-with=run_playbook_create
+    vagrant up --provision-with=run_playbook_destroy
+}
+
+check() {
+    local host="$1"
+    vagrant ssh operator --no-tty -c \
+        "export OS_CLOUD=openstack; openstack server list -fcsv" \
+        | grep "$host" \
+        || echo "Host ${host} not found."
+}
+
+set_up >/dev/null # drop provisioning output
+check "$HOST_NAME"
diff --git a/deployment/noheat/infra-openstack/vagrant/test/destroy_keypair.stderr b/deployment/noheat/infra-openstack/vagrant/test/destroy_keypair.stderr
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/deployment/noheat/infra-openstack/vagrant/test/destroy_keypair.stdout b/deployment/noheat/infra-openstack/vagrant/test/destroy_keypair.stdout
new file mode 100644 (file)
index 0000000..df6e492
--- /dev/null
@@ -0,0 +1 @@
+Keypair onap_ci_lab not found.
diff --git a/deployment/noheat/infra-openstack/vagrant/test/destroy_keypair.test b/deployment/noheat/infra-openstack/vagrant/test/destroy_keypair.test
new file mode 100755 (executable)
index 0000000..e809893
--- /dev/null
@@ -0,0 +1,21 @@
+#!/bin/sh
+
+export KEYPAIR_NAME='onap_ci_lab'
+
+export VAGRANT_CWD='..'
+
+set_up() {
+    vagrant up --provision-with=run_playbook_create
+    vagrant up --provision-with=run_playbook_destroy
+}
+
+check() {
+    local key="$1"
+    vagrant ssh operator --no-tty -c \
+        "export OS_CLOUD=openstack; openstack keypair list -fcsv" \
+        | grep "$key" \
+        || echo "Keypair ${key} not found."
+}
+
+set_up >/dev/null # drop provisioning output
+check "$KEYPAIR_NAME"