当库存中没有任何主机时,运行剧本时只会发出警告:
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'是否有一种方法来制造错误而不是警告?
我发现ansible.cfg中有这个参数:
[inventory]
unparsed_is_failed = True但只有当没有您要使用的库存文件时,它才会返回错误。它没有调查内容。
发布于 2021-01-08 16:03:07
一个简单的解决办法是:
---
# Check first if the supplied host pattern {{ RUNNER.HOSTNAME }} matches with the inventory
# or forces otherwise the playbook to fail (for Jenkins)
- hosts: localhost
vars_files:
- "{{ jsonfilename }}"
tasks:
- name: "Hostname validation | If OK, it will skip"
fail:
msg: "{{ RUNNER.HOSTNAME }} not found in the inventory group or hosts file {{ ansible_inventory_sources }}"
when: RUNNER.HOSTNAME not in hostvars
# The main playbook starts
- hosts: "{{ RUNNER.HOSTNAME }}"
vars_files:
- "{{ jsonfilename }}"
tasks:
- Your tasks
...
...
...中。
{
"RUNNER": {
"HOSTNAME": "hostname-to-check"
},
"VAR1":{
"CIAO": "CIAO"
}
}ansible-playbook main.yml --extra-vars="jsonfilename=var.json"您还可以根据自己的喜好对此解决方案进行调整,并通过命令直接传递主机名。
ansible-playbook -i hostname-to-check, my_playbook.yml但在最后一种情况下,请记住把你的剧本:
hosts: all发布于 2020-04-03 09:45:56
警告:如果主机列表为空,则只有本地主机可用。请注意,隐式localhost与“all”不匹配。
问:"Is有一种方法来制造错误,而不是Warning?"
答:是的。它是。在剧本里试一试。例如
- hosts: localhost
tasks:
- fail:
msg: "[ERROR] Empty inventory. No host available."
when: groups.all|length == 0
- hosts: all
tasks:
- debug:
msg: Playbook started空置的存货
致命:本地主机:失败!=> {“已更改”:false,"msg":“错误空库存。没有可用主机”}
。
https://stackoverflow.com/questions/61008355
复制相似问题