use local_file for ansible inventory
This commit is contained in:
		
							parent
							
								
									1920af5bc4
								
							
						
					
					
						commit
						75aeaf38a9
					
				
					 5 changed files with 24 additions and 30 deletions
				
			
		
							
								
								
									
										1
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							| 
						 | 
					@ -1 +0,0 @@
 | 
				
			||||||
ansible/id_ed25519
 | 
					 | 
				
			||||||
| 
						 | 
					@ -1,7 +1,7 @@
 | 
				
			||||||
[defaults]
 | 
					[defaults]
 | 
				
			||||||
inventory = inventory.yml
 | 
					inventory = inventory.ini
 | 
				
			||||||
interpreter_python = /usr/bin/python3
 | 
					interpreter_python = /usr/bin/python3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
remote_user = andrei
 | 
					remote_user = ubuntu
 | 
				
			||||||
private_key_file = ~/.ssh/id_ed25519_yatf
 | 
					private_key_file = ~/.ssh/id_ed25519
 | 
				
			||||||
host_key_checking = False
 | 
					host_key_checking = False
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,2 +0,0 @@
 | 
				
			||||||
plugin: cloud.terraform.terraform_provider
 | 
					 | 
				
			||||||
project_path: ../terraform
 | 
					 | 
				
			||||||
							
								
								
									
										10
									
								
								terraform/ansible.tf
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								terraform/ansible.tf
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,10 @@
 | 
				
			||||||
 | 
					resource "local_file" "ansible_inventory" {
 | 
				
			||||||
 | 
					  file_permission = "0644"
 | 
				
			||||||
 | 
					  filename        = "${path.module}/../ansible/inventory.ini"
 | 
				
			||||||
 | 
					  content         = <<EOT
 | 
				
			||||||
 | 
					[all]
 | 
				
			||||||
 | 
					%{for host in yandex_compute_instance.vm~}
 | 
				
			||||||
 | 
					${trimspace("${host.name} ansible_host=${host.network_interface.0.nat_ip_address}")}
 | 
				
			||||||
 | 
					%{endfor~}
 | 
				
			||||||
 | 
					EOT
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -3,9 +3,6 @@ terraform {
 | 
				
			||||||
    yandex = {
 | 
					    yandex = {
 | 
				
			||||||
      source = "yandex-cloud/yandex"
 | 
					      source = "yandex-cloud/yandex"
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    ansible = {
 | 
					 | 
				
			||||||
      source = "ansible/ansible"
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -36,7 +33,7 @@ resource "yandex_compute_instance" "vm" {
 | 
				
			||||||
    memory        = 2
 | 
					    memory        = 2
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  metadata = { user-data = "${file("users.yml")}" }
 | 
					  metadata = { ssh-keys = "ubuntu:${file("~/.ssh/id_ed25519.pub")}" }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
resource "yandex_vpc_network" "network1" {
 | 
					resource "yandex_vpc_network" "network1" {
 | 
				
			||||||
| 
						 | 
					@ -83,13 +80,3 @@ resource "yandex_lb_network_load_balancer" "balancer1" {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
resource "ansible_host" "vm" {
 | 
					 | 
				
			||||||
  count = length(yandex_compute_instance.vm)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  name = "vm${count.index}"
 | 
					 | 
				
			||||||
  groups = ["nginx"]
 | 
					 | 
				
			||||||
  variables = {
 | 
					 | 
				
			||||||
    ansible_host = yandex_compute_instance.vm[count.index].network_interface.0.nat_ip_address
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue