# Dynamic inventory

**URL:** <https://forum.ansible.com/t/dynamic-inventory/7289>\
**Category:** Get Help\
**Tags:** playbook, awx\
**Created:** [July 16, 2024, 9:13am UTC](https://forum.ansible.com/t/dynamic-inventory/7289 "2024-07-16T09:13:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fabeer](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/fabeer/32/7591_2.png) [@fabeer](https://forum.ansible.com/u/fabeer)\
**Post date:** [July 16, 2024, 9:13am UTC](https://forum.ansible.com/t/dynamic-inventory/7289/1 "2024-07-16T09:13:41Z")

</div>

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

```auto
{
	"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"
			}
		}
	}
}

```

---

<div class="post-metadata">

**Author:** ![jpmens](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jpmens/32/5543_2.png) [@jpmens](https://forum.ansible.com/u/jpmens)\
**Post date:** [July 16, 2024, 12:19pm UTC](https://forum.ansible.com/t/dynamic-inventory/7289/2 "2024-07-16T12:19:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [July 16, 2024, 1:31pm UTC](https://forum.ansible.com/t/dynamic-inventory/7289/3 "2024-07-16T13:31:49Z")

</div>

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](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/script_inventory.html). You could create an executable file that prints the json.

```auto
    - 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`

---

<div class="post-metadata">

**Author:** ![fabeer](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/fabeer/32/7591_2.png) [@fabeer](https://forum.ansible.com/u/fabeer)\
**Post date:** [July 16, 2024, 2:49pm UTC](https://forum.ansible.com/t/dynamic-inventory/7289/4 "2024-07-16T14:49:59Z")

</div>

solved.

```auto
- 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 }}"

```
