This example shows how to automate:
- Creation of two instances sequentially with Terraform on OpenStack cluster.
- Provisioning new instance via Ansible
Terraform does not have Ansible provisioner so we'll use local-exec provisioner for Ansible.
Create file /root/myservers.tf
# Configure the OpenStack Provider
provider "openstack" {
tenant_name = "MyProject"
user_name = "admin"
password = "AdminPassword"
auth_url = "http://controller:5000/v2.0"
}
# Create a server-A
resource "openstack_compute_instance_v2" "server-A" {
region = "MyRegion"
name = "server-A"
image_id = "fce7b6f8-d395-4f93-9aac-1db55e14d766"
flavor_id = "d0d03e8e-4bd0-4afa-9fb1-fdf370d42375"
key_pair = "MyRSA"
security_groups = ["default"]
network {
name = "MyProvider"
}
provisioner "local-exec" {
command = "sleep 30 && echo -e \"[server]\n${openstack_compute_instance_v2.server-A.network.0.fixed_ip_v4} ansible_ssh_private_key_file=/root/MyRSA.pem\" > inventory-terra && ansible-playbook -i inventory-terra /root/ansible/server.yml"
}
}
# Create a server-B
resource "openstack_compute_instance_v2" "server-B" {
depends_on = ["openstack_compute_instance_v2.server-A"]
region = "MyRegion"
name = "server-B"
image_id = "fce7b6f8-d395-4f93-9aac-1db55e14d766"
flavor_id = "d0d03e8e-4bd0-4afa-9fb1-fdf370d42375"
key_pair = "MyRSA"
security_groups = ["default"]
network {
name = "MyProvider"
}
provisioner "local-exec" {
command = "sleep 30 && echo -e \"[server]\n${openstack_compute_instance_v2.server-B.network.0.fixed_ip_v4} ansible_ssh_private_key_file=/root/MyRSA.pem\" > inventory-terra && ansible-playbook -i inventory-terra /opt/it/ansible/server.yml"
}
}
Ready to use:
terraform plan
terraform apply