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

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

问题描述

我目前通过以下方式运行剧本

I currently run my playbooks via

# 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
(...)

这很好.

我现在想

  • 通过网络服务接收广告资源:在剧本中包含对网络服务的调用并以适当的格式接收广告资源,无论它是什么(我控制Web服务)
  • 以及在没有-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.

这是否可能?我的印象是,在剧本开始时需要存货(=无法在剧本中生成库存)

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"

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

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