首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >失败,而不是在没有匹配主机时发出警告。

失败,而不是在没有匹配主机时发出警告。
EN

Stack Overflow用户
提问于 2020-04-03 09:00:12
回答 2查看 2.2K关注 0票数 2

当库存中没有任何主机时,运行剧本时只会发出警告:

代码语言:javascript
运行
复制
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

是否有一种方法来制造错误而不是警告?

我发现ansible.cfg中有这个参数:

代码语言:javascript
运行
复制
[inventory]
unparsed_is_failed = True

但只有当没有您要使用的库存文件时,它才会返回错误。它没有调查内容。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-01-08 16:03:07

一个简单的解决办法是:

  1. 创建游戏手册"main.yml“,如:

代码语言:javascript
运行
复制
---
# 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
...
...
...

  1. 将主机变量放入json文件"var.json":

中。

代码语言:javascript
运行
复制
{
    "RUNNER": {
        "HOSTNAME": "hostname-to-check"
    },
    "VAR1":{
        "CIAO": "CIAO"
    }
}

  1. 运行命令:

代码语言:javascript
运行
复制
ansible-playbook main.yml --extra-vars="jsonfilename=var.json"

您还可以根据自己的喜好对此解决方案进行调整,并通过命令直接传递主机名。

代码语言:javascript
运行
复制
ansible-playbook -i hostname-to-check, my_playbook.yml

但在最后一种情况下,请记住把你的剧本:

代码语言:javascript
运行
复制
hosts: all
票数 1
EN

Stack Overflow用户

发布于 2020-04-03 09:45:56

警告:如果主机列表为空,则只有本地主机可用。请注意,隐式localhost与“all”不匹配。

问:"Is有一种方法来制造错误,而不是Warning?"

答:是的。它是。在剧本里试一试。例如

代码语言:javascript
运行
复制
- 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":“错误空库存。没有可用主机”}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61008355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档