-
Notifications
You must be signed in to change notification settings - Fork 2
/
inv-gen-aws.rb
56 lines (49 loc) · 1.63 KB
/
inv-gen-aws.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require 'aws-sdk'
active_region = 'eu-west-1'
Aws.config.update({ region: active_region })
ec2 = Aws::EC2::Client.new
resp = ec2.describe_instances({ filters: [
{name:"tag:environment",values:['cassandra']}
]})
inventory = []
bastion_ip = '?.?.?.?'
resp.reservations.each do |res|
res.instances.select{ |x| x.state.name == 'running'}.each do |instance|
inventory.push({
'name' => instance.instance_id,
'role' => instance.tags.find{ |x| x.key.downcase == 'role'}.value,
'rack' => instance.placement.availability_zone,
'ip' => (instance.public_ip_address || instance.private_ip_address),
'seed' => instance.tags.find{ |x| x.key.downcase == 'seed'} != nil
})
end
end
puts '[bastion]'
inventory.each do |instance|
if instance['role'] == 'bastion'
puts "#{instance['name']} ansible_host=#{instance['ip']} ansible_user=ec2-user"
bastion_ip = instance['ip']
end
end
puts ''
puts '[seeds]'
inventory.each do |instance|
if instance['seed']
puts "#{instance['name']} ansible_host=#{instance['ip']} data_center=#{active_region} rack=#{instance['rack']}"
end
end
puts ''
puts '[nodes]'
inventory.each do |instance|
if instance['role'] == 'cassandra-node' && !instance['seed']
puts "#{instance['name']} ansible_host=#{instance['ip']} data_center=#{active_region} rack=#{instance['rack']}"
end
end
puts ''
puts '[cassandra:children]'
puts 'seeds'
puts 'nodes'
puts ''
puts '[cassandra:vars]'
puts 'ansible_user=ec2-user'
puts "ansible_ssh_common_args='-o \"ProxyCommand ssh -A ec2-user@#{bastion_ip} -W %h:%p\"'"