Can I use inventory data from a web service in a tutorial?

I am currently running my playbooks using

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

where myscript.py generates the appropriate host information (in the documentation ) and myplaybook.py starts with

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

This works great.

Now i would like

  • get inventory via web service: include a web service call in the playback program and get inventory in the appropriate format, regardless of whether it is (I manage the web service).
  • and also use this inventory directly in the playbook without the -i option, having the host: all directive, understand that it should use it.

Is this something possible out of reach? I get the impression that inventory is needed at the beginning of playback (= that it cannot be created in the textbook)

0
source share
1 answer

A dynamic dynamic array can be created using the add_host module.
Start with something like this and change it to suit 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" 
+2
source

Source: https://habr.com/ru/post/1012949/


All Articles