feat: 主机变量支持 - 每台主机独立BMC/IPMI配置

- 后端: inventory生成时写入主机自定义变量(host_vars)
- 前端: 主机编辑表单新增'主机变量'输入区(key=value格式)
- 模板: ipmitool-bmc改为读取主机变量,未配置时用默认值
- 示例: 每台主机设置 bmc_ip=10.10.10.x,Playbook自动区分
- 变量写入inventory: web ansible_host=... bmc_ip=10.10.10.1
This commit is contained in:
cnbugs
2026-07-30 23:07:10 +08:00
parent 0cc2103a90
commit c161c4487a
3 changed files with 43 additions and 17 deletions
+6 -1
View File
@@ -845,6 +845,10 @@ async function openHostModal(id){
</div>
<div class="f-group" style="margin-bottom:4px"><label class="f-label">所属主机组(可多选)</label>
<div class="chips" id="hf-groups">${S.groups.map(g=>`<span class="chip ${(h?.groups||[]).includes(g.name)?'on':''}" data-g="${esc(g.name)}">${esc(g.name)}</span>`).join('')||'<span class="f-hint" style="margin:0">暂无主机组,可在主机页右侧面板创建</span>'}</div>
</div>
<div class="f-group" style="margin-bottom:4px"><label class="f-label">主机变量(每行一个 key=value,如 bmc_ip=10.10.10.1</label>
<textarea class="inp mono" id="hf-vars" rows="3" placeholder="bmc_ip=10.10.10.1&#10;bmc_user=admin&#10;bmc_password=Admin@123">${Object.entries(h?.vars||{}).map(([k,v])=>k+'='+v).join('\n')}</textarea>
<div class="f-hint">变量会写入 inventoryPlaybook 中可用 {{ '{{' }} bmc_ip {{ '}}' }} 引用</div>
</div>`,
'',`<button class="btn btn-ghost" onclick="closeModal()">取消</button><button class="btn btn-primary" id="hf-save">${h?'保存修改':'添加主机'}</button>`);
const seg=$('#hf-auth');
@@ -858,7 +862,8 @@ async function openHostModal(id){
const auth=seg.querySelector('.on').dataset.v;
const body={name,ip,port:+$('#hf-port').value||22,username:$('#hf-user').value.trim()||'root',auth_type:auth,
password:auth==='password'?$('#hf-pwd').value:'',ssh_key:auth==='sshkey'?$('#hf-key').value:'',
groups:$$('#hf-groups .chip.on').map(c=>c.dataset.g)};
groups:$$('#hf-groups .chip.on').map(c=>c.dataset.g),
vars:Object.fromEntries($('#hf-vars').value.split('\n').map(l=>l.trim()).filter(l=>l&&!l.startsWith('#')).map(l=>{const i=l.indexOf('=');return i>0?[l.slice(0,i).trim(),l.slice(i+1).trim()]:null;}).filter(Boolean))};
await withBtn($('#hf-save'),async()=>{
if(h)await api(`/api/hosts/${h.id}`,{method:'PUT',body});
else await api('/api/hosts',{method:'POST',body});