Dynamic inventory

I need help creating a dynanic inventory within a playbook with the following information returned by the URI module:

{
	"3ed941bc_136b_40bf_9614_4ea3cd0a13e5": {
		"hosts": [
			"host1.example.com",
			"host2.example.com"
		]
	},
	"_meta": {
		"hostvars": {
			"host1.example.com": {
				"hostname": "host1",
				"domain": "example.com",
				"virtualization": {
					"isVirtual": true,
					"guest_name": "MyGuest",
					"type": "vmware",
					"datacenter_name": "MyDataCenter",
					"folder_name": "MyFolder"
				},
				"patching": {
					"ppsa": false,
					"excludedPackages": [
						"ep1",
						"ep2",
						"ep3"
					]
				},
				"enabledChannels": [
					"ch1",
					"ch2",
					"ch3"
				],
				"managedServices": [
					"ms1",
					"ms2",
					"ms3"
				],
				"skipFinalReboot": false,
				"doSnapshot": false,
				"pcs_sid": "6957b20f-48fa-4be5-a6e0-0b5cab2959cb"
			},
			"host2.example.com": {
				"hostname": "host2",
				"domain": "example.com",
				"virtualization": {
					"isVirtual": true,
					"guest_name": "MyGuest2",
					"type": "vmware",
					"datacenter_name": "MyDataCenter2",
					"folder_name": "MyFolder2"
				},
				"patching": {
					"ppsa": false,
					"excludedPackages": [
						"ep4",
						"ep5",
						"ep6"
					]
				},
				"enabledChannels": [
					"ch4",
					"ch5",
					"ch6"
				],
				"managedServices": [
					"ms4",
					"ms5",
					"ms6"
				],
				"skipFinalReboot": false,
				"doSnapshot": false,
				"pcs_sid": "e098b9cd-150c-4b2e-aa0f-d4868a5c9cfc"
			}
		}
	}
}

You’ve not said what exactly you need help with. What would also be helpful is to know why you want to do this. (tbh it sounds a bit strange to me wanting to create an Ansible dynamic inventory from a playbook)

You may also want to look at the add_host module which permits adding a hosts / groups to inventories for later plays in the playbook

1 Like

That is the format expected for the ansible.builtin.script inventory plugin ansible.builtin.script inventory – Executes an inventory script that returns JSON — Ansible Community Documentation. You could create an executable file that prints the json.

    - name: Create inventory script
      copy:
        content: |
          #!/bin/bash
          echo '{{ result.content | to_json }}'
        dest: /path/to/inv.sh
        mode: '0755'

And then validate it works with ansible-inventory:

ansible-inventory -i /path/to/inv.sh --list

solved.

- name: get info 
  hosts: localhost
  become: false
  connection: local
  gather_facts: false
  tasks:
    - name: get content 
      uri:
        url: "http://servera/pub/{{ session }}"
        method: GET
        return_content: yes
      register: loaded_info

    - name: debug loaded info
      set_fact:
        my_inv: "{{ loaded_info.content| from_json }}"

    - name: Create inventory
      add_host:
        name: "{{ item.key }}"
        groups: patching_hosts
        ansible_host: "{{ item.key }}"
        ansible_hostname: "{{ item.value.hostname }}"
        pcs_sid: "{{ item.value.patching.pcs_sid }}"
        domain: "{{ item.value.domain }}"
        virtualization: "{{ item.value.virtualization }}"
        patching: "{{ item.value.patching }}"
        enabledChannels: "{{ item.value.patching.enabledChannels }}"
        managedServices: "{{ item.value.patching.managedServices }}"
        skipFinalReboot: "{{ item.value.patching.skipFinalReboot }}"
        doSnapshot: "{{ item.value.patching.doSnapshot }}"
      loop: "{{ my_inv['_meta']['hostvars'] | dict2items }}"
1 Like