Skip to content

Commit 4658002

Browse files
committed
add management dependencies terraform
1 parent 0f533c7 commit 4658002

File tree

2 files changed

+47
-263
lines changed
  • tutorials
    • how-to-setup-applesilicon-server-with-ansible
    • how-to-setup-applesilicon-server-with-terraform

2 files changed

+47
-263
lines changed

tutorials/how-to-setup-applesilicon-server-with-ansible/index.mdx

Lines changed: 0 additions & 229 deletions
This file was deleted.

tutorials/how-to-setup-applesilicon-server-with-terraform/index.mdx

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ terraform apply
115115

116116
This will apply the new settings, ensuring that the server is launched within the specified VPC and connected to the Private Network.
117117

118-
## How to read server info using Terraform
118+
## Retrieve Server Information
119119

120-
To read server information after creation, you can use the terraform output command, assuming you have defined output variables in your `resources.tf`. For example:
120+
You can retrieve server information after the creation by using the terraform output command. To do so, you need to define output variables in your resources.tf. For example:
121121

122122
```terraform
123123
#resources.tf
@@ -132,60 +132,73 @@ After applying the configuration, run:
132132
terraform output server_ip
133133
```
134134

135-
## Delve into Terraform with local-exec, remote-exec and null_resource
135+
## Delving into Terraform Provisioners: local-exec, remote-exec, and null_resource
136136

137-
In Terraform, provisioners like local-exec, remote-exec, and null_resource can be used to automate the execution of scripts and manage resource configuration. Here’s how you can use them:
137+
Provisioners in Terraform help automate the execution of tasks. You can use local-exec, remote-exec, and null_resource to trigger actions in your environment. Here’s an overview of how they work:
138138

139-
[null_resource](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) is a special Terraform resource that doesn’t manage infrastructure directly. It serves as a placeholder for running provisioners and triggers actions without creating any infrastructure.
140-
[remote-exec](https://developer.hashicorp.com/terraform/language/resources/provisioners/remote-exec) provisioner allows you to execute scripts on remote resources after they’ve been created. It is useful for configuring remote servers or performing post-creation actions.
141-
[local-exec](https://developer.hashicorp.com/terraform/language/resources/provisioners/local-exec) provisioner enables you to run scripts on your local machine. This is helpful when you need to execute commands in your local environment based on the resources created by Terraform.
139+
[null_resource](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) This placeholder allows you to run provisioners without managing infrastructure directly. It triggers actions based on dependencies but doesn’t create or modify resources.
140+
[remote-exec](https://developer.hashicorp.com/terraform/language/resources/provisioners/remote-exec) This provisioner executes scripts on remote servers, making it ideal for configuring your infrastructure or performing post-creation tasks.
141+
[local-exec](https://developer.hashicorp.com/terraform/language/resources/provisioners/local-exec) This provisioner allows you to execute commands locally, on your local machine, after creating resources in Terraform.
142142

143-
1. We want to connect to our server, so we are going to retrieve all the connection information
143+
### Storing the SSH Key Locally
144+
145+
You can use the local-exec provisioner to store the SSH key of the server on your local machine, preventing future verification prompts:
144146

145147
``` terraform
146148
#resource.tf
147149
148-
resource "null_resource" "store_ssh_key" {
150+
resource "null_resource" "store_server_ssh_key" {
151+
depends_on = [scaleway_apple_silicon_server.server]
152+
149153
provisioner "local-exec" {
150154
command = <<-EOT
151-
mkdir -p ~/.ssh
152-
echo "${file("~/.ssh/id_rsa")}" > ~/.ssh/id_rsa
153-
chmod 600 ~/.ssh/id_rsa
154-
echo 'SSH key stored at ~/.ssh/id_rsa'
155+
ssh-keyscan -H ${scaleway_apple_silicon_server.server.ip} >> ~/.ssh/known_hosts
156+
echo "Stored SSH public key for ${scaleway_apple_silicon_server.server.ip}"
155157
EOT
156158
}
157159
}
158160
```
161+
### Installing Homebrew and Dependencies
159162

160-
2. To install dependancies which can be usefull
161-
162-
163-
164-
## Conclusion
165-
166-
In this tutorial, we have explored how to automate the creation and management of Apple silicon servers on Scaleway using two powerful tools: Terraform and Ansible. While both tools offer significant automation benefits, they each operate in distinct ways, which makes them suitable for different use cases.
167-
168-
### Keys differences
163+
Next, we can use remote-exec to install Homebrew and other essential dependencies on the server:
169164

170-
#### Terraform's state management
171-
172-
Terraform maintains a state file that tracks the current status of your infrastructure. This state file acts as the source of truth for the resources created and managed by Terraform, providing you with visibility and control over any changes. This allows Terraform to efficiently handle infrastructure updates and deletions while ensuring consistency across environments.
173-
174-
#### Ansible’s idempotency
165+
```terraform
166+
resource "null_resource" "install_homebrew_and_dependencies" {
167+
depends_on = [scaleway_apple_silicon_server.server]
168+
169+
provisioner "remote-exec" {
170+
inline = [
171+
"echo 'Installing Homebrew on the server...'",
172+
"which brew || /bin/bash -c '$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)'",
173+
"echo 'Adding Homebrew to the PATH...'",
174+
"echo 'export PATH=/opt/homebrew/bin:$PATH' >> ~/.zshrc",
175+
"source ~/.zshrc",
176+
"echo 'Installing essential dependencies...'",
177+
"brew install git wget curl",
178+
"echo 'Homebrew and dependencies installed.'",
179+
]
180+
181+
connection {
182+
type = "ssh"
183+
user = scaleway_apple_silicon_server.server.username
184+
host = scaleway_apple_silicon_server.server.ip
185+
password = scaleway_apple_silicon_server.server.password
186+
timeout = "5m"
187+
}
188+
}
189+
}
175190
176-
Ansible, on the other hand, does not have a built-in state management system. Instead, it focuses on idempotency, meaning that Ansible playbooks can be executed multiple times without causing unwanted side effects. Each run checks whether the desired state is already achieved, and if not, it makes the necessary changes. However, Ansible does not retain a record of resource states, so it might be less efficient for large-scale infrastructure management compared to Terraform.
191+
```
177192

178-
### Which to choose?
179193

180-
Terraform is an excellent choice for infrastructure provisioning where you need to keep track of your resources and manage dependencies between them. It is especially useful for handling lifecycle management (creation, modification, and deletion) of your infrastructure.
181194

182-
Ansible shines in configuration management, automation, and orchestrating tasks across your infrastructure. It’s ideal for deploying applications, configuring services, and ensuring your systems are in the desired state after provisioning.
195+
## Conclusion
183196

184-
You can also combine both tools for optimal results! Use Terraform for provisioning the infrastructure and Ansible for configuring the servers and deploying applications on top of it. This hybrid approach allows you to leverage the best of both worlds.
197+
In this tutorial, we have explored how to automate the creation and management of Apple Silicon servers on Scaleway using Terraform. By leveraging Terraform’s infrastructure as code (IaC) capabilities, we streamlined server creation, network configuration, and the installation of essential dependencies. However, it’s important to note that while Terraform excels at managing infrastructure and automating deployments, it has limitations when it comes to handling more complex dependencies and configurations that may evolve over time.
185198

186-
By following this guide, you now have a solid understanding of how to automate the creation and management of Apple silicon servers on Scaleway, whether you're using Terraform or Ansible. Both tools can greatly streamline your workflow, reduce manual effort, and help you maintain consistency across your infrastructure.
199+
For more intricate use cases, especially when managing complex configurations or handling dependencies between various resources, Ansible is a better fit. Ansible offers a more flexible, agentless approach to configuration management, where it excels in defining and automating tasks like installing software, configuring system settings, and managing service dependencies. It is ideal for handling the post-provisioning setup or when orchestrating multiple servers across your infrastructure.
187200

188-
For more advanced topics, consider exploring Terraform’s state management and Ansible’s playbook design to enhance your automation skills further. Happy automating!
201+
In an upcoming tutorial, we will dive deeper into how Ansible can be integrated into your workflow for managing dependencies and handling more advanced server configurations, enhancing the automation process beyond simple infrastructure provisioning.
189202

190203

191204

0 commit comments

Comments
 (0)