## ansible 与terraform 一起使用 ## 1. windows下不支持ansible 2. 我们会在linux下面使用terraform,并且在linux下面使用terraform+ansible 3. 以centos 7.7 为例 ``` 1. 安装 ansible yum install ansible -y 2. 使用ssh-kegen 生成key ~/.ssh/id_rsa和id_rsa.pub 3. 创建ecs使用上面生成的key, resource "alicloud_key_pair" "alicloud_key_pair" { key_pair_name = "mykey" public_key = "${file(var.ssh_key_public)}" } variable "ssh_key_public" { default = "~/.ssh/id_rsa.pub" description = "Path to the SSH public key for accessing cloud instances. Used for creating AWS keypair." } key_name = alicloud_key_pair.alicloud_key_pair.key_name 4. 使用ansible 来拷贝本地的文件到ecs服务器 resource "local_file" "inventory" { filename = "myinventory" content = "[nginx]\n${alicloud_instance.web.*.public_ip[0]}" } resource "null_resource" "run-ansible" { depends_on = [alicloud_instance.web,local_file.inventory] triggers = { key = "${uuid()}" } provisioner "local-exec" { command = "ansible-playbook -u root -i myinventory --private-key ${var.ssh_key_private} -T 3000 provision.yml" } } --- - hosts: nginx remote_user: root become: yes become_method: sudo vars: tasks: - name: copy file to remote copy: src: ./html/ dest: /usr/share/nginx/html/ - name: restart nginx shell: | systemctl start nginx ```