Skip to content

Commit a2b2dde

Browse files
authored
[ZT] Update Ansible Tunnel guide (#21736)
* update terraform code * update ansible example * update tunnel names
1 parent c9de6f8 commit a2b2dde

File tree

3 files changed

+110
-119
lines changed

3 files changed

+110
-119
lines changed

src/content/docs/cloudflare-one/connections/connect-networks/deployment-guides/ansible.mdx

Lines changed: 77 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Ansible works alongside Terraform to streamline the Cloudflare Tunnel setup proc
1616
To complete the steps in this guide, you will need:
1717

1818
- [A Google Cloud Project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#creating_a_project) and [GCP CLI installed and authenticated](https://cloud.google.com/sdk/docs/install).
19-
- [Basic knowledge of Terraform](/cloudflare-one/connections/connect-networks/deployment-guides/terraform/) and[Terraform installed](https://developer.hashicorp.com/terraform/tutorials/certification-associate-tutorials/install-cli).
19+
- [Basic knowledge of Terraform](/cloudflare-one/connections/connect-networks/deployment-guides/terraform/) and [Terraform installed](https://developer.hashicorp.com/terraform/tutorials/certification-associate-tutorials/install-cli).
2020
- [A zone on Cloudflare](/fundamentals/setup/manage-domains/add-site/).
2121
- [A Cloudflare API token](/fundamentals/api/get-started/create-token/) with `Cloudflare Tunnel` and `DNS` permissions.
2222

@@ -74,32 +74,7 @@ You will need to declare the [providers](https://registry.terraform.io/browse/pr
7474

7575
2. Add the following providers to `providers.tf`. The `random` provider is used to generate a tunnel secret.
7676

77-
```txt
78-
terraform {
79-
required_providers {
80-
cloudflare = {
81-
source = "cloudflare/cloudflare"
82-
}
83-
google = {
84-
source = "hashicorp/google"
85-
}
86-
random = {
87-
source = "hashicorp/random"
88-
}
89-
}
90-
required_version = ">= 0.13"
91-
}
92-
93-
# Providers
94-
provider "cloudflare" {
95-
api_token = var.cloudflare_token
96-
}
97-
provider "google" {
98-
project = var.gcp_project_id
99-
}
100-
provider "random" {
101-
}
102-
```
77+
<Render file="terraform/providers-v5" />
10378

10479
### Configure Cloudflare resources
10580

@@ -113,30 +88,53 @@ The following configuration will modify settings in your Cloudflare account.
11388

11489
2. Add the following resources to `Cloudflare-config.tf`:
11590

116-
```txt
117-
# Generates a 64-character secret for the tunnel.
118-
# Using `random_password` means the result is treated as sensitive and, thus,
119-
# not displayed in console output. Refer to: https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/password
120-
resource "random_password" "tunnel_secret" {
121-
length = 64
122-
}
123-
124-
# Creates a new locally-managed tunnel for the GCP VM.
125-
resource "cloudflare_tunnel" "auto_tunnel" {
126-
account_id = var.cloudflare_account_id
127-
name = "Ansible GCP tunnel"
128-
secret = base64sha256(random_password.tunnel_secret.result)
129-
}
13091

131-
# Creates the CNAME record that routes ssh_app.${var.cloudflare_zone} to the tunnel.
132-
resource "cloudflare_record" "ssh_app" {
133-
zone_id = var.cloudflare_zone_id
134-
name = "ssh_app"
135-
value = "${cloudflare_argo_tunnel.auto_tunnel.id}.cfargotunnel.com"
136-
type = "CNAME"
137-
proxied = true
138-
}
139-
```
92+
```tf
93+
# Generates a 32-byte secret for the tunnel.
94+
resource "random_bytes" "tunnel_secret" {
95+
byte_length = 32
96+
}
97+
98+
# Creates a new remotely-managed tunnel for the GCP VM.
99+
resource "cloudflare_zero_trust_tunnel_cloudflared" "gcp_tunnel" {
100+
account_id = var.cloudflare_account_id
101+
name = "Ansible GCP tunnel"
102+
tunnel_secret = random_bytes.tunnel_secret.base64
103+
}
104+
105+
# Reads the token used to run the tunnel on the server.
106+
data "cloudflare_zero_trust_tunnel_cloudflared_token" "gcp_tunnel_token" {
107+
account_id = var.cloudflare_account_id
108+
tunnel_id = cloudflare_zero_trust_tunnel_cloudflared.gcp_tunnel.id
109+
}
110+
111+
# Creates the CNAME record that routes http_app.${var.cloudflare_zone} to the tunnel.
112+
resource "cloudflare_dns_record" "http_app" {
113+
zone_id = var.cloudflare_zone_id
114+
name = "http_app"
115+
content = "${cloudflare_zero_trust_tunnel_cloudflared.gcp_tunnel.id}.cfargotunnel.com"
116+
type = "CNAME"
117+
ttl = 1
118+
proxied = true
119+
}
120+
121+
# Configures tunnel with a public hostname route for clientless access.
122+
resource "cloudflare_zero_trust_tunnel_cloudflared_config" "gcp_tunnel_config" {
123+
tunnel_id = cloudflare_zero_trust_tunnel_cloudflared.gcp_tunnel.id
124+
account_id = var.cloudflare_account_id
125+
config = {
126+
ingress = [
127+
{
128+
hostname = "http_app.${var.cloudflare_zone}"
129+
service = "http://localhost:80"
130+
},
131+
{
132+
service = "http_status:404"
133+
}
134+
]
135+
}
136+
}
137+
```
140138

141139
### Configure GCP resources
142140

@@ -158,7 +156,7 @@ The following configuration defines the specifications for the GCP virtual machi
158156
}
159157
160158
# Sets up a GCP VM instance.
161-
resource "google_compute_instance" "origin" {
159+
resource "google_compute_instance" "http_server" {
162160
name = "ansible-inst"
163161
machine_type = var.machine_type
164162
zone = var.zone
@@ -209,7 +207,7 @@ The following configuration defines the specifications for the GCP virtual machi
209207

210208
### Export variables to Ansible
211209

212-
The following Terraform resource exports the tunnel ID and other variables to `tf_ansible_vars_file.yml`. Ansible will use this data to configure and run `cloudflared` on the server.
210+
The following Terraform resource exports the [tunnel token](/cloudflare-one/connections/connect-networks/configure-tunnels/remote-tunnel-permissions/) and other variables to `tf_ansible_vars_file.yml`. Ansible will use the tunnel token to configure and run `cloudflared` on the server.
213211

214212
1. In your configuration directory, create a new `tf` file:
215213

@@ -219,20 +217,18 @@ The following Terraform resource exports the tunnel ID and other variables to `t
219217

220218
2. Copy and paste the following content into `export.tf`:
221219

222-
```txt
223-
resource "local_file" "tf_ansible_vars_file" {
224-
content = <<-DOC
225-
# Ansible vars_file containing variable values from Terraform.
226-
tunnel_id: ${cloudflare_argo_tunnel.auto_tunnel.id}
227-
account: ${var.cloudflare_account_id}
228-
tunnel_name: ${cloudflare_argo_tunnel.auto_tunnel.name}
229-
secret: ${random_id.tunnel_secret.b64_std}
230-
zone: ${var.cloudflare_zone}
231-
DOC
232-
233-
filename = "./tf_ansible_vars_file.yml"
234-
}
235-
```
220+
```tf
221+
resource "local_file" "tf_ansible_vars_file" {
222+
content = <<-DOC
223+
# Ansible vars_file containing variable values from Terraform.
224+
tunnel_id: ${cloudflare_zero_trust_tunnel_cloudflared.gcp_tunnel.id}
225+
tunnel_name: ${cloudflare_zero_trust_tunnel_cloudflared.gcp_tunnel.name}
226+
tunnel_token: ${data.cloudflare_zero_trust_tunnel_cloudflared_token.gcp_tunnel_token.token}
227+
DOC
228+
229+
filename = "./tf_ansible_vars_file.yml"
230+
}
231+
```
236232

237233
## 5. Create the Ansible playbook
238234

@@ -259,38 +255,27 @@ Ansible playbooks are YAML files that declare the configuration Ansible will dep
259255
shell: wget https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb
260256
- name: Depackage cloudflared.
261257
shell: sudo dpkg -i cloudflared-linux-amd64.deb
262-
- name: Create a cloudflared service directory.
263-
shell: mkdir -p /etc/cloudflared/
264-
- name: Create the config file for cloudflared and define the ingress rules for the tunnel.
265-
copy:
266-
dest: "/etc/cloudflared/config.yml"
267-
content: |
268-
tunnel: "{{ tunnel_id }}"
269-
credentials-file: /etc/cloudflared/cert.json
270-
logfile: /var/log/cloudflared.log
271-
loglevel: info
272-
ingress:
273-
- hostname: "ssh_app.{{ zone }}"
274-
service: ssh://localhost:22
275-
- service: http_status:404
276-
- name: Create the tunnel credentials file for cloudflared.
277-
copy:
278-
dest: "/etc/cloudflared/cert.json"
279-
content: |
280-
{
281-
"AccountTag" : "{{ account | quote }}",
282-
"TunnelID" : "{{ tunnel_id | quote }}",
283-
"TunnelName" : "{{ tunnel_name | quote }}",
284-
"TunnelSecret" : "{{ secret | quote }}"
285-
}
286258
- name: Install the tunnel as a systemd service.
287-
shell: cloudflared service install
259+
shell: "cloudflared service install {{ tunnel_token }}"
288260
- name: Start the tunnel.
289261
systemd:
290262
name: cloudflared
291263
state: started
292264
enabled: true
293265
masked: no
266+
- name: Deploy an example Apache web server on port 80.
267+
shell: apt update && apt -y install apache2
268+
- name: Edit the default Apache index file.
269+
copy:
270+
dest: /var/www/html/index.html
271+
content: |
272+
<!DOCTYPE html>
273+
<html>
274+
<body>
275+
<h1>Hello Cloudflare!</h1>
276+
<p>This page was created for a Cloudflare demo.</p>
277+
</body>
278+
</html>
294279
```
295280
296281
[Keywords](https://docs.ansible.com/ansible/latest/reference_appendices/playbooks_keywords.html#play) define how Ansible will execute the configuration. In the example above, the `vars_files` keyword specifies where variable definitions are stored, and the `tasks` keyword specifies the actions Ansible will perform.
@@ -323,4 +308,4 @@ It may take several minutes for the GCP instance and tunnel to come online. You
323308

324309
## 7. Test the connection
325310

326-
You can now SSH to the GCP server through the new `ssh_app.<zone>` hostname. For instructions on how to connect, refer to our [SSH guide](/cloudflare-one/connections/connect-networks/use-cases/ssh/).
311+
To test, open a browser and go to `http://http_app.<CLOUDFLARE_ZONE>.com` (for example, `http_app.example.com`). You should see the **Hello Cloudflare!** test page.

src/content/docs/cloudflare-one/connections/connect-networks/deployment-guides/terraform.mdx

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -81,33 +81,7 @@ You will need to declare the [providers](https://registry.terraform.io/browse/pr
8181
<Tabs syncKey="dashPlusAPI">
8282
<TabItem label="Terraform (v5)">
8383

84-
```tf
85-
terraform {
86-
required_providers {
87-
cloudflare = {
88-
source = "cloudflare/cloudflare"
89-
version = ">= 5.3.0"
90-
}
91-
google = {
92-
source = "hashicorp/google"
93-
}
94-
random = {
95-
source = "hashicorp/random"
96-
}
97-
}
98-
required_version = ">= 1.2"
99-
}
100-
101-
# Providers
102-
provider "cloudflare" {
103-
api_token = var.cloudflare_token
104-
}
105-
provider "google" {
106-
project = var.gcp_project_id
107-
}
108-
provider "random" {
109-
}
110-
```
84+
<Render file="terraform/providers-v5" />
11185

11286
</TabItem>
11387
<TabItem label="Terraform (v4)">
@@ -245,6 +219,7 @@ The following configuration will modify settings in your Cloudflare account.
245219
]
246220
}
247221
```
222+
248223
</TabItem>
249224
<TabItem label="Terraform (v4)">
250225

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
{}
3+
---
4+
5+
```tf
6+
terraform {
7+
required_providers {
8+
cloudflare = {
9+
source = "cloudflare/cloudflare"
10+
version = ">= 5.3.0"
11+
}
12+
google = {
13+
source = "hashicorp/google"
14+
}
15+
random = {
16+
source = "hashicorp/random"
17+
}
18+
}
19+
required_version = ">= 1.2"
20+
}
21+
22+
# Providers
23+
provider "cloudflare" {
24+
api_token = var.cloudflare_token
25+
}
26+
provider "google" {
27+
project = var.gcp_project_id
28+
}
29+
provider "random" {
30+
}
31+
```

0 commit comments

Comments
 (0)