首页 > 解决方案 > 运行剧本,将主机作为参数传递,但主机不作为 /ansible/hosts 中的条目存在

问题描述

为什么在运行剧本时将主机名作为参数传递,而 /ansible/host 文件中不存在该主机名?

我确实尝试过

ansible-playbook -i 主机名,playook.yml

标签: ansible

解决方案


确保您的主机是剧本中的全部或变量:

---
- name: Test
  hosts: all
  gather_facts: True

  tasks:
    - name: Debug
      debug:
        var: groups

然后运行为:

ansible-playbook --inventory=windows, debug.yml

你会得到:

PLAY [Test] **************************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************
ok: [windows]

TASK [Debug] *************************************************************************************************************************************************************
ok: [windows] => {
    "groups": {
        "all": [
            "windows"
        ],
        "ungrouped": [
            "windows"
        ]
    }
}

PLAY RECAP ***************************************************************************************************************************************************************
windows                    : ok=2    changed=0    unreachable=0    failed=0 

其他选项是使用变量并在运行时传递它:

所以:

---
- name: Test
  hosts: "{{ host }}"
  gather_facts: True

  tasks:
    - name: Debug
      debug:
        var: groups

并做:

ansible-playbook  debug.yml -e host=windows 

除此之外,您还有add_hostgroup_by选项


推荐阅读