Commit dac9e2d1 authored by nanahira's avatar nanahira

write vars in another file

parent a3d84184
---
- hosts: wg
vars:
services: []
tasks:
- name: load vars
include_vars:
file: '../result/{{item}}.yaml'
with_items:
- global-vars
- vars-{{inventory_hostname_short}}
- name: directory
file:
path: '{{ansible_user_dir}}/nextgen-network/{{item}}'
......
......@@ -4,7 +4,8 @@
"description": "",
"scripts": {
"build": "tsc",
"start": "node build/inventory.js"
"start": "node build/inventory.js",
"all": "npm run build && npm start"
},
"dependencies": {
"@types/ip": "^1.1.0",
......
......@@ -96,22 +96,16 @@ class InventoryBuilder {
host.wgPublickey = await this.wgPublickey(host.wgPrivateKey);
}
this.vars = await this.loadUtilities();
const inventoryValue = { hosts: Object.fromEntries(Object.values(this.hosts).map(host => [host.name, this.getHostConnectionInfo(host)])) };
await fs.promises.writeFile('result/inventory.yaml', YAML.stringify(inventoryValue));
// console.log(Object.values(this.hosts));
const rawHosts = await Promise.all(Object.values(this.hosts).map(async (h) => [h.name, await this.host_vars(h)]));
const hosts = Object.fromEntries(rawHosts);
// console.log(hosts);
/* const rawHostsForSwitch = rawHosts.map(rh => {
const hostVars = JSON.parse(JSON.stringify(rh[1]));
hostVars.ansible_ssh_host = hostVars.address;
hostVars.ansible_ssh_port = this.hosts[rh[0]].sshSystem || 22;
return [`switch-${rh[0]}`, hostVars];
});
const switchHosts = Object.fromEntries(rawHostsForSwitch); */
const result = YAML.stringify({
wg: { hosts, vars: this.vars },
//switch: { hosts: switchHosts, vars: JSON.parse(JSON.stringify(this.vars)) }
});
return fs.promises.writeFile('result/inventory.yaml', result);
const hostsArray = await Promise.all(Object.values(this.hosts).map(async (h) => [h.name, await this.host_vars(h)]));
//const hosts = Object.fromEntries(hostsArray);
//await fs.promises.writeFile('result/inventory.yaml', YAML.stringify({
// wg: { hosts }
//}));
await fs.promises.writeFile('result/global-vars.yaml', YAML.stringify(this.vars));
await Promise.all(hostsArray.map(host => fs.promises.writeFile(`result/vars-${host[0]}.yaml`, YAML.stringify(host[1]))));
}
async loadUtilities() {
......@@ -141,6 +135,15 @@ class InventoryBuilder {
return vars;
}
getHostConnectionInfo(host) {
return {
ansible_ssh_host: host.host,
ansible_ssh_user: host.user,
ansible_ssh_port: host.sshPort || 22,
ansible_python_interpreter: host.python || 'python3',
}
}
async host_vars(host) {
const connections = [];
host.dockerServices = {
......@@ -195,10 +198,10 @@ class InventoryBuilder {
}
return {
ansible_ssh_host: host.host,
ansible_ssh_user: host.user,
ansible_ssh_port: host.sshPort || 22,
ansible_python_interpreter: host.python || 'python3',
//ansible_ssh_host: host.host,
//ansible_ssh_user: host.user,
//ansible_ssh_port: host.sshPort || 22,
//ansible_python_interpreter: host.python || 'python3',
id: host.id,
address: host.address,
isCN: host.location.startsWith('CN'),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment