Skip to content

Commit

Permalink
ansible-1-dynamic-inv
Browse files Browse the repository at this point in the history
  • Loading branch information
117thDragon committed Mar 18, 2024
1 parent 277acd6 commit 78cd64e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
16 changes: 16 additions & 0 deletions ansible/inventory.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"app": {
"hosts": {
"appserver": {
"ansible_host": 178.154.201.209
}
}
},
"db": {
"hosts": {
"dbserver": {
"ansible_host": 158.160.123.163
}
}
}
}
49 changes: 49 additions & 0 deletions ansible/script_inv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/python3

import yandex_cloud_client as yc
import json
from sys import argv

options = ['--list', '--host']

folder_id = 'b1g91p2spmjgqtkfjh67'

with open('/home/akuravskij/otus/secrets/key.json', 'r') as infile:
credentials = json.load(infile)

client = yc.ComputeClient(service_account_key=credentials)

clients = client.instances_in_folder(folder_id)


app_name = inst[0].name
db_name = inst[1].name


# получаем IP хостов
app_ip = inst[0].network_interfaces[0].primary_v4_address.one_to_one_nat.address
db_ip = inst[1].network_interfaces[0].primary_v4_address.one_to_one_nat.address


data = \
{
"app": {
"hosts": {
"appserver": {
"ansible_host": app_ip
}
}
},
"db": {
"hosts": {
"dbserver": {
"ansible_host": db_ip
}
}
}
}

a = json.dumps(data)
f = open('inventory.json', 'w')
f.write(json.dumps(data, indent=4))
f.close()

0 comments on commit 78cd64e

Please sign in to comment.