Commit dac9e2d1 authored by nanahira's avatar nanahira

write vars in another file

parent a3d84184
--- ---
- hosts: wg - hosts: wg
vars:
services: []
tasks: tasks:
- name: load vars
include_vars:
file: '../result/{{item}}.yaml'
with_items:
- global-vars
- vars-{{inventory_hostname_short}}
- name: directory - name: directory
file: file:
path: '{{ansible_user_dir}}/nextgen-network/{{item}}' path: '{{ansible_user_dir}}/nextgen-network/{{item}}'
......
...@@ -4,7 +4,8 @@ ...@@ -4,7 +4,8 @@
"description": "", "description": "",
"scripts": { "scripts": {
"build": "tsc", "build": "tsc",
"start": "node build/inventory.js" "start": "node build/inventory.js",
"all": "npm run build && npm start"
}, },
"dependencies": { "dependencies": {
"@types/ip": "^1.1.0", "@types/ip": "^1.1.0",
......
...@@ -96,22 +96,16 @@ class InventoryBuilder { ...@@ -96,22 +96,16 @@ class InventoryBuilder {
host.wgPublickey = await this.wgPublickey(host.wgPrivateKey); host.wgPublickey = await this.wgPublickey(host.wgPrivateKey);
} }
this.vars = await this.loadUtilities(); 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)); // 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 hostsArray = await Promise.all(Object.values(this.hosts).map(async (h) => [h.name, await this.host_vars(h)]));
const hosts = Object.fromEntries(rawHosts); //const hosts = Object.fromEntries(hostsArray);
// console.log(hosts); //await fs.promises.writeFile('result/inventory.yaml', YAML.stringify({
/* const rawHostsForSwitch = rawHosts.map(rh => { // wg: { hosts }
const hostVars = JSON.parse(JSON.stringify(rh[1])); //}));
hostVars.ansible_ssh_host = hostVars.address; await fs.promises.writeFile('result/global-vars.yaml', YAML.stringify(this.vars));
hostVars.ansible_ssh_port = this.hosts[rh[0]].sshSystem || 22; await Promise.all(hostsArray.map(host => fs.promises.writeFile(`result/vars-${host[0]}.yaml`, YAML.stringify(host[1]))));
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);
} }
async loadUtilities() { async loadUtilities() {
...@@ -141,6 +135,15 @@ class InventoryBuilder { ...@@ -141,6 +135,15 @@ class InventoryBuilder {
return vars; 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) { async host_vars(host) {
const connections = []; const connections = [];
host.dockerServices = { host.dockerServices = {
...@@ -195,10 +198,10 @@ class InventoryBuilder { ...@@ -195,10 +198,10 @@ class InventoryBuilder {
} }
return { return {
ansible_ssh_host: host.host, //ansible_ssh_host: host.host,
ansible_ssh_user: host.user, //ansible_ssh_user: host.user,
ansible_ssh_port: host.sshPort || 22, //ansible_ssh_port: host.sshPort || 22,
ansible_python_interpreter: host.python || 'python3', //ansible_python_interpreter: host.python || 'python3',
id: host.id, id: host.id,
address: host.address, address: host.address,
isCN: host.location.startsWith('CN'), 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