Skip to content

Commit 83bf2d8

Browse files
committed
Updated readme, added both local and remote exec methods to code.
1 parent 3a0019c commit 83bf2d8

File tree

4 files changed

+90
-14
lines changed

4 files changed

+90
-14
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
This repository contains code examples for running Terraform and Ansible together in different configurations.
33

44
## Run Ansible from Terraform
5+
Use the code examples in the `terraform_gcp` or `terraform_azure` folders to see how this is done. Basically there are two steps. First is a remote exec which forces Terraform to wait until SSH is running to run Ansible. This can be anything, even an `echo "Hello World"` command.
56

67
## Run Terraform from Ansible
8+
The code example in the `terraform_gcp` directory has code for remote exec commented out. You can comment out the local_exec code and run this to have Ansible run on the remote host. With this method we first copy our playbook to the remote host, then we install and run Ansible locally there.
79

810
## Build System Images with Packer and Ansible
11+
HashiCorp's Packer tool allows you to use your existing Ansible playbooks to easily build machine images on the cloud or virtualization platform of your choice. Packer uses a JSON file for configuration, and is run from the command line.
912

1013
## Integrate HashiCorp Vault with Ansible

ansible/httpd.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
become: yes
44
become_user: root
55
tasks:
6+
- name: Install firewalld
7+
yum: name=firewalld
68
- name: Stop firewalld
79
service: name=firewalld state=stopped
810
- name: Install the httpd package

packer/packer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"builders": [{
3+
"type": "amazon-ebs",
4+
"ssh_pty": true,
5+
"access_key": "{{user `aws_access_key`}}",
6+
"secret_key": "{{user `aws_secret_key`}}",
7+
"region": "us-west-2",
8+
"instance_type": "t2.medium",
9+
"source_ami": "ami-28e07e50",
10+
"ssh_username": "ec2-user",
11+
"ami_name": "Ansible with Packer Demo - v0.1"
12+
}],
13+
14+
"provisioners": [{
15+
"type": "ansible",
16+
"playbook_file": "../ansible/httpd.yml"
17+
}]
18+
}

terraform_gcp/main.tf

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,73 @@ resource "google_compute_instance" "tfansible" {
3939
scopes = ["userinfo-email", "compute-ro", "storage-ro"]
4040
}
4141

42-
provisioner "remote-exec" {
43-
inline = ["echo 'Hello World'"]
44-
45-
connection {
46-
type = "ssh"
47-
host = "${google_compute_instance.tfansible.network_interface.0.access_config.0.assigned_nat_ip}"
48-
user = "${var.ssh_user}"
49-
private_key = "${file("${var.private_key_path}")}"
50-
}
51-
}
52-
53-
provisioner "local-exec" {
54-
command = "ansible-playbook -i '${google_compute_instance.tfansible.network_interface.0.access_config.0.assigned_nat_ip},' --private-key ${var.private_key_path} ../ansible/httpd.yml"
55-
}
42+
##############################################################################
43+
# This is the 'local exec' method.
44+
# Ansible runs from the same host you run Terraform from
45+
##############################################################################
46+
# provisioner "remote-exec" {
47+
# inline = ["echo 'Hello World'"]
48+
49+
# connection {
50+
# type = "ssh"
51+
# user = "${var.ssh_user}"
52+
# private_key = "${file("${var.private_key_path}")}"
53+
# }
54+
# }
55+
56+
# provisioner "local-exec" {
57+
# command = "ansible-playbook -i '${google_compute_instance.tfansible.network_interface.0.access_config.0.assigned_nat_ip},' --private-key ${var.private_key_path} ../ansible/httpd.yml"
58+
# }
59+
60+
##############################################################################
61+
# This is the 'remote exec' method.
62+
# Ansible runs on the target host.
63+
##############################################################################
64+
# provisioner "remote-exec" {
65+
# inline = [
66+
# "mkdir /home/${var.ssh_user}/files",
67+
# "mkdir /home/${var.ssh_user}/ansible",
68+
# ]
69+
70+
# connection {
71+
# type = "ssh"
72+
# user = "${var.ssh_user}"
73+
# private_key = "${file("${var.private_key_path}")}"
74+
# }
75+
# }
76+
# provisioner "file" {
77+
# source = "../ansible/httpd.yml"
78+
# destination = "/home/${var.ssh_user}/ansible/httpd.yml"
79+
80+
# connection {
81+
# type = "ssh"
82+
# user = "${var.ssh_user}"
83+
# private_key = "${file("${var.private_key_path}")}"
84+
# }
85+
# }
86+
# provisioner "file" {
87+
# source = "../files/webapp.sh"
88+
# destination = "/home/${var.ssh_user}/files/webapp.sh"
89+
90+
# connection {
91+
# type = "ssh"
92+
# user = "${var.ssh_user}"
93+
# private_key = "${file("${var.private_key_path}")}"
94+
# }
95+
# }
96+
# provisioner "remote-exec" {
97+
# inline = [
98+
# "sudo yum -y install ansible",
99+
# "cd ansible; ansible-playbook -c local -i \"localhost,\" httpd.yml",
100+
# ]
101+
102+
# connection {
103+
# type = "ssh"
104+
# user = "${var.ssh_user}"
105+
# private_key = "${file("${var.private_key_path}")}"
106+
# }
107+
# }
108+
# Don't comment out this next line.
56109
}
57110

58111
resource "google_compute_firewall" "default" {

0 commit comments

Comments
 (0)