Using Ansible with Terraform



Terraform , . « AWS» « ». VPC security group SSH, .



, AMI . — Ansible, , , Terraform Ansible ( , Ansible 2.5, . ), Chef Salt. Packer, ansible () ansible-local, Docker.



Terraform Ansible, . — .



?



, , AMI , , , , . . - , . , , , .



— AMI, - , , Ansible. , Packer Ansible.



, , — .



Ansible Terraform



: Ansible Terraform . , .



inventory IP



— Ansible local-exec, :



provisioner "local-exec" {
    command = "ansible-playbook -i '${self.public_ip},' --private-key ${var.ssh_key_private} provision.yml"
}


, . local-exec , , .



remote-exec, , local-exec.



, «Ansible provisioner»:



provisioner "remote-exec" {
    inline = ["sudo dnf -y install python"]

    connection {
      type        = "ssh"
      user        = "fedora"
      private_key = "${file(var.ssh_key_private)}"
    }
  }

  provisioner "local-exec" {
    command = "ansible-playbook -u fedora -i '${self.public_ip},' --private-key ${var.ssh_key_private} provision.yml"
  }


ansible-playbook , Ansible Terraform:



$ ll infra
drwxrwxr-x. 3 avd avd 4.0K Mar  5 15:54 roles/
-rw-rw-r--. 1 avd avd  367 Mar  5 15:19 ansible.cfg
-rw-rw-r--. 1 avd avd 2.5K Mar  7 18:54 main.tf
-rw-rw-r--. 1 avd avd  454 Mar  5 15:27 variables.tf
-rw-rw-r--. 1 avd avd   38 Mar  5 15:54 provision.yml


inventory , , inventory. , Consul, , . , inventory.



, , , .



inventory Terraform



, Terraform — Terraform Ansible. Terraform, Ansible inventory , .



terraform apply, ansible-playbook -i inventory site.yml, inventory — , inventory.



, — , Ansible Terraform.



, .



Inventory, Terraform



, , , — inventory Terraform.



Terraform , , . local backend JSON, inventory Ansible.



, , .





[all]
52.51.215.84

[all:vars]

[server]
52.51.215.84

[server.0]
52.51.215.84

[type_aws_instance]
52.51.215.84

[name_c10k server]
52.51.215.84

[%_1]
52.51.215.84




$ ~/soft/terraform.py --root . --hostfile
## begin hosts generated by terraform.py ##
52.51.215.84        C10K Server
## end hosts generated by terraform.py ##


Ansible Terraform,



, , Ansible Terraform, , , Chef.



, , , — Terraform.



, , :



...
provisioner "ansible" {
    plays {
        playbook = "./provision.yml"
        hosts = ["${self.public_ip}"]
    }
    become = "yes"
    local = "yes"
}
...


, , , .





Terraform Ansible — , . Ansible local-exec, Ansible inventory.



.



!




All Articles