Skip to content

Commit 817499a

Browse files
committed
add invoking terraform from ansible sample
1 parent 9e1d585 commit 817499a

File tree

9 files changed

+154
-0
lines changed

9 files changed

+154
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Involiking terraform from ansible
2+
This is most basic sample about invoking terraform from ansible. This will create following resources. Architecture overview is [here](https://docs.google.com/presentation/d/1pqtbiJRGc3uUm8ulhMBf4SWm2WPCCrhgUInjm9DMYdc/edit#slide=id.g5512275ccb_2_0).
3+
1. Create VPC
4+
1. Create Vswitch
5+
1. Create Security Group and set some rules
6+
1. Create 3 ECS instances in Vswitch
7+
1. Provision httpd to these with ansible
8+
9+
## How to use
10+
First you need to chnage configuration to yours and install ansible.
11+
```
12+
$ brew install ansible
13+
$ cd terraform
14+
$ cp terraform.tfvars.sample terrafrom.tfvars
15+
$ vim terraform.tfvars
16+
=> Edit variables with your favorite editor.
17+
```
18+
19+
Deploy to Alibaba Cloud
20+
```
21+
$ ansible-playbook -i ./inventry.sh -u root
22+
Apply complete! Resources: 7 added, 0 changed, 0 destroyed.
23+
24+
Outputs:
25+
26+
eip = xx.xx.xx.xx
27+
```
28+
29+
## Reference
30+
- [terraform – Manages a Terraform deployment (and plans)](https://docs.ansible.com/ansible/latest/modules/terraform_module.html)
31+
- [HASHICORP TERRAFORM AND RED HAT ANSIBLE AUTOMATION](https://www.redhat.com/cms/managed-files/pa-terraform-and-ansible-overview-f14774wg-201811-en.pdf)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[defaults]
2+
host_key_checking = False
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
if [ -e terraform/terraform.tfstate ]; then
3+
ip=`cat terraform/terraform.tfstate | jq '.modules[].outputs[].value' | cut -d '"' -f 2`
4+
cat << EOS
5+
{
6+
"cloud_servers" : [ $ip ]
7+
}
8+
EOS
9+
fi
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
- name: hoge
3+
hosts: cloud_servers
4+
tasks:
5+
- name: be sure httpd is installed
6+
yum: name=httpd state=installed
7+
8+
- name: be sure httpd is running and enabled
9+
service: name=httpd state=started enabled=yes
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
- hosts: 127.0.0.1
3+
connection: local
4+
tasks:
5+
- name: Exec terraform scripts
6+
terraform:
7+
project_path: 'terraform/'
8+
state: present
9+
10+
- name: Refresh inventory because of creating cloud servers
11+
meta: refresh_inventory
12+
13+
- name: Wait for port 22 to open
14+
wait_for:
15+
port: 22
16+
host: "{{ item }}"
17+
delay: 10
18+
timeout: 30
19+
with_items: "{{ groups['cloud_servers'] }}"

invoking_from_ansible_sample/site.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
- import_playbook: setup_cloud.yml
3+
- import_playbook: provisioning_config.yml
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "publiv_ip" {
2+
value = "${join(",",alicloud_instance.web.*.public_ip)}"
3+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
variable "access_key" {}
2+
variable "secret_key" {}
3+
variable "region" {}
4+
variable "zone" {}
5+
variable "publickey" {}
6+
variable "ssh_cidr" {}
7+
8+
provider "alicloud" {
9+
access_key = "${var.access_key}"
10+
secret_key = "${var.secret_key}"
11+
region = "${var.region}"
12+
}
13+
14+
resource "alicloud_security_group" "sg" {
15+
name = "terraform-sg"
16+
vpc_id = "${alicloud_vpc.vpc.id}"
17+
}
18+
19+
resource "alicloud_security_group_rule" "allow_http" {
20+
type = "ingress"
21+
ip_protocol = "tcp"
22+
nic_type = "intranet"
23+
policy = "accept"
24+
port_range = "80/80"
25+
priority = 1
26+
security_group_id = "${alicloud_security_group.sg.id}"
27+
cidr_ip = "0.0.0.0/0"
28+
}
29+
30+
resource "alicloud_security_group_rule" "allow_ssh" {
31+
type = "ingress"
32+
ip_protocol = "tcp"
33+
nic_type = "intranet"
34+
policy = "accept"
35+
port_range = "22/22"
36+
priority = 1
37+
security_group_id = "${alicloud_security_group.sg.id}"
38+
cidr_ip = "${var.ssh_cidr}"
39+
}
40+
41+
resource "alicloud_vpc" "vpc" {
42+
name = "terraform-vpc"
43+
cidr_block = "192.168.1.0/24"
44+
}
45+
46+
resource "alicloud_vswitch" "vsw" {
47+
vpc_id = "${alicloud_vpc.vpc.id}"
48+
cidr_block = "192.168.1.0/28"
49+
availability_zone = "${var.zone}"
50+
}
51+
52+
resource "alicloud_instance" "web" {
53+
count = 3
54+
instance_name = "terraform-ecs"
55+
availability_zone = "${var.zone}"
56+
image_id = "centos_7_3_64_40G_base_20170322.vhd"
57+
instance_type = "ecs.n4.small"
58+
system_disk_category = "cloud_efficiency"
59+
security_groups = ["${alicloud_security_group.sg.id}"]
60+
vswitch_id = "${alicloud_vswitch.vsw.id}"
61+
internet_max_bandwidth_out = 10
62+
}
63+
64+
resource "alicloud_key_pair" "key" {
65+
key_name = "my_public_key"
66+
public_key = "${var.publickey}"
67+
}
68+
69+
resource "alicloud_key_pair_attachment" "attach" {
70+
key_name = "${alicloud_key_pair.key.id}"
71+
instance_ids = ["${alicloud_instance.web.*.id}"]
72+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
access_key = "xxxxxxxxxxxxxxxxxx"
2+
secret_key = "xxxxxxxxxxxxxxxxxx"
3+
region = "ap-northeast-1"
4+
zone = "ap-northeast-1a"
5+
ssh_cidr = "xx.xx.xx.xx/xx"
6+
publickey = "xxxxxxxxxxx"

0 commit comments

Comments
 (0)