You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Ansible中将主机指定为变量?配置后执行报错求助

Ansible 动态指定主机执行 playbook 失败问题

网上找了不少相关示例,但都解决不了我的问题,具体情况如下:

情况1:通过变量指定目标主机

我的 test.yaml 内容如下:

---
- hosts: "{{ non_default_host }}"
  tasks:
    - debug:
        msg: 'This is the host specified'

执行以下命令:

ansible-playbook -i hosts test.yaml -e "non_default_host=<somehost>"

结果弹出一堆警告,playbook 直接跳过执行:

[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided host list is empty, only localhost is available
[WARNING]: Could not match supplied host pattern, ignoring somehost

PLAY [somehost]
skipping: no hosts matched

情况2:将主机改为 all

把 test.yaml 里的 hosts 改成 all:

---
- hosts: all
  tasks:
    - debug:
        msg: 'This is the host specified'

再执行这条命令就能正常运行:

ansible-playbook -i 'somehost,'  test.yaml

内容的提问来源于stack exchange,提问作者user_dev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 20:42:35