Parcourir la source

disk mount setups

Chris Mague il y a 6 ans
Parent
commit
571a5587a1
4 fichiers modifiés avec 115 ajouts et 2 suppressions
  1. 44 0
      ansible/playbook.yml
  2. 2 0
      inventory.tpl
  3. 57 2
      provisioning.tf
  4. 12 0
      ssh.tpl

+ 44 - 0
ansible/playbook.yml

@@ -0,0 +1,44 @@
+---
+
+- hosts: all
+  become: yes
+  become_user: root
+  become_method: sudo
+  gather_facts: yes
+
+  pre_tasks:
+    - name: Update Apt Cache
+      apt: update_cache=yes cache_valid_time=86400
+      when: ansible_os_family == "Debian"
+    - name: Ubuntu Packages
+      apt: >
+        pkg={{item}}
+        state=present
+      with_items:
+        - jq
+    - name: create re home dir
+      file:
+        state=directory
+        path="/redis"
+    - name: create ephemeral filesystem
+      filesystem:
+        fstype=ext4
+        dev="/dev/xvdh"
+    - name: mount ephemeral dir filesystem
+      mount:
+        name: /redis/ephemeral
+        src: /dev/xvdh
+        fstype: ext4
+        state: mounted
+    - name: create persistant filesystem
+      filesystem:
+        fstype=ext4
+        dev="/dev/xvdj"
+    - name: mount persistant dir filesystem
+      mount:
+        name: /redis/persistant
+        src: /dev/xvdj
+        fstype: ext4
+        state: mounted
+
+

+ 2 - 0
inventory.tpl

@@ -0,0 +1,2 @@
+[all]
+${host_ip} ansible_ssh_common_args='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -F /tmp/${vpc_name}_node_${ncount}.cfg'

+ 57 - 2
provisioning.tf

@@ -1,3 +1,5 @@
+##########################################################################################
+# ensure we can get to the node first
 resource "null_resource" "remote-config" {
   count = var.data-node-count
   provisioner "remote-exec" {
@@ -7,7 +9,60 @@ resource "null_resource" "remote-config" {
       private_key = "${file("~/.ssh/${var.vpc-name}.pem")}"
       agent       = true
     }
-    inline = ["hostname"]
+    inline = ["sudo apt update && sudo apt install -y python python-pip"]
   }
-  depends_on = ["aws_instance.re", "aws_eip_association.re-eip-assoc"]
+  depends_on = ["aws_instance.re", "aws_eip_association.re-eip-assoc", "null_resource.inventory-setup", "null_resource.ssh-setup"]
 }
+
+###############################################################################
+# Template Data
+data "template_file" "ansible_inventory" {
+  count = var.data-node-count
+  template = "${file("${path.module}/inventory.tpl")}"
+  vars = {
+    host_ip  = "${element(aws_instance.re.*.public_ip, count.index)}"
+    vpc_name = "${var.vpc-name}"
+    ncount   = "${count.index}"
+  }
+}
+
+data "template_file" "ssh_config" {
+  count = var.data-node-count
+  template = "${file("${path.module}/ssh.tpl")}"
+  vars = {
+    host_ip  = "${element(aws_instance.re.*.public_ip, count.index)}"
+    vpc_name = "${var.vpc-name}"
+    ncount   = "${count.index}"
+  }
+}
+
+###############################################################################
+# Template Write
+resource "null_resource" "inventory-setup" {
+  count = var.data-node-count
+  provisioner "local-exec" {
+    command = "echo \"${element(data.template_file.ansible_inventory.*.rendered, count.index)}\" > /tmp/${var.vpc-name}_node_${count.index}.ini"
+  }
+  depends_on = ["data.template_file.ansible_inventory"]
+}
+
+resource "null_resource" "ssh-setup" {
+  count = var.data-node-count
+  provisioner "local-exec" {
+    command = "echo \"${element(data.template_file.ssh_config.*.rendered, count.index)}\" > /tmp/${var.vpc-name}_node_${count.index}.cfg"
+  }
+  depends_on = ["data.template_file.ssh_config"]
+}
+
+###############################################################################
+# Run some ansible
+resource "null_resource" "ansible-run" {
+  count = var.data-node-count
+  provisioner "local-exec" {
+    command = "ansible-playbook ${path.module}/ansible/playbook.yml --private-key ~/.ssh/${var.vpc-name}.pem -i /tmp/${var.vpc-name}_node_${count.index}.ini --become -e 'MYENV=1'"
+  }
+  depends_on = ["null_resource.remote-config"]
+}
+
+
+

+ 12 - 0
ssh.tpl

@@ -0,0 +1,12 @@
+Host ${host_ip}
+  User ubuntu
+  IdentityFile ~/.ssh/${vpc_name}.pem
+  ForwardAgent yes
+  GSSAPIAuthentication no
+  VerifyHostKeyDNS no
+  HashKnownHosts no
+  TCPKeepAlive yes
+  ServerAliveInterval 300
+  StrictHostKeyChecking no
+  UserKnownHostsFile=/dev/null
+  IdentitiesOnly yes