我可以在 playbook 中使用来自 Web 服务的库存数据吗? [英] Can I use inventory data from a web service within a playbook?

查看:29
本文介绍了我可以在 playbook 中使用来自 Web 服务的库存数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前通过

# ansible-playbook -i myscript.py myplaybook.yaml

其中 myscript.py 生成相关的主机信息(根据 文档) 和 myplaybook.py

where myscript.py generates the relevant host information (per the documentation) and myplaybook.py starts with

---
- hosts: all
(...)

这很好用.

我现在想要

  • 通过网络服务接收库存:在剧本中包含一个调用网络服务 并以适当的格式接收库存,无论它是什么(我控制网络服务)
  • 以及直接在剧本中使用这个清单,没有 -i 参数,让 host: all 指令理解它应该使用它.
  • receive the inventory via a web service: include within the playbook a call to the web service and receive the inventory in the appropriate format, whatever it is (I control the web service)
  • as well as make use of this inventory directly within the playbook, without the -i parameter, having the host: all directive understand that it is supposed to use it.

这在ansible中是可能的吗?我的印象是在剧本开始时需要库存(=它不能在剧本中生成)

Is this something which is possible in ansible? I am under the impression that the inventory is needed at the start of the playbook (= that it cannot be generated within the playbook)

推荐答案

您可以使用 add_host 模块动态创建库存.
从这样的开始,然后根据您的需要进行修改:

You can create your inventory dynamically with add_host module.
Start with something like this and modify it to your needs:

---
- hosts: localhost
  tasks:
    - add_host: name={{item}} group=hosts_from_webservice
      with_url: https://mywebservice/host_list_as_simple_strings
      # in this example web service should return one ip/hostname by line:
      # 10.1.1.1
      # 10.1.1.2
      # 10.1.1.3

    - add_host: name={{(item | from_json).host}} group=hosts_from_webservice description={{(item | from_json).desc}}
      with_url: https://mywebservice/host_list_as_json_strings
      # in this example web service should return JSON object on every line:
      # {"host":"10.1.1.1","desc":"hello"}
      # {"host":"10.1.1.2","desc":"world"}
      # {"host":"10.1.1.3","desc":"test"}

- hosts: hosts_from_webservice
  tasks:
    - debug: msg="I'm a host from webservice"

这篇关于我可以在 playbook 中使用来自 Web 服务的库存数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆