Skip to content

Commit 5e032f2

Browse files
committed
Terraform code with local-exec.
1 parent f148535 commit 5e032f2

File tree

4 files changed

+230
-0
lines changed

4 files changed

+230
-0
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Local .terraform directories
2+
**/.terraform/*
3+
4+
# .tfstate files
5+
*.tfstate
6+
*.tfstate.*
7+
8+
# .tfvars files
9+
*.tfvars

terraform/main.tf

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
##############################################################################
2+
# Terraform and Ansible - Better Together
3+
4+
resource "azurerm_resource_group" "terraform_ansible" {
5+
name = "${var.resource_group}"
6+
location = "${var.location}"
7+
}
8+
9+
resource "azurerm_virtual_network" "vnet" {
10+
name = "${var.virtual_network_name}"
11+
location = "${azurerm_resource_group.terraform_ansible.location}"
12+
address_space = ["${var.address_space}"]
13+
resource_group_name = "${azurerm_resource_group.terraform_ansible.name}"
14+
}
15+
16+
resource "azurerm_subnet" "subnet" {
17+
name = "${var.prefix}subnet"
18+
virtual_network_name = "${azurerm_virtual_network.vnet.name}"
19+
resource_group_name = "${azurerm_resource_group.terraform_ansible.name}"
20+
address_prefix = "${var.subnet_prefix}"
21+
}
22+
23+
resource "azurerm_network_security_group" "tf-guide-sg" {
24+
name = "${var.prefix}-sg"
25+
location = "${var.location}"
26+
resource_group_name = "${azurerm_resource_group.terraform_ansible.name}"
27+
28+
security_rule {
29+
name = "HTTP"
30+
priority = 100
31+
direction = "Inbound"
32+
access = "Allow"
33+
protocol = "Tcp"
34+
source_port_range = "*"
35+
destination_port_range = "80"
36+
source_address_prefix = "*"
37+
destination_address_prefix = "*"
38+
}
39+
40+
security_rule {
41+
name = "SSH"
42+
priority = 101
43+
direction = "Inbound"
44+
access = "Allow"
45+
protocol = "Tcp"
46+
source_port_range = "*"
47+
destination_port_range = "22"
48+
source_address_prefix = "*"
49+
destination_address_prefix = "*"
50+
}
51+
}
52+
53+
resource "azurerm_network_interface" "tf-guide-nic" {
54+
name = "${var.prefix}tf-guide-nic"
55+
location = "${var.location}"
56+
resource_group_name = "${azurerm_resource_group.terraform_ansible.name}"
57+
network_security_group_id = "${azurerm_network_security_group.tf-guide-sg.id}"
58+
59+
ip_configuration {
60+
name = "${var.prefix}ipconfig"
61+
subnet_id = "${azurerm_subnet.subnet.id}"
62+
private_ip_address_allocation = "Dynamic"
63+
public_ip_address_id = "${azurerm_public_ip.tf-guide-pip.id}"
64+
}
65+
}
66+
67+
resource "azurerm_public_ip" "tf-guide-pip" {
68+
name = "${var.prefix}-ip"
69+
location = "${var.location}"
70+
resource_group_name = "${azurerm_resource_group.terraform_ansible.name}"
71+
public_ip_address_allocation = "Dynamic"
72+
domain_name_label = "${var.hostname}"
73+
}
74+
75+
resource "azurerm_virtual_machine" "site" {
76+
name = "${var.hostname}-site"
77+
location = "${var.location}"
78+
resource_group_name = "${azurerm_resource_group.terraform_ansible.name}"
79+
vm_size = "${var.vm_size}"
80+
81+
network_interface_ids = ["${azurerm_network_interface.tf-guide-nic.id}"]
82+
delete_os_disk_on_termination = "true"
83+
84+
storage_image_reference {
85+
publisher = "${var.image_publisher}"
86+
offer = "${var.image_offer}"
87+
sku = "${var.image_sku}"
88+
version = "${var.image_version}"
89+
}
90+
91+
storage_os_disk {
92+
name = "${var.hostname}-osdisk"
93+
managed_disk_type = "Standard_LRS"
94+
caching = "ReadWrite"
95+
create_option = "FromImage"
96+
}
97+
98+
os_profile {
99+
computer_name = "${var.hostname}"
100+
admin_username = "${var.admin_username}"
101+
admin_password = "${var.admin_password}"
102+
}
103+
104+
os_profile_linux_config {
105+
disable_password_authentication = false
106+
}
107+
108+
provisioner "local-exec" {
109+
command = "ansible-playbook -i '${self.public_ip},' --private-key ${var.ssh_key} apache.yml"
110+
}
111+
112+
}

terraform/outputs.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
##############################################################################
2+
# Outputs File
3+
#
4+
# Expose the outputs you want your users to see after a successful
5+
# `terraform apply` or `terraform output` command. You can add your own text
6+
# and include any data from the state file. Outputs are sorted alphabetically;
7+
# use an underscore _ to move things to the bottom.
8+
9+
output "_instructions" {
10+
value = "This output contains plain text. You can add variables too."
11+
}
12+
13+
output "public_dns" {
14+
value = "${azurerm_public_ip.tf-guide-pip.fqdn}"
15+
}
16+
17+
output "App_Server_URL" {
18+
value = "http://${azurerm_public_ip.tf-guide-pip.fqdn}"
19+
}

terraform/variables.tf

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
##############################################################################
2+
# Variables File
3+
#
4+
# Here is where we store the default values for all the variables used in our
5+
# Terraform code. If you create a variable with no default, the user will be
6+
# prompted to enter it (or define it via config file or command line flags.)
7+
8+
variable "ssh_key" {
9+
description = "SSH key for connecting to our Red Hat instance"
10+
}
11+
12+
variable "resource_group" {
13+
description = "The name of your Azure Resource Group."
14+
default = "Terraform-Azure-Beginners"
15+
}
16+
17+
variable "prefix" {
18+
description = "This prefix will be included in the name of some resources."
19+
default = "tfguide"
20+
}
21+
22+
variable "hostname" {
23+
description = "Virtual machine hostname. Used for local hostname, DNS, and storage-related names."
24+
default = "catapp"
25+
}
26+
27+
variable "location" {
28+
description = "The region where the virtual network is created."
29+
default = "centralus"
30+
}
31+
32+
variable "virtual_network_name" {
33+
description = "The name for your virtual network."
34+
default = "vnet"
35+
}
36+
37+
variable "address_space" {
38+
description = "The address space that is used by the virtual network. You can supply more than one address space. Changing this forces a new resource to be created."
39+
default = "10.0.0.0/16"
40+
}
41+
42+
variable "subnet_prefix" {
43+
description = "The address prefix to use for the subnet."
44+
default = "10.0.10.0/24"
45+
}
46+
47+
variable "storage_account_tier" {
48+
description = "Defines the storage tier. Valid options are Standard and Premium."
49+
default = "Standard"
50+
}
51+
52+
variable "storage_replication_type" {
53+
description = "Defines the replication type to use for this storage account. Valid options include LRS, GRS etc."
54+
default = "LRS"
55+
}
56+
57+
variable "vm_size" {
58+
description = "Specifies the size of the virtual machine."
59+
default = "Standard_A0"
60+
}
61+
62+
variable "image_publisher" {
63+
description = "Name of the publisher of the image (az vm image list)"
64+
default = "Canonical"
65+
}
66+
67+
variable "image_offer" {
68+
description = "Name of the offer (az vm image list)"
69+
default = "UbuntuServer"
70+
}
71+
72+
variable "image_sku" {
73+
description = "Image SKU to apply (az vm image list)"
74+
default = "16.04-LTS"
75+
}
76+
77+
variable "image_version" {
78+
description = "Version of the image to apply (az vm image list)"
79+
default = "latest"
80+
}
81+
82+
variable "admin_username" {
83+
description = "Administrator user name"
84+
default = "adminuser"
85+
}
86+
87+
variable "admin_password" {
88+
description = "Administrator password"
89+
default = "Adminpassword123!"
90+
}

0 commit comments

Comments
 (0)